2016-09-19 51 views
0

假设我有2个dataframes。我想从每个数据帧中只选择一行,让我们说行业c3,并将它们放在另一个data.frame,df3中。采取从n个dataframes一行,并创建所采取的行一个新的数据帧,R

df1 = data.frame(Industries = c("c1", "c2", "c3", "c4", "c5"), Exports = c(100, 40, 30, 10, 50), Imports =c(90,50,25,15,50)) 
df2 = data.frame(Industries = c("c1", "c2", "c3", "c4", "c5"), Exports = c(20, 90, 10, 30, 60), Imports =c(40,30,25,55,40)) 

df1 
     Industries Exports Imports 
    1   c1  100  90 
    2   c2  40  50 
    3   c3  30  25 
    4   c4  10  15 
    5   c5  50  50 
df2 
     Industries Exports Imports 
    1   c1  20  40 
    2   c2  90  30 
    3   c3  10  25 
    4   c4  30  55 
    5   c5  60  40 

在我真实的数据我有16个不同的dataframes,所以如果可能的代码应该允许把16行从另一个16个dataframes,将创建。

+0

'做行.call(rbind,lapply(list(df1,df2),subset,Industries ==“c3”))'? – Psidom

回答

0

假设您的列总是相同的,你可以用dplyr和purrr包以下内容:

library(purrr) 
library(dplyr) 

df1 = data.frame(Industries = c("c1", "c2", "c3", "c4", "c5"), Exports = c(100, 40, 30, 10, 50), Imports =c(90,50,25,15,50)) 
df2 = data.frame(Industries = c("c1", "c2", "c3", "c4", "c5"), Exports = c(20, 90, 10, 30, 60), Imports =c(40,30,25,55,40)) 

list(df1, df2) %>% map_df(~ filter(., Industries == "c3")) 
#> Industries Exports Imports 
#> 1   c3  30  25 
#> 2   c3  10  25 
  • list(df1, df2)是在每个数据帧组合成一个列表
  • map_df迭代,看好(如果可能)将结果作为数据框返回。
  • ~ filter(., Industries == "c3")是每个数据帧上运行的功能,这仅返回行,其中Industries == "c3"
0

我们可以使用data.table。将在list数据集与mgetpasterbind数据集在listrbindlist),设置“key”作为“工业”和子集有“C3”列

library(data.table) 
setkey(rbindlist(mget(paste0("df", 1:2))), "Industries")["c3"] 
# Industries Exports Imports 
#1:   c3  30  25 
#2:   c3  10  25 
相关问题