[R] ifelse help?
Charles C. Berry
cberry at tajo.ucsd.edu
Mon Jan 19 19:34:10 CET 2009
On Mon, 19 Jan 2009, rkevinburton at charter.net wrote:
> I am having a hard time understanding what is happening with ifelse.
> Let me illustrate:
> h <- numeric(5)
> p <- 1:5
> j <- floor(j)
And j is 0:4 + epsilon , where 0 <= epsilon < 1, evidently.
> x <- 1:1000
> ifelse(h == 0, x[j+2], 1:5)
>  2 3 4 5 6
> My question is, "shouldn't this be retruning 25 numbers?"
No. It should be
A vector of the same length and attributes (including class) as
test and data values from the values of yes or no
according to ?ifelse, and 'test' is what you have as 'h'
> z <- as.data.frame( diag(2 ) )
> ifelse(z == 0, letters , 1:5)
[1,] "1" "c"
[2,] "b" "4"
all args have different lengths and classes. But the result has those of
the 'test' arg.
> It seems that the ifelse should check 5 values of h for zero. For each
> of the 5 values I am thinking it should return an array of 5 (x[j+2] if
> h[index] == 0). Since the dimension of h is 5 that would mean 25 values.
h has no attributes, therefore no 'dimension'
> But that isn't what is being returned.Something about this that I don't
> understand. Please help my ignorance.
> Thank you.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help