I do not understand how I can unnest a listcolumn of tsibbles. I recently updated the R version of an old project, which caused non working code.
Example data:
library(tidyverse)
library(fable)
library(tsibble)
library(tsibbledata)
data <- tsibbledata::hh_budget
data
# A tsibble: 88 x 8 [1Y]
# Key: Country [4]
Country Year Debt DI Expenditure Savings Wealth Unemployment
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Australia 1995 95.7 3.72 3.40 5.24 315. 8.47
2 Australia 1996 99.5 3.98 2.97 6.47 315. 8.51
3 Australia 1997 108. 2.52 4.95 3.74 323. 8.36
4 Australia 1998 115. 4.02 5.73 1.29 339. 7.68
5 Australia 1999 121. 3.84 4.26 0.638 354. 6.87
6 Australia 2000 126. 3.77 3.18 1.99 350. 6.29
7 Australia 2001 132. 4.36 3.10 3.24 348. 6.74
8 Australia 2002 149. 0.0218 4.03 -1.15 349. 6.37
9 Australia 2003 159. 6.06 5.04 -0.413 360. 5.93
10 Australia 2004 170. 5.53 4.54 0.657 379. 5.40
# ... with 78 more rows
nest the data:
data_nested <- data %>%
nest(data = c(-Country))
This would also do the same nesting:
data_nested <- data %>%
group_by_key() %>%
nest()
data_nested
Results in this:
# A tibble: 4 x 2
# Groups: Country [4]
Country data
<chr> <list>
1 Australia <tsibble [22 x 7]>
2 Canada <tsibble [22 x 7]>
3 Japan <tsibble [22 x 7]>
4 USA <tsibble [22 x 7]>
Now I'd like to unnest this, I would like to get the same as the input data, a tsibble with 88 rows, Key: Country[4],...
Back then I used this:
tsibble::unnest_tsibble(data_nested, cols = data)
Produces an Error Message I do not understand at all.
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 know there were these updates, I played a bit around with unnest_wider and unnest_longer and unnest_legacy.
unnest_longer(data_nested, col = 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
Unnest legacy only allows tibbles...
unnest_legacy(data_nested)
Error: The result is not a valid tsibble.
i Do you need `as_tibble()` to work with data frame?
The only thing that worked is really cumbersome:
data_again_unnested <- data_nested %>%
unnest_wider(data) %>%
unnest(cols = c(-Country))
data_again_unnested
# A tibble: 88 x 8
Country Year Debt DI Expenditure Savings Wealth Unemployment
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Australia 1995 95.7 3.72 3.40 5.24 315. 8.47
2 Australia 1996 99.5 3.98 2.97 6.47 315. 8.51
3 Australia 1997 108. 2.52 4.95 3.74 323. 8.36
4 Australia 1998 115. 4.02 5.73 1.29 339. 7.68
5 Australia 1999 121. 3.84 4.26 0.638 354. 6.87
6 Australia 2000 126. 3.77 3.18 1.99 350. 6.29
7 Australia 2001 132. 4.36 3.10 3.24 348. 6.74
8 Australia 2002 149. 0.0218 4.03 -1.15 349. 6.37
9 Australia 2003 159. 6.06 5.04 -0.413 360. 5.93
10 Australia 2004 170. 5.53 4.54 0.657 379. 5.40
# ... with 78 more rows
But it outputs of course a tibble so I'd would again need to coerce it back to a tsibble, this is quite annoying, a lot of steps, just for a simple unnest.
data_again_unnested %>% as_tsibble(key = Country, index = Year)
# A tsibble: 88 x 8 [1Y]
# Key: Country [4]
Country Year Debt DI Expenditure Savings Wealth Unemployment
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Australia 1995 95.7 3.72 3.40 5.24 315. 8.47
2 Australia 1996 99.5 3.98 2.97 6.47 315. 8.51
3 Australia 1997 108. 2.52 4.95 3.74 323. 8.36
4 Australia 1998 115. 4.02 5.73 1.29 339. 7.68
5 Australia 1999 121. 3.84 4.26 0.638 354. 6.87
6 Australia 2000 126. 3.77 3.18 1.99 350. 6.29
7 Australia 2001 132. 4.36 3.10 3.24 348. 6.74
8 Australia 2002 149. 0.0218 4.03 -1.15 349. 6.37
9 Australia 2003 159. 6.06 5.04 -0.413 360. 5.93
10 Australia 2004 170. 5.53 4.54 0.657 379. 5.40
# ... with 78 more rows
Is there no other way to do it? Has anyone an idea?