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
Using cbind to merge different variables
2 messages · arun, PIKAL Petr
2 days later
Hi Another option is dat1$Newvar <- 1*(rowSums(dat1)>0) Regards Petr
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of arun Sent: Friday, December 20, 2013 4:59 PM To: R help Subject: Re: [R] Using cbind to merge different variables 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
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.