假設我有以下 tibbles 串列:
a_list_of_tibbles <- list(
a = tibble(a = rnorm(10)),
b = tibble(a = runif(10)),
c = tibble(a = letters[1:10])
)
現在我想將它們全部映射到單個資料幀/tibble 中,由于列型別不同,這是不可能的。
我該怎么辦?
我試過這個,但我想擺脫 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)
uj5u.com熱心網友回復:
我們可以在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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383103.html
下一篇:在R中查找字串組之間的距離
