我不明白如何取消嵌套列表列。我最近更新了一个旧项目的 R 版本,导致代码无法工作。
示例数据:
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
嵌套数据:
data_nested <- data %>%
nest(data = c(-Country))
这也会做同样的嵌套:
data_nested <- data %>%
group_by_key() %>%
nest()
data_nested
结果如下:
# 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]>
现在我想取消嵌套,我想获得与输入数据相同的 88 行 tsibble,Key:Country[4],...
那时我用这个:
tsibble::unnest_tsibble(data_nested, cols = 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_wider 和 unnest_longer 和 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 只允许 tibbles...
unnest_legacy(data_nested)
Error: The result is not a valid tsibble.
i Do you need `as_tibble()` to work with data frame?
唯一有效的是真的很麻烦:
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
但它当然会输出一个 tibble,所以我需要再次将它强制转换回 tsibble,这很烦人,有很多步骤,只是为了一个简单的 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
没有其他方法可以做到吗?有人有想法吗?