Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to 'binarize' some data in a dataframe in R

Tags:

r

I have a dataframe and I'd like to binarize every data point in the first 56 columns on the condition that if the value is greater than 0 then it gets set to 1, otherwise it is set to 0. Is there an easy way to do this?

like image 762
Hoser Avatar asked Mar 06 '13 04:03

Hoser


3 Answers

An approach using pmin and pmax. (not really recommended)

pmin(pmax(m[,2:5], 0),1)

But it allows be to add some benchmarking

ag <- function() ifelse(m[,2:5] > 0,1,0)
mn <- function()pmin(pmax(m[,2:5], 0),1)
am <- function()  (m[, 2:5] > 0) + 0
am2 <- function()  as.numeric((m[, 2:5] > 0))

library(microbenchmark)
microbenchmark(ag(),mn(), am(), am2())
## Unit: microseconds
##   expr    min     lq  median      uq     max neval
##   ag() 19.888 20.712 21.9375 22.6430  39.548   100
##   mn() 50.135 51.172 52.2530 53.1055 113.854   100
##   am()  3.076  3.406  4.1755  4.6030   7.912   100
##  am2()  2.623  2.989  3.4640  4.0135   6.995   100

@AnandaMahto's solutions are the clear winners, with the as.numeric approach even faster!

like image 40
mnel Avatar answered Nov 09 '22 03:11

mnel


Using the vectorized ifelse you can do :

   m[,1:56] <- ifelse(m[,1:56] > 0,1,0)

For example, we can test this in small matrix :

 m <- matrix(sample(c(-2,2),5*3,rep=T),ncol=5,nrow=3,byrow=T)
> m
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    2    2    2   -2
[2,]    2    2   -2    2   -2
[3,]    2    2    2    2    2
> m[,2:5] <- ifelse(m[,2:5] > 0,1,0)
> m
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    1    1    1    0
[2,]    2    1    0    1    0
[3,]    2    1    1    1    1
like image 148
agstudy Avatar answered Nov 09 '22 03:11

agstudy


You can make use of the fact that TRUE and FALSE equate to "1" and "0" and do:

set.seed(1)
mydf <- data.frame(matrix(rnorm(100), nrow = 10))
mydf[, 1:5] <- (mydf[, 1:5] > 0) + 0
mydf
#    X1 X2 X3 X4 X5         X6          X7           X8         X9        X10
# 1   0  1  1  1  0  0.3981059  2.40161776  0.475509529 -0.5686687 -0.5425200
# 2   1  1  1  0  0 -0.6120264 -0.03924000 -0.709946431 -0.1351786  1.2078678
# 3   0  0  1  1  1  0.3411197  0.68973936  0.610726353  1.1780870  1.1604026
# 4   1  0  0  0  1 -1.1293631  0.02800216 -0.934097632 -1.5235668  0.7002136
# 5   1  1  1  0  0  1.4330237 -0.74327321 -1.253633400  0.5939462  1.5868335
# 6   0  0  0  0  0  1.9803999  0.18879230  0.291446236  0.3329504  0.5584864
# 7   1  0  0  0  1 -0.3672215 -1.80495863 -0.443291873  1.0630998 -1.2765922
# 8   1  1  0  0  1 -1.0441346  1.46555486  0.001105352 -0.3041839 -0.5732654
# 9   1  1  0  1  0  0.5697196  0.15325334  0.074341324  0.3700188 -1.2246126
# 10  0  1  1  1  1 -0.1350546  2.17261167 -0.589520946  0.2670988 -0.4734006

The idea of +0 is simply to force the logical values of TRUE and FALSE to their numeric equivalent. If you are working on all the columns in a matrix and you used as.numeric(mydf > 0), you would have to re-convert the resulting vector to a matrix. However, in this case, this works perfectly well (as pointed out by @Dason).

mydf[, 1:5] <- as.numeric(mydf[, 1:5] > 0)
like image 40
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 09 '22 03:11

A5C1D2H2I1M1N2O1R2T1