1

Recent versions of dplyr can bind_rows even with classes like yearqtr. However, I am having a problem when I also use the .id parameter. For example:

library(dplyr) # dplyr_1.0.6, vctrs_0.3.8 
bind_rows(a = tibble(d = as.Date('2000-1-1')), b = tibble(d = as.Date('2000-1-1')), .id = 'Id') # Ok
library(zoo) # zoo_1.8-9
bind_rows(a = tibble(d = as.yearqtr('2000q1')), b = tibble(d = as.yearqtr('2000q1'))) # Ok
bind_rows(a = tibble(d = as.yearqtr('2000q1')), b = tibble(d = as.yearqtr('2000q1')), .id = 'Id') # Problem

The problem with the last line is that the d column ends up a strange class, vctrs:::common_class_fallback instead of the expected yearqtr class.

Edit: Is this a bug?

banbh
  • 1,331
  • 1
  • 13
  • 31
  • Not all classes are compatible based on the `vctrs` docs. What do you expect as a solution? You may use `vec_rbind(tibble(d = as.yearqtr('2000q1')), tibble(d = as.yearqtr('2000q1')))` but, it wouldn't keep an 'Id' column as `.id` argument is not there – akrun Sep 28 '21 at 21:25
  • @akrun I think `yearqtr` is compatible. If you try the penultimate line in my example (which uses `yearqtr` but has no `.id`) then it works fine. So the answer to your question is that I expect both of the last two expressions to produce a tibble with a `d` column with class `yearqtr` (but only the first does!) – banbh Sep 28 '21 at 21:32
  • Another option is `rbindlist` from `data.table`, which works as well `rbindlist(list(a = tibble(d = as.yearqtr('2000q1')), b = tibble(d = as.yearqtr('2000q1'))), idcol = 'id') %>% as_tibble` – akrun Sep 28 '21 at 21:35
  • Internally, it is calling the `vec_rbind`. and the issue arises from this step `vec_rbind(tibble(d = as.yearqtr('2000q1')), tibble(d = as.yearqtr('2000q1')), .names_to = "id")` – akrun Sep 28 '21 at 21:39
  • I have added an edit to my question with an explicit question, namely whether this is a bug. I suspect it is. – banbh Sep 29 '21 at 01:00
  • @akrun I was playing around with `vec_rbind` and it seems to provide an interesting workaround. Instead of `bind_rows(..., .id='Id')` one can do `vec_rbind(..., .id=NULL) %>% rownames_to_column('Id')`. – banbh Sep 29 '21 at 01:10
  • It seems it is related to having one column since this works: `bind_rows(a = tibble(d = as.yearqtr('2000q1'), v = 11), b = tibble(d = as.yearqtr('2000q1'), v = 12), .id = "id")` – G. Grothendieck Sep 29 '21 at 06:03
  • But `bind_rows(a = tibble(v = 11, d = as.yearqtr('2000q1')), b = tibble(v = 12, d = as.yearqtr('2000q1')), .id = "id")` doesn't work. So perhaps the issue is certain classes (`yearqtr` but not `Date`) appearing in the *last* column? – banbh Sep 29 '21 at 13:04
  • In fact if there are multiple columns of type `yearqtr` only the last one, if it is the vert last column, has the above problem. It seems to me this is clearly a bug. – banbh Sep 30 '21 at 18:56
  • I created an issue for `vctrs`: https://github.com/r-lib/vctrs/issues/1462 – banbh Oct 01 '21 at 13:40

0 Answers0