如何解决我如何才能嵌套一个小清单列表?
我不明白如何才能嵌套一个列表列。 我最近更新了旧项目的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行的小消息,键: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 max; returning -Inf
不必要的遗产仅允许小标题...
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
但是它当然会输出一个小提示,因此我将不得不再次将其强制回一个小提示,这很烦人,需要很多步骤,只是为了一个简单的嵌套。
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
还有别的办法吗?有人知道吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。