问题描述
自从更新到tidyr
版本1.0.0以来,取消嵌套数据帧列表时,我开始收到错误消息.
Since the update to tidyr
version 1.0.0 I have started to get an error when unnesting a list of dataframes.
出现错误是因为列表中的某些数据框包含具有所有NA值(逻辑)的列,而其他数据框包含相同的列但具有某些字符值(字符).具有所有NA值的列被编码为逻辑,而其他列被编码为字符向量.
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.
tidyr
的早期版本的默认行为可以毫无问题地处理不同的列类型(至少在运行脚本时没有出现此错误).
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).
我可以从tidyr::unest()
内部解决此问题吗?
Can I solve this issue from inside tidyr::unest()
?
可复制的示例:
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>.
推荐答案
您可以在取消嵌套之前将所有相关列转换为字符.
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(.))))
后一个示例的数据:
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"))
这篇关于tidyr :: unnest()具有不同的列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!