在R中,如何有效地inner_join多个tbls或data.frames?
例如:
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")
我想要加入的数百个测试类的data.frames.
解决方法
您可以在列表中收集数据帧,并使用Reduce:
L <- list(songs,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())(具有可选模式arg to ls)将所有内容都放入列表中. 在评论中提到的@akrun,plyr的选择是:
library(plyr) join_all(L,type='inner')