假设我有以下 tibbles 列表:
a_list_of_tibbles <- list(
a = tibble(a = rnorm(10)),
b = tibble(a = runif(10)),
c = tibble(a = letters[1:10])
)
现在我想将它们全部映射到一个数据框/小标题中,由于列类型不同,这是不可能的。
我该怎么做?
我已经试过了,但我想去掉 for 循环
for(i in 1:length(a_list_of_tibbles)){
a_list_of_tibbles[[i]] <- a_list_of_tibbles[[i]] %>% mutate_all(as.character)
}
然后我运行:
map_dfr(.x = a_list_of_tibbles, .f = as_tibble)
最佳答案
我们可以在 map
中进行计算 - 使用 across
而不是后缀 _all
(即将弃用)来遍历数据集的列
library(dplyr)
library(purrr)
map_dfr(a_list_of_tibbles,
~.x %>%
mutate(across(everything(), as.character) %>%
as_tibble))
-输出
# A tibble: 30 × 1
a
<chr>
1 0.735200825884485
2 1.4741501589461
3 1.39870958697574
4 -0.36046362308853
5 -0.893860999301402
6 -0.565468636033674
7 -0.075270267983768
8 2.33534260196058
9 0.69667906338348
10 1.54213170143702
# … with 20 more rows
关于r - 改变 tibbles 列表中的所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70385341/