[R] multiple uses ifelse function

S Ellison S.Ellison at LGCGroup.com
Tue Oct 11 18:01:36 CEST 2016



> -----Original Message-----
> testseq<-seq(1:20)
> testchange<-ifelse(testseq<=4,'x',testseq)
> testchange<-c(ifelse(testseq<=4,'x',testseq),ifelse(testseq>=5,'y',testseq))
> 
> The last instruction causes the vector 'testchange' to change dimensions,

Of course it does. ifelse(test, yes, no) returns a vector of length length(test). Your last line concatenates two of them, so you'll get a vector of length 40.

You might also want to note that you are replacing numbers with character strings, so 
ifelse(testseq<=4,'x',testseq)

will return four 'x's and then - because R has to coerce everything to a single type - character representations of numbers 5:20. That will not then respond well to subsequent numeric comparisons ...

S Ellison


*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list