[R] Performing operations only on selected data

Marcel Curlin cemarcel at u.washington.edu
Mon Nov 26 03:33:56 CET 2012


Thank you, this works very well. My only remaining question about this is
about how ifelse is working; I understand the basic syntax (df$condition2
gets assigned the value *runif(nrow(df1[df1$condition1<=1,]),0,1)* or the
value *df$condition1* depending on whether or not df$condition1 meets the
criterion "<=1".

As I understand it, "runif(nrow(df1[df1$condition1<=1,]),0,1)" is a vector
of random values with vector length equal to the number of rows meeting
"df$condition1<=1" and df$condition1 is just my column of condition1 values.
So the command seems to be going down row by row and assigning condition2
values from one of two vectors in an "interleaved" way. 

So my question is, how does R keep track of which item in each of the
vectors to assign to condition2? For example, if the first 4 entries of
condition1 are 1, 3, 4, 1,  how does R know to use the *first* entry of
vector runif(nrow(df1[df1$condition1<=1,]),0,1) then the *second* and
*third* values of vector df$condition1, then the *second* value of vector
runif(nrow(df1[df1$condition1<=1,]),0,1)?



--
View this message in context: http://r.789695.n4.nabble.com/Performing-operations-only-on-selected-data-tp4650646p4650803.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list