如何在 R 中 dplyr::inner_join 多表或 data.frames
how to dplyr::inner_join multi tbls or data.frames in R
在 R 中,我如何 inner_join
多个 tbls
或 data.frame
有效?
例如:
devtools::install_github("rstudio/EDAWR")
library(EDAWR)
library(dplyr)
data(songs)
data(artists)
test <- songs
colnames(test) <- c("song2", "name")
inner_join(songs, artists,by="name") %>% inner_join(test,by="name")
有数百个 test
-like data.frames
我想加入。
您可以在列表中收集数据帧并使用 Reduce
:
L <- list(songs, artists, test)
Reduce(inner_join, L)
# name plays song song2
# 1 John guitar Across the Universe Across the Universe
# 2 John guitar Come Together Across the Universe
# 3 John guitar Across the Universe Come Together
# 4 John guitar Come Together Come Together
# 5 Paul bass Hello, Goodbye Hello, Goodbye
您可以使用 L <- mget(ls())
(带有一个可选的 pattern
arg 到 ls
)将所有内容放入列表中。
正如@akrun 在评论中提到的,plyr
替代方案是:
library(plyr)
join_all(L, type='inner')
在 R 中,我如何 inner_join
多个 tbls
或 data.frame
有效?
例如:
devtools::install_github("rstudio/EDAWR")
library(EDAWR)
library(dplyr)
data(songs)
data(artists)
test <- songs
colnames(test) <- c("song2", "name")
inner_join(songs, artists,by="name") %>% inner_join(test,by="name")
有数百个 test
-like data.frames
我想加入。
您可以在列表中收集数据帧并使用 Reduce
:
L <- list(songs, artists, test)
Reduce(inner_join, L)
# name plays song song2
# 1 John guitar Across the Universe Across the Universe
# 2 John guitar Come Together Across the Universe
# 3 John guitar Across the Universe Come Together
# 4 John guitar Come Together Come Together
# 5 Paul bass Hello, Goodbye Hello, Goodbye
您可以使用 L <- mget(ls())
(带有一个可选的 pattern
arg 到 ls
)将所有内容放入列表中。
正如@akrun 在评论中提到的,plyr
替代方案是:
library(plyr)
join_all(L, type='inner')