[R] How to compare, match two columns from diferent dataframe and assign values from one datafram to the other
Wang, Ying
Ying.Wang at uth.tmc.edu
Fri Sep 17 23:02:42 CEST 2010
Dear R experts,
I'm new to R. It seems to be a simple question but I just can't find a way to do it. Please help me.
I have two data sets x and y as shown in the following. I want to compare the first two columns in x and y, find the matched ones and assign the relative value from column 2 of y to generate the third column of x. Any help wil be appreciated. Thanks.
> x
[,1] [,2]
[1,] "1" "0"
[2,] "2" "0"
[3,] "3" "0"
[4,] "4" "0"
[5,] "5" "0"
[6,] "6" "0"
[7,] "7" "0"
[8,] "8" "0"
[9,] "9" "0"
[10,] "10" "0"
> y
[,1] [,2]
[1,] "7" "a"
[2,] "4" "b"
[3,] "2" "c"
[4,] "8" "d"
[5,] "12" "e"
[6,] "15" "f"
The result I expect is:
x
[,1] [,2] [,3]
[1,] "1" "0" NA
[2,] "2" "0" "c"
[3,] "3" "0" NA
[4,] "4" "0" "b"
[5,] "5" "0" NA
[6,] "6" "0" NA
[7,] "7" "0" "a"
[8,] "8" "0" "d"
[9,] "9" "0" NA
[10,] "10" "0" NA
Best,
Ying
More information about the R-help
mailing list