[R] Matching values between 2 data.frame.
David Winsemius
dwinsemius at comcast.net
Sat May 20 20:53:45 CEST 2017
> On May 20, 2017, at 11:23 AM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:
>
> 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.
It's going to have a greater chance of delivering the desired result if you convert the factor columns into character.
>
> Thanks,
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list