How do I dichotomise efficiently

performance, r

Solution

b <- as.numeric(y!=0)

Problem

Is there a more "R-minded" way to dichotomise efficiently? Thanks. ``` y<-c(0,3,2,1,0,0,2,5,0,1,0,0);b<-vector() for (k in 1:length(y)) { if (y[k] == 0) b[k] = 0 else b[k] = 1 } y;b ```

Original source