[英]how to dplyr::inner_join multi tbls or data.frames in R
In R, how can I inner_join
multiple tbls
or data.frame
s effectively? 在R中,我如何有效地
inner_join
多个tbls
或data.frame
?
For example: 例如:
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")
There are hundreds test
-like data.frames
that I want join. 我想要加入数百种
data.frames
test
的数据data.frames
。
You could collect the data frames in a list and use Reduce
: 您可以在列表中收集数据框并使用
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
You can use L <- mget(ls())
(with an optional pattern
arg to ls
) to get everything into a list. 您可以使用
L <- mget(ls())
(使用可选的pattern
arg到ls
)将所有内容放入列表中。
As @akrun mentioned in the comments, a plyr
alternative is: 正如@akrun在评论中提到的,一个
plyr
替代方案是:
library(plyr)
join_all(L, type='inner')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.