ifelse help?
r-help-bounces at r-project.org napsal dne 19.01.2009 21:08:59:
Sorry I didn't give the proper initialization of j. But you are right j
should
also be an array of 5. So x[j + 5] would return 5 values. So if the array returned from 'ifelse' is the same dimention as test
(h), then
are all the values of h being tested? So since h as you say has no
dimensions
is the test only testing h[1]? Again it seems that if all of the
elements of h
are tested (there are 5 elements) and each element produces an array of
5 the
resulting array should be 25.
No
A vector of the same length and attributes (including class) as
test and data values from the values of yes or no
Let make an experiment h<-numeric(5) h[2]<-1 h[5]<-1 so as 2 conditions are not met set.seed(111) j<-abs(rnorm(5)*50) j <- floor(j) ifelse(h == 0, x[j+2], 1:5) [1] 13 2 17 117 5 Results in vector of 5 numbers as h is vector of 5 numbers Items 1,3 and 4 are taken from corresponding positions in x[j+2] items 2 and 5 from corresponding positions of 1:5 compare h<-numeric(5) ifelse(h == 0, x[j+2], 1:5) [1] 13 18 17 117 10 If you want to have all elements (j+2) from x you can try use list lll<-list(a=1:5, b=letters, c=32:48, d=rnorm(5), e=42) ifelse(h == 0, lll, 1:5) Regards Petr
Kevin ---- "Charles C. Berry" <cberry at tajo.ucsd.edu> wrote:
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) [1] 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'
Consider
z <- as.data.frame( diag(2 ) ) ifelse(z == 0, letters , 1:5)
V1 V2 [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' HTH, Chuck
But that isn't what is being returned.Something about this that I
don't
understand. Please help my ignorance. Thank you. Kevin
______________________________________________ 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.
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
______________________________________________ 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.