[R] ifelse()

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Tue Feb 10 23:09:40 CET 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of kayj
> Sent: Tuesday, February 10, 2009 1:44 PM
> To: r-help at r-project.org
> Subject: [R] ifelse()
> 
> 
> I have a problem with ifelse(), I do not understand how it works. 
> 
> > X<-c(2,2,1,1,0,0)
> > str(X)
>  num [1:6] 2 2 1 1 0 0
> > Y<-ifelse(X>0,1,0)
> > Y
> [1] 1 1 1 1 0 0
> >
> 
> Can some one explain what is going on, I do not understand 
> what ifelse is
> doing in this case. Can someone explain the output Y.
> 
> Thanks
> 

ifelse goes through your vector X, 1 element at a time, and if the element is greater than zero it returns a 1, otherwise, 0.  The resulting vector of 1s and 0s is assigned to Y

Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA  98504-5204
 
 




More information about the R-help mailing list