Skip to content
Prev 167565 / 398502 Next

ifelse help?

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