Reputation: 33
I've got two data frames,
df1
number x_1 y_1
1 a a
2 a b
3 b b
4 c b
5 c c
df2
number_1 x_1 y_1 number_2 x_2 y_2
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
I want to add data into the columns x_1, y_1, x_2, y_2 based on whether number_1 and number_2 match the numbers in the number column in df1. So in the first two rows of df2, I should have:
df2
number_1 x_1 y_1 number_2 x_2 y_2
1 a a 2 a b
1 a a 3 b b
I tried something like this, but it doesn't work and it takes extremely long with an extremely huge dataset:
for(i in 1:nrow(df1))
for(j in 1:nrow(df2))
if(df2$number_1[j] == df1$number[i])
df2$x_1[j] = df1$x_1[i]
df2$y_1[j] = df1$y_1[i]
if(df2$number_2[j] == df1$number_1[i])
df2$x_1[j] = df1$x_1[i]
df2$y_1[j] = df$y_1[i]
I'm a beginner so I'll really appreciate if you could explain the steps you propose too. Thank you very much.
Upvotes: 3
Views: 77
Reputation: 886938
Try either merge
or left_join
from dplyr
merge(df1,
merge(df1,df2[,c(1,4)], by.x='number', by.y='number_1'),
by.x='number', by.y='number_2')
Or
library(dplyr)
left_join(
left_join(df2[c(1,4)], df1, by=c('number_1'='number')) ,
df1, by=c('number_2'='number'))
Or as @David Arenburg mentioned in the comments, match
can be used here.
df2[c("x_1", "y_1")] <- df1[match(df2$number_1, df1$number), 2:3]
df2[c("x_2", "y_2")] <- df1[match(df2$number_2, df1$number), 2:3]
Upvotes: 1