Re: [R] inefficient ifelse() ?

2012-03-28 Thread Berend Hasselman
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

Re: [R] inefficient ifelse() ?

2012-03-28 Thread R. Michael Weylandt
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 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

Re: [R] inefficient ifelse() ?

2012-03-28 Thread Manta
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(

Re: [R] inefficient ifelse() ?

2011-03-02 Thread rex.dwyer
4 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [1] 3 4 6 12 > -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of ivo welch Sent: Tuesday, March 01, 2011 5:20 PM To: William Dunlap Cc: r-help Subject: Re: [R] inefficient ife

Re: [R] inefficient ifelse() ?

2011-03-01 Thread William Dunlap
f Of > ivo welch > Sent: Tuesday, March 01, 2011 2:20 PM > To: William Dunlap > Cc: r-help > Subject: Re: [R] inefficient ifelse() ? > > yikes. you are asking me too much. > > thanks everybody for the information. I learned something new. > > my suggestion would

Re: [R] inefficient ifelse() ?

2011-03-01 Thread ivo welch
From: r-help-boun...@r-project.org >> [mailto:r-help-boun...@r-project.org] On Behalf Of ivo welch >> Sent: Tuesday, March 01, 2011 12:36 PM >> To: Henrique Dallazuanna >> Cc: r-help >> Subject: Re: [R] inefficient ifelse() ? >> >> thanks, Henrique.  did

Re: [R] inefficient ifelse() ?

2011-03-01 Thread Thomas Lumley
On Wed, Mar 2, 2011 at 9:36 AM, ivo welch wrote: > thanks, Henrique.  did you mean > >    as.vector(t(mapply(function(x, f)f(x), split(t, ((t %% 2)==0)), > list(f, g   ? > > otherwise, you get a matrix. > > its a good solution, but unfortunately I don't think this can be used > to redefine ife

Re: [R] inefficient ifelse() ?

2011-03-01 Thread William Dunlap
e, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of ivo welch > Sent: Tuesday, March 01, 2011 12:36 PM > To: Henrique Dallazuanna > Cc: r-help > Subject: Re: [R] inefficient

Re: [R] inefficient ifelse() ?

2011-03-01 Thread Dennis Murphy
Hi: As far as I can see, the problem has to do with the way you wrote f and g: > f(2) f for 2 [1] 4 > g(5) g for 5 [1] 15 You output an unsaved character string with a numeric result, but the character string is not part of the return object since it is neither saved as a name or an attribute. I

Re: [R] inefficient ifelse() ?

2011-03-01 Thread ivo welch
thanks, Henrique. did you mean as.vector(t(mapply(function(x, f)f(x), split(t, ((t %% 2)==0)), list(f, g ? otherwise, you get a matrix. its a good solution, but unfortunately I don't think this can be used to redefine ifelse(cond,ift,iff) in a way that is transparent. the ift and iff

Re: [R] inefficient ifelse() ?

2011-03-01 Thread Henrique Dallazuanna
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 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

[R] inefficient ifelse() ?

2011-03-01 Thread ivo welch
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 p