0

I have four binary yes/no variables. I want to create a combined variable that is dummy coded in the following manner:

  • 0 if participants say "Yes" to all 4 Variables (4/4)
  • 1 if participants say "Yes" to 3 out of the 4 Variables (3/4)
  • 2 if participants say "Yes to 2 out of the 4 Variables (2/4)
  • 3 if participants say "Yes" to 1 out of the 4 Variables (1/4)
  • 4 if participants say "Yes" to 0 out of the 4 Variables (0/4)

The order of which questions they've said yes to does not matter.

I've tried to create something like this:

VariableCodingFor1/4 <- ifelse(Var1 =="Yes" & Var2 == "No" & Var3 =="No", Var4 =="No") |
ifelse(Var1 =="No" & Var2 == "Yes" & Var3 =="No", Var4 =="No") |
ifelse(Var1 =="No" & Var2 == "No" & Var3 =="Yes", Var4 =="No") |
ifelse(Var1 =="No" & Var2 == "No" & Var3 =="No", Var4 =="Yes") 

But I'm definitely missing something

Any help would be greatly appreciated !

Heshani
  • 13
  • 3
  • 2
    Weöcome to SO. Please provide: https://stackoverflow.com/help/minimal-reproducible-example – deschen Feb 28 '22 at 08:04
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 28 '22 at 08:07
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 28 '22 at 09:16

1 Answers1

1

This?

> set.seed(123)
> df=data.frame(matrix(sample(c("Yes","No"),24,T),6,4))
> rowSums(df=="Yes")
[1] 2 3 3 2 2 2
> 4-rowSums(df=="Yes")
[1] 2 1 1 2 2 2
user2974951
  • 9,535
  • 1
  • 17
  • 24