dear R experts--- t <- 1:30 f <- function(t) { cat("f for", t, "\n"); return(2*t) } g <- function(t) { cat("g for", t, "\n"); return(3*t) } s <- ifelse( t%%2==0, g(t), f(t)) shows that the ifelse function actually evaluates both f() and g() for all values first, and presumably then just picks left or right results based on t%%2. uggh... wouldn't it make more sense to evaluate only the relevant parts of each vector and then reassemble them? /iaw ---- Ivo Welch
Try this: mapply(function(x, f)f(x), split(t, t %% 2), list(g, f)) On Tue, Mar 1, 2011 at 4:19 PM, ivo welch <ivowel@gmail.com> wrote:> dear R experts--- > > t <- 1:30 > f <- function(t) { cat("f for", t, "\n"); return(2*t) } > g <- function(t) { cat("g for", t, "\n"); return(3*t) } > s <- ifelse( t%%2==0, g(t), f(t)) > > shows that the ifelse function actually evaluates both f() and g() for > all values first, and presumably then just picks left or right results > based on t%%2. uggh... wouldn't it make more sense to evaluate only > the relevant parts of each vector and then reassemble them? > > /iaw > ---- > Ivo Welch > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Just use a regular if statement: X <- if(b == "YY") a else substr(a, 1,3) Michael On Wed, Mar 28, 2012 at 11:19 AM, Manta <mantino84 at libero.it> wrote:> I have a similar problem. I have a dataset and an element. If the element is > equal to "YY", I want to take the first column of the dataset, otherwise I > want to take the second column. The following does not work, as it only > evaluates the first element. Any idea? > > a=c("AAAXXX","BBBXXX") > a=merge(a,c("AAA","BBB")) > b="YY" >> ifelse(b=="YY",a,substr(a,1,3)) > [1] "AAAXXX" > > > > -- > View this message in context: http://r.789695.n4.nabble.com/inefficient-ifelse-tp3330423p4512579.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.
On 28-03-2012, at 17:19, Manta wrote:> I have a similar problem. I have a dataset and an element. If the element is > equal to "YY", I want to take the first column of the dataset, otherwise I > want to take the second column. The following does not work, as it only > evaluates the first element. Any idea? >yes. See the description of ifelse.> a=c("AAAXXX","BBBXXX") > a=merge(a,c("AAA","BBB")) > b="YY" >> ifelse(b=="YY",a,substr(a,1,3)) > [1] "AAAXXX"If you want the first column of a when b=="YY" why are you doing a and not a[,1]? If you want the second column of a when !(b=="YY") why are you doing substr(a,1,3) and not a[,2]? You are getting a scalar as result because of the way the ifelse function works. To get what you want you could do: a[,if(b=="YY") 1 else 2] or acolumn <- if( b == "YY" ) 1 else 2 a[,acolumn] or a[,2-(b=="YY")] Berend