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 !

3
  • 2
    Weöcome to SO. Please provide: stackoverflow.com/help/minimal-reproducible-example Commented Feb 28, 2022 at 8:04
  • It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions. Commented Feb 28, 2022 at 8: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. Commented Feb 28, 2022 at 9:16

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.