[R] Using cbind to merge different variables

arun smartpink111 at yahoo.com
Fri Dec 20 16:58:56 CET 2013


HI,

May be this helps:
set.seed(45)
 dat1 <- as.data.frame(matrix(sample(0:1,100*5,replace=TRUE),ncol=5))

dat1$Newvar <- 1*(!!rowSums(dat1))

A.K.


Hello. 

I have a problem combining a number of variables. I have five 
columns with binary variables with the values 0 and 1. I would like to 
combine them into just one binary variable with 1 whenever just one of 
the other has value one, and 0 if none of them have value one. 

How can I do that? 

I tried to use cbind function, but for some reason I get results
 1 even though all of the varuables included = 0 and for some rows I get
 2. I tried changing the deparse.level but that doesn't seem to be the 
problem. 

I hope someone can help. 

Kind regards



More information about the R-help mailing list