【发布时间】:2023-02-06 13:17:01
【问题描述】:
我一直在努力了解tidyr 和tibblify 中的取消嵌套函数。我相信您应该能够使用 unnest_longer() 来复制下面将这种嵌套列表变成 tibble 的更多手动方法,但我一直在努力研究文档。如何执行此操作的正确示例将极大地帮助我:
# Example nested list
nl <- list(time = list("2023-02-06", "2023-02-07", "2023-02-08",
"2023-02-09", "2023-02-10", "2023-02-11",
"2023-02-12"),
precipitation_sum = list(0.9, 0, 0, 0.3, 0, 0, 0))
# one way to do it (extract colnames and construct)
tibble(!!! setNames(map(nl, unlist),names(nl)))
# another way (collect & reduce each sublist)
as_tibble(lapply(nl, function(x) Reduce(c, x)))
# how to use tidyr and unnest_longer? (below is incorrect)
unnest_longer(tibble(nl), col = everything())
【问题讨论】:
标签: r tidyr nested-lists