tsibble
tsibble copied to clipboard
Unnest doesn't work when nesting the index
I'm trying to perform the following nest and unnest actions.
library(tidyr)
library(tsibble)
data <- tsibbledata::hh_budget
data_nested <- data %>% nest(data = -Country)
data_nested %>% unnest(data)
#> Error in if (unknown_interval(interval) && (nrows > vec_size(key_data))) { :
#> missing value where TRUE/FALSE needed
#> In addition: Warning messages:
#> 1: In min(..., na.rm = TRUE) :
#> no non-missing arguments to min; returning Inf
#> 2: In max(..., na.rm = TRUE) :
#> no non-missing arguments to max; returning -Inf
I found out that the error comes from here:
vctrs::vec_unchop(data_nested$data)
#> Error in if (unknown_interval(interval) && (nrows > vec_size(key_data))) { :
#> missing value where TRUE/FALSE needed
#> In addition: Warning messages:
#> 1: In min(..., na.rm = TRUE) :
#> no non-missing arguments to min; returning Inf
#> 2: In max(..., na.rm = TRUE) :
#> no non-missing arguments to max; returning -Inf
I actually get the expected result if I do the following, however I can't define indices from unnest.
vctrs::vec_unchop(data_nested$data, indices = tsibble::key_rows(data))
#> # A tsibble: 88 x 7 [1Y]
#> Year Debt DI Expenditure Savings Wealth Unemployment
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1995 95.7 3.72 3.40 5.24 315. 8.47
#> 2 1996 99.5 3.98 2.97 6.47 315. 8.51
#> 3 1997 108. 2.52 4.95 3.74 323. 8.36
#> 4 1998 115. 4.02 5.73 1.29 339. 7.68
#> 5 1999 121. 3.84 4.26 0.638 354. 6.87
#> 6 2000 126. 3.77 3.18 1.99 350. 6.29
#> 7 2001 132. 4.36 3.10 3.24 348. 6.74
#> 8 2002 149. 0.0218 4.03 -1.15 349. 6.37
#> 9 2003 159. 6.06 5.04 -0.413 360. 5.93
#> 10 2004 170. 5.53 4.54 0.657 379. 5.40
#> # ... with 78 more rows
I don't have the same error if Year (the index) is one of the nesting variables:
data %>% nest(data = -c(Year)) %>% unnest(data)
data %>% nest(data = -c(Country, Year)) %>% unnest(data)
However the outputs are tibbles instead of tsibbles.
Here sessionInfo():
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tsibble_0.9.2 tidyr_1.1.2
loaded via a namespace (and not attached):
[1] Rcpp_1.0.5 rstudioapi_0.11 knitr_1.30 magrittr_1.5 tidyselect_1.1.0 anytime_0.3.9
[7] R6_2.4.1 rlang_0.4.7 fansi_0.4.1 dplyr_1.0.2 tools_4.0.2 xfun_0.15
[13] utf8_1.1.4 cli_2.0.2 tsibbledata_0.2.0 htmltools_0.5.0 ellipsis_0.3.1 assertthat_0.2.1
[19] yaml_2.2.1 digest_0.6.25 tibble_3.0.3 lifecycle_0.2.0 crayon_1.3.4 purrr_0.3.4
[25] vctrs_0.3.4 glue_1.4.2 evaluate_0.14 rmarkdown_2.3 compiler_4.0.2 pillar_1.4.6
[31] generics_0.0.2 lubridate_1.7.9 pkgconfig_2.0.3
Since {tidyr} v1.0.0, unnest() no longer works for tsibble and also makes it impossible to adapt for tsibble.
The data_nested is a tbl_df, and unnest.tbl_df() doesn't know how to properly handle a list of tsibbles. I can't provide unnest.tbl_df() in {tsibble} to overwrite it.
library(tidyr)
library(tsibble)
library(tsibbledata)
data <- hh_budget
data_nested <- data %>% nest(data = -Country)
class(data_nested)
#> [1] "tbl_df" "tbl" "data.frame"
Created on 2020-09-26 by the reprex package (v0.3.0)
There was the function unnest_tsibble but is now marked as deprecated. What is the alternative?