[R] Performing operations only on selected data

arun smartpink111 at yahoo.com
Mon Nov 26 14:35:40 CET 2012


HI Marcel,

Sorry, that was a mistake.
I guess this should be the one:
 df$condition2<-ifelse(df$condition1<=1,runif(length(df$condition1),0,1),df$condition1)
df$condition2 

#[1] 0.5207776 0.5227388 0.5196520 0.6552820 2.0000000 2.0000000 2.0000000
 #[8] 2.0000000 3.0000000 3.0000000 3.0000000 3.0000000 4.0000000 4.0000000
#[15] 4.0000000 4.0000000
A.K.





----- Original Message -----
From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
To: Marcel Curlin <cemarcel at u.washington.edu>; r-help at r-project.org
Cc: 
Sent: Monday, November 26, 2012 1:23 AM
Subject: Re: [R] Performing operations only on selected data

a) Please read the posting guide. This mailing list is not Nabble, and you are requested to post in plain text and include context from previous messages in the thread.

b) arun's solution is wrong in two respects: it fails to add condition1 to the random numbers, and it feeds differently-sized vectors as values to the ifelse function.

The length of each argument to ifelse should be as long as the desired result.

BTW: "df" is a function in the base package, always present. It is not a good idea to use that name for your own purposes, as eventually you may want to use that base function or at least not confuse others.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Marcel Curlin <cemarcel at u.washington.edu> wrote:

>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.
>
>______________________________________________
>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.

______________________________________________
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