英文:
unnest_longer column with char and list entries
问题
I am trying to convert JSON file to tibble. First I combine all list entries of the JSON file to a tibble that looks like this:
df <- tibble(none_nested = c("a", "b", "c"),
all_nested = c(list(list("a", "b")), list(list("a", "b")), list(list("a", "b"))),
some_nested = c("a", "b", list(list("a", "b")))
)
The problem is that I cannot call unnest_longer on some_nested.
unnest_longer(df, some_nested)
->
Error in `col_to_long()`:
! Can't combine `..1$some_nested` <character> and `..4$some_nested` <list>.
英文:
I am trying to convert JSON file to tibble. First I combine all list entries of the JSON file to a tibble that looks like this:
df <- tibble(none_nested = c("a", "b", "c"),
all_nested = c(list(list("a", "b")), list(list("a", "b")), list(list("a", "b"))),
some_nested = c("a", "b", list(list("a", "b")))
)
The problem is that I cannot call unnest_longer on some_nested.
unnest_longer(df, some_nested)
->
Error in `col_to_long()`:
! Can't combine `..1$some_nested` <character> and `..4$some_nested` <list>.
答案1
得分: 1
在some_nested
列中,一些行不在列表中(第一行和第二行是字符),因此您无法使用unnest_longer
。
下面的代码应该可以工作:
df %>%
mutate(some_nested = map(some_nested, ~if(is.list(.x)) .x else list(.x))) %>%
unnest_longer(some_nested)
英文:
In the some_nested column, some rows are not in the list (first and second rows are in char) so you couldn't use the unnest_longer.
below code should work
df |>
mutate(some_nested = map(some_nested, ~if(is.list(.x)) .x else list(.x))) |>
unnest_longer(some_nested)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论