我有这个 dfs 列表:
my_list <- list(structure(list(observations = c(1L, 5L), variables = c(4L,
8L)), class = "data.frame", row.names = c("asp_202003...Copy.xlsx",
"asp_202003.xlsx")), structure(list(observations = c(3L, 1L),
variables = 5:4), class = "data.frame", row.names = c("eay_201008_a.xlsx",
"eay_202003.xlsx")), structure(list(observations = 3:4, variables = c(4L,
6L)), class = "data.frame", row.names = c("wana_202309...Copy.xlsx",
"wana_202309.xlsx")))
我像这样合并 dfs:
my_merge <- my_list %>% reduce(full_join)
输出: 我的_合并
# observations variables
#1 1 4
#2 5 8
#3 3 5
#4 3 4
#5 4 6
但我想将索引名称(或提取它们)保留在名为“文件”的新列中,如下所示:
所需输出:
# file observations variables
# asp_202003...Copy.xlsx 1 4
# asp_202003.xlsx 5 8
# etc.
另请注意,所需的输出应有 6 行,而不是当前 my_merge 对象中的 5 行!在当前的 my_merge 对象中,两行之间的相同值意味着其中一行“丢失”。这也是我想将文件名设置为索引的原因。
您可以先将它们制成小标题并将行名称保存为变量,然后使用
bind_rows()
。
library(dplyr)
my_list <- list(structure(list(observations = c(1L, 5L), variables = c(4L, 8L)), class = "data.frame",
row.names = c("asp_202003...Copy.xlsx", "asp_202003.xlsx")),
structure(list(observations = c(3L, 1L), variables = 5:4), class = "data.frame",
row.names = c("eay_201008_a.xlsx", "eay_202003.xlsx")),
structure(list(observations = 3:4, variables = c(4L, 6L)), class = "data.frame",
row.names = c("wana_202309...Copy.xlsx", "wana_202309.xlsx")))
bind_rows(purrr::map(my_list, ~as_tibble(.x, rownames="file")))
#> # A tibble: 6 × 3
#> file observations variables
#> <chr> <int> <int>
#> 1 asp_202003...Copy.xlsx 1 4
#> 2 asp_202003.xlsx 5 8
#> 3 eay_201008_a.xlsx 3 5
#> 4 eay_202003.xlsx 1 4
#> 5 wana_202309...Copy.xlsx 3 4
#> 6 wana_202309.xlsx 4 6
创建于 2024-03-18,使用 reprex v2.0.2