Tidyr: Unnest no longer handles lists with date vectors

Created on 8 Feb 2018  路  3Comments  路  Source: tidyverse/tidyr

Reproducible example:

library(dplyr)
library(tidyr)
library(lubridate)
df1 <- seq(from = as.Date('2017-01-01'), to = as.Date('2017-01-10'), by = "1 day") %>%
  list() %>%
  data_frame(x = .)
df2 <- seq(from = as.Date('2017-01-01'), to = as.Date('2017-01-10'), by = "1 day") %>%
  as.integer() %>%
  list() %>%
  data_frame(x = .)

Using the above, I am unable to unnest df1 but can get around it by coercing dates to integers and then back to dates.

> unnest(df1)
Error: Each column must either be a list of vectors or a list of data frames [x]
> unnest(df2) %>% mutate(x = as_date(x))
# A tibble: 10 x 1
   x         
   <date>    
 1 2017-01-01
 2 2017-01-02
 3 2017-01-03
 4 2017-01-04
 5 2017-01-05
 6 2017-01-06
 7 2017-01-07
 8 2017-01-08
 9 2017-01-09
10 2017-01-10

Below is my sessionInfo():

> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux

Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] bindrcpp_0.2    lubridate_1.7.2 tidyr_0.8.0     dplyr_0.7.4    

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15     utf8_1.1.3       crayon_1.3.4     assertthat_0.2.0 R6_2.2.2         magrittr_1.5    
 [7] pillar_1.1.0     cli_1.0.0        stringi_1.1.6    rlang_0.1.6      rstudioapi_0.7   tools_3.4.3     
[13] stringr_1.2.0    glue_1.2.0       purrr_0.2.4      yaml_2.1.16      compiler_3.4.3   pkgconfig_2.0.1 
[19] tidyselect_0.2.3 bindr_0.1        tibble_1.4.2
bug

Most helpful comment

Fixed in 96ded3f5be0122a9b533b69cc160219a9bc45cd2

All 3 comments

I think the change happened in commit f4188173 to make unnest handles list of lists.
https://github.com/tidyverse/tidyr/blob/f418817335eac47813d0f7bafb377eb2caa1b070/R/unnest.R#L144-L155

There is an is.object test activated because x is of class Date. Then, df1 is seen as type "mixted" that throws the error in unnest.

And it seems to mean than unnest cannot handles any typed element because if I understand well is.object will return true (so will get an error) for any object with a class. That is verified:

library(dplyr, warn.conflicts = F)
library(tidyr)
as.factor(month.name) %>%
  list() %>%
  data_frame(x = .) %>%
  unnest()
#> Error: Each column must either be a list of vectors or a list of data frames [x]

hms::hms(hours = 1:10) %>%
  list() %>%
  data_frame(x = .) %>%
  unnest()
#> Error: Each column must either be a list of vectors or a list of data frames [x]

Created on 2018-02-10 by the reprex package (v0.2.0).

This is an issue with more than dates vectors...
Not sure how to handle that yet. I just wanted to help identify the error.

Minimal reprex:

library(tidyr)

df <- tibble::tibble(x = as.list(as.factor(letters[1:3])))
unnest(df)
#> Error: Each column must either be a list of vectors or a list of data frames [x]

@thays42 in the future, I'd appreciate it if you didn't include session info unless it's explicitly asked for, or you've used reprex::reprex(..., si = TRUE) to hide it away. Otherwise it clutters the discussion without adding significant value.

Fixed in 96ded3f5be0122a9b533b69cc160219a9bc45cd2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidhunterwalsh picture davidhunterwalsh  路  4Comments

mindymallory picture mindymallory  路  3Comments

andrewpbray picture andrewpbray  路  8Comments

kendonB picture kendonB  路  5Comments

romagnolid picture romagnolid  路  8Comments