tidyr::unnest() with different column types

你。 提交于 2020-05-15 10:14:05

问题


Since the update to tidyr version 1.0.0 I have started to get an error when unnesting a list of dataframes.

The error comes because some of the data frames in the list contain a column with all NA values (logical), while other of the dataframes contain the same column but with some character values (character). The columns with all NA values are coded as logicals while the others are coded as character vectors.

The default behavior of earlier versions of tidyr handled the different column types without problems (at least I didn't get this error when running the script).

Can I solve this issue from inside tidyr::unest() ?

Reproducible example:

library(tidyr)

a <- tibble(
  value = rnorm(3),
  char_vec = c(NA, "A", NA))

b <- tibble(
  value = rnorm(2),
  char_vec = c(NA, "B"))

c <- tibble(
  value = rnorm(3),
  char_vec = c(NA, NA, NA))

tibble(
  file = list(a, b, c)) %>% 
  unnest(cols = c(file))
#> No common type for `..1$file$char_vec` <character> and `..3$file$char_vec`
#> <logical>.

Created on 2019-10-11 by the reprex package (v0.3.0)


回答1:


You can convert all relevant columns to character one step before unnesting.

tibble(
  file = list(a, b, c)) %>% 
  mutate(file = map(file, ~ mutate(.x, char_vec = as.character(char_vec)))) %>%
  unnest(cols = c(file))

If there are several columns that need treatment you can do:

 tibble(
  file = list(a, b, c)) %>% 
  mutate(file = map(file, ~ mutate_at(.x, vars(starts_with("char")), ~as.character(.)))) 

Data for the latter example:

a <- tibble(
  value = rnorm(3),
  char_vec = c(NA, "A", NA),
  char_vec2 = c(NA, NA, NA))

b <- tibble(
  value = rnorm(2),
  char_vec = c(NA, "B"),
  char_vec2 = c("C", "A"))

c <- tibble(
  value = rnorm(3),
  char_vec = c(NA, NA, NA),
  char_vec2 = c("B", NA, "A"))



来源:https://stackoverflow.com/questions/58337311/tidyrunnest-with-different-column-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!