【发布时间】:2015-08-18 23:20:12
【问题描述】:
我正在尝试循环合并多个列上的两个数据帧,但我遇到了代码问题,并且无法在 SO 上找到任何答案。以下是一些示例数据框:
box <- c(5,7,2)
year <- c(1999,1999,1999)
rep5 <- c(5,5,5)
rep7 <- c(7,7,7)
rep2 <- c(2,2,2)
df1 <- data.frame(box,year,rep5,rep7,rep2)
box1 <- c(5,5,5,5,7,7,7,7,2,2,2,2)
box2 <- c(5,7,2,5,5,7,2,4,5,7,2,9)
year2 <- c(1999,1999,1999,2000,1999,1999,1999,1999,1999,1999,1999,1999)
distance <- c(0,100,200,0,100,0,300,200,200,300,0,300)
df2 <- data.frame(box1,box2,year2,distance)
df1
box year rep5 rep7 rep2
1 5 1999 5 7 2
2 7 1999 5 7 2
3 2 1999 5 7 2
df2
box1 box2 year2 distance
1 5 5 1999 0
2 5 7 1999 100
3 5 2 1999 200
4 5 5 2000 0
5 7 5 1999 100
6 7 7 1999 0
7 7 2 1999 300
8 7 4 1999 200
9 2 5 1999 200
10 2 7 1999 300
11 2 2 1999 0
12 2 9 1999 300
我要做的是获取从 df2 到 df1 的距离信息,其中 df1 year 与 df2 year 匹配,df1 box 与 df2 box1 匹配,df1 rep[i] 与 df2 box2 匹配。我可以为单个 df1 rep[i] 列执行此操作,如下所示:
merge(df1, df2, by.x=c("box", "rep5", "year"), by.y=c("box1", "box2", "year2"), all.x = TRUE)
这给出了所需的输出:
box rep5 year rep7 rep2 distance
1 2 5 1999 7 2 200
2 5 5 1999 7 2 0
3 7 5 1999 7 2 100
但是,为了单独为每个 rep[i] 列保存此操作(我在真实数据集中有很多这些列),我希望能够遍历这些列。这是我尝试这样做的代码:
reps <- c(df1$rep7, df1$rep2)
df3 <- for (i in reps) {merge(df1, df2, by.x=c("box", i, "year"), by.y=c("box1", "box2", "year2"), all.x = TRUE)}
df3
当我运行该代码时,我收到错误“fix.by(by.x, x) 中的错误:'by' 必须指定唯一有效的列。”我也尝试定义
reps <- c("rep7", "rep2")
当我使用该定义运行相同的代码时,我得到 df3 为 NULL 的结果。
我想要的输出(为清楚起见重命名了距离列)是:
box year rep5 rep7 rep2 dist5 dist7 dist2
1 2 1999 5 7 2 200 300 0
2 5 1999 5 7 2 0 100 200
3 7 1999 5 7 2 100 0 300
我做错了什么?非常感谢您能给我的任何帮助!
【问题讨论】: