Wang, Ying
2010-Sep-17 21:02 UTC
[R] How to compare, match two columns from diferent dataframe and assign values from one datafram to the other
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
Dennis Murphy
2010-Sep-18 00:17 UTC
[R] How to compare, match two columns from diferent dataframe and assign values from one datafram to the other
Hi: df <- data.frame(x = 1:10, y = rep(0, 10)) dg <- data.frame(x = c(7, 4, 2, 8, 12, 15), y = letters[1:6]) merge(df, dg, by = 'x', all.x = TRUE) x y.x y.y 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> HTH, Dennis On Fri, Sep 17, 2010 at 2:02 PM, Wang, Ying <Ying.Wang@uth.tmc.edu> wrote:> 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 > > ______________________________________________ > R-help@r-project.org mailing list > 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. >[[alternative HTML version deleted]]
Maybe Matching Threads
- How to compare linear models with intercept and those without intercept using minimizing adjs R^2 strategy
- conditional value assignment
- iscsi conn error: Xen related?
- Looking for a quick way to combine rows in a matrix
- Interacting with dendrogram plots, locator() or click()