首页 > 解决方案 > 如何“取消嵌套”两个不兼容的嵌套 tibble 列?

问题描述

我有一列嵌套的小标题。我想x将第二行的值转换为一个列表,以便我可以取消嵌套。如何根据列名 ( x) 及其格式 ( character) 更改此嵌套列表的值?

tibble(data = c(tibble(x = list(NULL)), 
                 tibble(x = ""))) %>% 
  unnest(data)
#> Error: Can't combine `..1$data` <list> and `..2$data` <character>.

标签: rtidyr

解决方案


@Duck's comment worked for me.

library(dplyr)
library(tidyr)
tibble(data = c(tibble(x = list(NULL)), tibble(x = ""))) %>% 
unnest_wider(data)

Let me try to explain:

tidy::unnest() struggles to unnest tibbles that have more than one level of nesting. Unnest_wider is capable of "rectangling" which the tidyverse website describes as "the art and craft of taking a deeply nested list (often sourced from wild caught JSON or XML) and taming it into a tidy data set of rows and columns."

Unnest_wider takes each element of a list-column and makes a new column.


推荐阅读