'binary variable does not correspond with category numbers
I'm trying to create a new binary variable based on several categorical variables. I have tried multiple ways to do this including base or if else commands, and mutate case when from dyplyr. When I make the new variable, the number does not add up to how many were in the categories in the original variables.
data<- c("ket"(1,0,0,0,1,0)
c("weed"(0,1,1,1,0,0)
c("speed"(1,0,0,1,0,0)
c("meth"(0,0,0,1,0,0)
data<-data%>%
mutate(druguse = case_when(
(weed==1 | ket==1 | meth==1 | speed==1) ~1,
(TRUE ~0)))
the new variable should add up to how many answered one in each category, but the number in my new variable is a lot lower.
thank you!
Solution 1:[1]
You can avoid having to write out an explicit case_when here, by taking the sign of the sum of each row. This will be 0 if the whole row is zero, and one otherwise.
data %>% mutate(druguse = sign(rowSums(.)))
#> ket weed speed meth druguse
#> 1 1 0 1 0 1
#> 2 0 1 0 0 1
#> 3 0 1 0 0 1
#> 4 0 1 1 1 1
#> 5 1 0 0 0 1
#> 6 0 0 0 0 0
Data
data <- structure(list(ket = c(1, 0, 0, 0, 1, 0), weed = c(0, 1, 1, 1,
0, 0), speed = c(1, 0, 0, 1, 0, 0), meth = c(0, 0, 0, 1, 0, 0
)), class = "data.frame", row.names = c(NA, -6L))
data
#> ket weed speed meth
#> 1 1 0 1 0
#> 2 0 1 0 0
#> 3 0 1 0 0
#> 4 0 1 1 1
#> 5 1 0 0 0
#> 6 0 0 0 0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
