I have 5 variables, var1, var2 etc which are all coded as such:
Factor w/ 2 levels "no","yes": 2 1 1 2 1 2 1 1 1 1 ...
I would like to combine them into one. So far I have only used:
comb_drug <- with(dt1,interaction(var1, var2, var2, var4, var5))
which gives a variable with 32 levels. I would now like to create a variable with the following 3 levels:
- all 5 are yes
- any 4 are yes
- less than 4 are yes
What is the best way to do this ? Here is some example data:
var1 <- as.factor(c(2,2,1,2,2,1,2,1,2,2))
var2 <- as.factor(c(2,1,2,2,2,1,2,2,2,2))
var3 <- as.factor(c(2,2,1,2,2,2,2,2,1,2))
var4 <- as.factor(c(2,2,1,2,2,2,2,2,1,2))
var5 <- as.factor(c(2,2,2,1,2,1,2,1,1,2))
dt <- data.frame(var1,var2,var3,var4,var5)
for ( i in 1:5) {
levels(dt[,i]) <- c("no","yes")
}
var1 var2 var3 var4 var5
1 yes yes yes yes yes
2 yes no yes yes yes
3 no yes no no yes
4 yes yes yes yes no
5 yes yes yes yes yes
6 no no yes yes no
7 yes yes yes yes yes
8 no no yes yes no
9 yes yes no no no
10 yes yes yes yes yes
I would instead like
newvar
1 allyes
2 4yes
3 lessthan4yes
4 4yes
5 allyes
6 lessthan4yes
7 allyes
8 lessthan4yes
9 lessthan4yes
10 allyes