How do I dichotomise efficiently
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
Try this:
b <- rep(0, length(y))
b[y != 0] <- 1
This is efficient because y and b are the same size and rep() is very fast/vectorized.
Edit:Here's another approach:
b <- ifelse(y == 0, 0, 1)
The ifelse() function is also vectorized.
b <- as.numeric(y!=0)
Use ifelse(). This is vectorized and (edit: somewhat) fast.
> 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
Edit 2: This approach is less fast than the as.numeric(y!=0) approach.
> t <- Sys.time(); b <- as.numeric(y!=0); Sys.time() - t # Rob's approach
Time difference of 0.0002379417 secs
> t <- Sys.time(); b <- ifelse(y==0, 0, 1); Sys.time() - t # Shane's 2nd and my approach
Time difference of 0.000428915 secs
> t <- Sys.time(); b = sapply( y, decider ); Sys.time() - t # James's approach
Time difference of 0.0004429817 sec
But to some, ifelse may be trivially more readable than the as.numeric approach.
Note the OP's version took 0.0004558563 to run.
b<-(y!=0)+0
> b
[1] 0 1 1 1 0 0 1 1 0 1 0 0
You have something that works. Are you worried about speed for some reason? Here's 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 )
精彩评论