How to effectively dichotomize

Is there a more "R-minded" way to effectively dichotomize? 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
+3
source share
5 answers
b <- as.numeric(y!=0)
+5
source

Try the following:

b <- rep(0, length(y))
b[y != 0] <- 1

This is effective because y and b are the same size, and rep () is very fast / vectorized.

Edit: Here is a different approach:

b <- ifelse(y == 0, 0, 1) 

The ifelse () function is also vectorized.

+6
source

ifelse(). (: ) .

> y <- c(0,3,2,1,0,0,2,5,0,1,0,0)
> b <- ifelse(y == 0, 0, 1)
 [1] 0 1 1 1 0 0 1 1 0 1 0 0

2: , as.numeric(y!= 0).

> t <- Sys.time(); b <- as.numeric(y!=0); Sys.time() - t # Rob approach
Time difference of 0.0002379417 secs
> t <- Sys.time(); b <- ifelse(y==0, 0, 1); Sys.time() - t # Shane 2nd and my approach
Time difference of 0.000428915 secs
> t <- Sys.time(); b = sapply( y, decider ); Sys.time() - t # James approach
Time difference of 0.0004429817 sec

ifelse , as.numeric.

, OP 0.0004558563 .

+2
 b<-(y!=0)+0

> b
 [1] 0 1 1 1 0 0 1 1 0 1 0 0
+1

You have something that works. Are you worried about speed for some reason? Here is an alternative:

y<-c(0,3,2,1,0,0,2,5,0,1,0,0)

decider = function( x ) {
   if ( x == 0 ) {
      return(0)
   }

   return(1)
}

b = sapply( y, decider )
0
source

Source: https://habr.com/ru/post/1719482/


All Articles