readxl vs. RMark: A tibble is not a dataframe

posts related to the RMark library, which may not be of general interest to users of 'classic' MARK

readxl vs. RMark: A tibble is not a dataframe

Postby B.K. Sandercock » Fri Sep 13, 2019 2:59 am

RMark folks:

Writing a cautionary note on a conflict between packages that took me some time to sort out. I used the readxl package to import an Excel file into R for a nest survival analysis. When I tried to run the RMark code that I had successfully used for other analyses, I received the following error messages. The messages were confusing because I had already coerced year to be a factor.

Code: Select all
Error: Must use a vector in `[`, not an object of class matrix.
Call `rlang::last_error()` to see a backtrace
In addition: Warning messages:
1: Unknown or uninitialised column: 'data'.
2: Unknown or uninitialised column: 'ch'.
3: Unknown or uninitialised column: 'AgeDay1'.
4: Unknown or uninitialised column: 'data'.
5: In process.data(data, begin.time = begin.time, model = model, mixtures = mixtures,  :
 
  Year  is not a factor variable. Coercing to factor.


My problem was that readxl creates objects that are a Tibble instead of a dataframe. If you use head(object), the view is a little different:
Code: Select all
> head(nest)
# A tibble: 6 x 9
  Year  Host        Para  FirstFound LastPresent LastChecked  Fate NestAge  Freq
  <fct> <fct>       <fct>      <int>       <int>       <int> <int>   <int> <int>
1 2013  Common Myna No            73         112         112     0      NA     1
2 2013  Common Myna No            79         109         109     0      NA     1
3 2013  Common Myna No            79         106         106     0      NA     1
4 2013  Common Myna No            97         104         104     0      NA     1
5 2013  Common Myna No            92         128         128     0      NA     1
6 2013  Long taile~ Yes           92         100         104     1      NA     1


And if you use str(object), the header line mentions the other classes:
Code: Select all
> str(nest)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':       562 obs. of  9 variables:
 $ Year       : Factor w/ 9 levels "2008","2009",..: 6 6 6 6 6 6 6 6 6 6 ...
 $ Host       : Factor w/ 3 levels "Common Myna",..: 1 1 1 1 1 3 1 1 1 1 ...
 $ Para       : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 2 1 1 1 1 ...
 $ FirstFound : int  73 79 79 97 92 92 117 117 117 109 ...
 $ LastPresent: int  112 109 106 104 128 100 122 142 117 118 ...
 $ LastChecked: int  112 109 106 104 128 104 122 142 121 118 ...
 $ Fate       : int  0 0 0 0 0 1 0 0 1 0 ...
 $ NestAge    : int  NA NA NA NA NA NA NA NA NA NA ...
 $ Freq       : int  1 1 1 1 1 1 1 1 1 1 ...


The solution is to convert the tibble to a dataframe before you pass it to the functions of RMark.
Code: Select all
nest = as.data.frame(nest)


Hope this note saves others a little time in trouble-shooting if you encounter a similar problem.

Cheers, Brett.
B.K. Sandercock
 
Posts: 48
Joined: Mon Jun 02, 2003 4:18 pm
Location: Norwegian Institute of Nature Research

Re: readxl vs. RMark: A tibble is not a dataframe

Postby jlaake » Mon Sep 16, 2019 11:51 am

Sorry about that. I should have posted something a few months ago when this was brought to my attention. The version on my google drive fixes this problem by converting it to a dataframe. It will be posted to CRAN once I complete some other changes.

--jeff
jlaake
 
Posts: 1479
Joined: Fri May 12, 2006 12:50 pm
Location: Escondido, CA


Return to RMark

Who is online

Users browsing this forum: No registered users and 1 guest