简体   繁体   English

在 R 中合并多对数据帧

[英]Merging multiple pairs of data-frame in R

I am looking to merge multiple pairs of data frames.我正在寻找合并多对数据框。 For example I have following data frames Con_2014, Con_2015, Con_2016..... and Income_2014, Income_2015, Income_2016.例如,我有以下数据框 Con_2014、Con_2015、Con_2016 ..... 和 Income_2014、Income_2015、Income_2016。 So I want to merge data for each year like the following but at once.所以我想合并每年的数据,如下所示。

Con_Income_2014 <- merge(Income_2014,Con_2014)
Con_Income_2015 <- merge(Income_2015,Con_2015)
Con_Income_2016 <- merge(Income_2016,Cons_2016)`

I tried the following way but it does not work.我尝试了以下方法,但它不起作用。

for (i in 1: length(years)){
  assign(paste("Cons_Income", as.character(years[i]),sep =""),merge(paste("Income_",as.character(years[i]),sep=""),paste("Cons_",as.character(years[i]),sep="")))}

It runs but it give me just name of each pairs.它运行,但它只给我每对的名称。 Its merging strings(the name of the file but not the data)它的合并字符串(文件名但不是数据)

It's not the prettiest thing, but it works:这不是最漂亮的东西,但它有效:

Income_2014 = data.frame(id = 1:5, x = runif(5,-1,1))
Income_2015 = data.frame(id = 6:10, x = runif(5,-1,1))
Income_2016 = data.frame(id = 11:15, x = runif(5,-1,1))
Cons_2014 = data.frame(id = 1:5, y = runif(5,2,3))
Cons_2015 = data.frame(id = 6:10, y = runif(5,2,3))
Cons_2016 = data.frame(id = 11:15, y = runif(5,2,3))
years <- 2014:2016
for (i in 1: length(years)){
  assign(paste("Cons_Income", as.character(years[i]),sep =""),
    merge(eval(parse(text=paste("Income_",as.character(years[i]),sep=""))),                  
          eval(parse(text=paste("Cons_",as.character(years[i]),sep="")))))
}

*Observation. *观察。 I believe the last object should be con_... and not cons_... (a typo)我相信最后一个 object 应该是 con_... 而不是 cons_... (错字)

We do not neet for loops.我们不需要 for 循环。 If all years are associated with a pair of dataframes, and the naming is consistent, we can use mget(ls(pattern =....)) to create two lists of dataframes, then do the pairwise merge with mapply :如果所有年份都与一对数据框相关联,并且命名一致,我们可以使用mget(ls(pattern =....))创建两个数据框列表,然后使用mapply进行成对合并:

mapply(merge,
       mget(ls(pattern = "Income_20\\d{2}")),
       mget(ls(pattern = "Con_20\\d{2}")),
       SIMPLIFY = FALSE) %>%
    setNames(paste0('income_con_', 2014:2016))

Data created by @DaveArmstrong: @DaveArmstrong 创建的数据:

set.seed(1)

Income_2014 = data.frame(id = 1:5, x = runif(5,-1,1))
Income_2015 = data.frame(id = 6:10, x = runif(5,-1,1))
Income_2016 = data.frame(id = 11:15, x = runif(5,-1,1))
Cons_2014 = data.frame(id = 1:5, y = runif(5,2,3))
Cons_2015 = data.frame(id = 6:10, y = runif(5,2,3))
Cons_2016 = data.frame(id = 11:15, y = runif(5,2,3))

Output: Output:

$income_con_2014
  id          x        y
1  1 -0.4689827 2.497699
2  2 -0.2557522 2.717619
3  3  0.1457067 2.991906
4  4  0.8164156 2.380035
5  5 -0.5966361 2.777445

$income_con_2015
  id          x        y
1  6  0.7967794 2.934705
2  7  0.8893505 2.212143
3  8  0.3215956 2.651674
4  9  0.2582281 2.125555
5 10 -0.8764275 2.267221

$income_con_2016
  id          x        y
1 11 -0.5880509 2.386114
2 12 -0.6468865 2.013390
3 13  0.3740457 2.382388
4 14 -0.2317926 2.869691
5 15  0.5396828 2.340349

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM