[R] Merging and Updating Data Frames with Unequal Size

arun smartpink111 at yahoo.com
Fri Feb 8 22:08:27 CET 2013


Hi,
Try:
 R[,2:4]<-F[,2:4][match(R$x,F$x),]
 R$p<- unique(F$p)
    
R
    x y z  w k  p
1  -1 1 3  8 9 18
2  -2 2 4  9 9 18
3  -3 3 5 10 9 18
4  -1 1 3  8 9 18
5  -2 2 4  9 9 18
6  -3 3 5 10 9 18
7  -1 1 3  8 9 18
8  -2 2 4  9 9 18
9  -3 3 5 10 9 18
10 -1 1 3  8 9 18
11 -2 2 4  9 9 18
12 -3 3 5 10 9 18
13 -1 1 3  8 9 18
14 -2 2 4  9 9 18
15 -3 3 5 10 9 18
A.K.




----- Original Message -----
From: Lorenzo Isella <lorenzo.isella at gmail.com>
To: "r-help at stat.math.ethz.ch" <r-help at stat.math.ethz.ch>
Cc: 
Sent: Friday, February 8, 2013 3:57 PM
Subject: [R] Merging and Updating Data Frames with Unequal Size

Dear All,
I am trying to merge 2 dataframes of with different sizes.
Let's say that one dataframe R contains some raw data and another data frame F contains the information to fix R.
For instance

F <- data.frame(cbind(x=-seq(10),
                      y=1:10,
                      z=3:12,
                      w=8:17,
                      p=18
                      ))



R <- data.frame(cbind(x=rep(-seq(3),5), y=18:32,z=100:86,w=4,k=9))

I would like to do the following: for every value of R$x (i.e. 1,2,3 in this case), I will look up the same value in
F$x and use the info on that row of F to update the values of R$y, R$z,R$w (i.e. the raws which have the same names in both data frames), whereas I will simply append (as a new column) the values of F$p to the new dataframe.

The resulting data frame should look like this

x y z  w k  p
-1 1 3  8 9 18
-2 2 4  9 9 18
-3 3 5 10 9 18
-1 1 3  8 9 18
-2 2 4  9 9 18
-3 3 5 10 9 18
-1 1 3  8 9 18
-2 2 4  9 9 18
-3 3 5 10 9 18
-1 1 3  8 9 18
-2 2 4  9 9 18
-3 3 5 10 9 18
-1 1 3  8 9 18
-2 2 4  9 9 18
-3 3 5 10 9 18

(it took longer to explain it than to do it!).

I can do this with a double loop, but I think that playing with the merge command should do the trick, although so far I have been unsuccessful.
Any suggestion is welcome.

Lorenzo

______________________________________________
R-help at 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.




More information about the R-help mailing list