readxl vs. RMark: A tibble is not a dataframe

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.
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:
And if you use str(object), the header line mentions the other classes:
The solution is to convert the tibble to a dataframe before you pass it to the functions of RMark.
Hope this note saves others a little time in trouble-shooting if you encounter a similar problem.
Cheers, Brett.
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.