[R] Merging and Updating Data Frames with Unequal Size

Lorenzo Isella lorenzo.isella at gmail.com
Fri Feb 8 21:57:06 CET 2013


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



More information about the R-help mailing list