Skip to content

ifelse help?

5 messages · rkevinburton at charter.net, Charles C. Berry, Gustaf Rydevik +1 more

#
I am having a hard time understanding what is happening with ifelse.

Let me illustrate:

h <- numeric(5)
p <- 1:5
j <- floor(j)
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?" 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. But that isn't what is being returned.Something about this that I don't understand. Please help my ignorance.

Thank you.

Kevin
#
On Mon, 19 Jan 2009, rkevinburton at charter.net wrote:

            
And j is 0:4 + epsilon , where 0 <= epsilon < 1, evidently.
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
V1  V2
[1,] "1" "c"
[2,] "b" "4"
all args have different lengths and classes. But the result has those of 
the 'test' arg.
h has no attributes, therefore no 'dimension'

HTH,

Chuck
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
#
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. 

Kevin
---- "Charles C. Berry" <cberry at tajo.ucsd.edu> wrote:
#
On Mon, Jan 19, 2009 at 9:08 PM, <rkevinburton at charter.net> wrote:
ifelse returns values "row-by-row", so to speak. in this case, it will
return the vector:
c(x[j+2][1] , x[j+2][2] , x[j+2][3] , x[j+2][4] , x[j+2][5]).

If you instead write:

h<-numeric(5)
 j<-1:5
p <- 1:5
x<-1:1000
ifelse(h == 0,list(x[j+2]), 1:5)

,you get what you expected, since ifelse recycles the second argument
if necessary.

Regards,

Gustaf
#
r-help-bounces at r-project.org napsal dne 19.01.2009 21:08:59:
should
(h), then
dimensions
elements of h
5 the
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
of
each
if
values.
don't
http://www.R-project.org/posting-guide.html
Medicine
92093-0901
http://www.R-project.org/posting-guide.html