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. If you save the output to an object, it returns the numeric outcome: > s <- f(2) > s [1] 4 > names(s) NULL > attributes(s) NULL However, > f <- function(x) 2^x > g <- function(x) 3^x > ifelse(t %% 2 == 0, f(t), g(t)) [1] 3.000000e+00 4.000000e+00 2.700000e+01 1.600000e+01 2.430000e+02 [6] 6.400000e+01 2.187000e+03 2.560000e+02 1.968300e+04 1.024000e+03 [11] 1.771470e+05 4.096000e+03 1.594323e+06 1.638400e+04 1.434891e+07 [16] 6.553600e+04 1.291402e+08 2.621440e+05 1.162261e+09 1.048576e+06 [21] 1.046035e+10 4.194304e+06 9.414318e+10 1.677722e+07 8.472886e+11 [26] 6.710886e+07 7.625597e+12 2.684355e+08 6.863038e+13 1.073742e+09 is the same as > ifelse(t %% 2 == 0, 2^t, 3^t) [1] 3.000000e+00 4.000000e+00 2.700000e+01 1.600000e+01 2.430000e+02 [6] 6.400000e+01 2.187000e+03 2.560000e+02 1.968300e+04 1.024000e+03 [11] 1.771470e+05 4.096000e+03 1.594323e+06 1.638400e+04 1.434891e+07 [16] 6.553600e+04 1.291402e+08 2.621440e+05 1.162261e+09 1.048576e+06 [21] 1.046035e+10 4.194304e+06 9.414318e+10 1.677722e+07 8.472886e+11 [26] 6.710886e+07 7.625597e+12 2.684355e+08 6.863038e+13 1.073742e+09 which is the vector result you apparently wanted. If you also want to output the character string with the value, you could either return a list or rewrite the print method of ifelse() to accommodate it. HTH, Dennis On Tue, Mar 1, 2011 at 12:36 PM, ivo welch <ivo.we...@gmail.com> 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 ifelse(cond,ift,iff) in a way that is transparent. the > ift and iff functions will always be evaluated before the function > call happens, even with lazy evaluation. :-( > > I still think that it makes sense to have a smarter vectorized %if% in > a vectorized language like R. just my 5 cents. > > /iaw > > ---- > Ivo Welch (ivo.we...@brown.edu, ivo.we...@gmail.com) > > > > > > On Tue, Mar 1, 2011 at 2:33 PM, Henrique Dallazuanna <www...@gmail.com> > wrote: > > 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 <ivo...@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 > > > > ______________________________________________ > 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. > [[alternative HTML version deleted]]
______________________________________________ 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.