[R] updating subset of data.frame

Rnewb oneoff1234 at yahoo.com
Mon Nov 30 05:06:54 CET 2009


i have a data frame and a numeric vector indexed as a subset of the rows in
the data.frame.  what command can i use to assign the values in the vector
to the appropriate rows of the data.frame?  here's my failed attempt.  what
i would want is data[1,'z'] == 2, data[5,'z'] == -4, data[8,'z'] == -5,
data[9,'z'] == 5, and for the other values of 'z' to remain 0. 

> data 
     x   y z 
1   -6   4 0 
2   -3  19 0 
3   15  40 0 
4    0  17 0 
5    1  12 0 
6   17  45 0 
7    4  19 0 
8  -13 -16 0 
9   -7   5 0 
10  -5  22 0 
> vec 
 1  5  8  9 
 2 -4 -5  5 
> data['z']=vec 
Error in `[<-.data.frame`(`*tmp*`, "z", value = c(2, -4, -5, 5)) : 
  replacement has 4 rows, data has 10 
> 

thanks, 
Rnewb
-- 
View this message in context: http://n4.nabble.com/updating-subset-of-data-frame-tp931051p931051.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list