在 R 中,如何在多个数据帧上使用 left_join?
In R, how to use left_join on several data frames?
如何更简单的合并dataframe?
我有三个数据帧 (table_base / table_a / table_b)。我想按行组合它们以获得 'table_final' 的结果。下面是我的代码,它可以工作,但有点复杂。我怎样才能简化它?
实际上,除了 table_a 和 table_b.
,我还有更多的表要加入
library(dplyr)
table_base <- data.frame(cat=c("a","b","c","d"))
table_a <- data.frame(cat=c("a","b"),
value=c(1,2))
table_b <- data.frame(cat=c("a","c","d"),
value=c(7,9,10))
table_final <- table_base %>%
left_join(table_a,by='cat',fill=0) %>%
left_join(table_b,by='cat') %>%
mutate(value=if_else(!is.na(value.x),value.x,value.y)) %>%
select(cat,value)
您只需要从每个 table 中取出适当的行并绑定它们:
table_list <- list(table_a, table_b)
table_list %>%
map("cat") %>%
map2(c(list(NULL), accumulate(head(., -1), union)), setdiff) %>%
map2_dfr(table_list, ~filter(.y, cat %in% .x))
使用purrr::reduce合并多个dataframes,然后使用dplyr::coalesce 获取第一个非 na 值:
library(dplyr)
library(purrr)
list(table_base, table_a, table_b) %>%
reduce(left_join, by = "cat") %>%
mutate(value = coalesce(!!!select(., starts_with("value")))) %>%
select(cat, value)
# cat value
# 1 a 1
# 2 b 2
# 3 c 9
# 4 d 10
如何更简单的合并dataframe? 我有三个数据帧 (table_base / table_a / table_b)。我想按行组合它们以获得 'table_final' 的结果。下面是我的代码,它可以工作,但有点复杂。我怎样才能简化它? 实际上,除了 table_a 和 table_b.
,我还有更多的表要加入 library(dplyr)
table_base <- data.frame(cat=c("a","b","c","d"))
table_a <- data.frame(cat=c("a","b"),
value=c(1,2))
table_b <- data.frame(cat=c("a","c","d"),
value=c(7,9,10))
table_final <- table_base %>%
left_join(table_a,by='cat',fill=0) %>%
left_join(table_b,by='cat') %>%
mutate(value=if_else(!is.na(value.x),value.x,value.y)) %>%
select(cat,value)
您只需要从每个 table 中取出适当的行并绑定它们:
table_list <- list(table_a, table_b)
table_list %>%
map("cat") %>%
map2(c(list(NULL), accumulate(head(., -1), union)), setdiff) %>%
map2_dfr(table_list, ~filter(.y, cat %in% .x))
使用purrr::reduce合并多个dataframes,然后使用dplyr::coalesce 获取第一个非 na 值:
library(dplyr)
library(purrr)
list(table_base, table_a, table_b) %>%
reduce(left_join, by = "cat") %>%
mutate(value = coalesce(!!!select(., starts_with("value")))) %>%
select(cat, value)
# cat value
# 1 a 1
# 2 b 2
# 3 c 9
# 4 d 10