gpt4 book ai didi

r - 改变 tibbles 列表中的所有列

转载 作者:行者123 更新时间:2023-12-05 02:35:57 26 4
gpt4 key购买 nike

假设我有以下 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com