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
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
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(
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
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
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
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
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
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
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
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
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
12 matches
Mail list logo