[R] Matching values between 2 data.frame.

Christofer Bogaso bogaso.christofer at gmail.com
Sat May 20 20:23:07 CEST 2017


Hi again,

Let say I have below 2 data frames.

OriginalData = data.frame('Value1' = 1:12, 'Value2' = 11:22, 'AA1' =
c('AA4', 'AA3', 'AA4', 'AA1', 'AA2', 'AA1', 'AA6', 'AA6', 'AA3',
'AA3', 'AA4', 'AA3'), 'Value' = NA)

TargetValue = data.frame('AA' = c('AA1', 'AA2', 'AA3', 'AA4', 'AA5',
'AA6'), 'BB' = c('B', 'B', 'B', 'B', 'CC', 'CC'), 'Value' = c(5, 10,
25, 7, 35, 21))

OriginalData
TargetValue

Now I need to replace OriginalData's 'AA1' column with TargetValue's
'BB' column, based on matched values between 'AA1' & 'AA' columns of
OriginalData & TargetValue respectively. With this same law, I need to
update 'Value' column of  OriginalData with that of TargetValue.

As an example, after replacement by above rule, 1st row of
OriginalData should look like :

> OriginalData

   Value1 Value2 AA1 Value

1       1     11 B    7

Values of TargetValue's 'AA' column are unique i.e. no duplication

Previously I have implemented a 'for' loop to implement above, however
since both of my data.frames are quite big, it is taking long time to
execute. Is there any 'R' way to implement this quickly.

Appreciate for any pointer.

Thanks,



More information about the R-help mailing list