[R] ifelse help?

Gustaf Rydevik gustaf.rydevik at gmail.com
Tue Jan 20 09:41:53 CET 2009


On Mon, Jan 19, 2009 at 9:08 PM,  <rkevinburton at charter.net> wrote:
> 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
>

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

-- 
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik




More information about the R-help mailing list