Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Add data from row to a column with conditions

Tags:

r

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.

like image 267
tutuahha Avatar asked Jan 30 '26 09:01

tutuahha


1 Answers

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]
like image 154
akrun Avatar answered Feb 02 '26 01:02

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!