如何在 R 上创建复杂条件变量
我有四个二元是/否变量。 我想创建一个按以下方式进行虚拟编码的组合变量:
- 0 如果参与者对所有 4 个变量说“是”(4/4)
- 1 如果参与者对 4 个变量中的 3 个说“是”(3/4) )
- 2 如果参与者对 4 个变量中的 2 个说“是”(2/4)
- 3 如果参与者对 4 个变量中的 1 个说“是”(1/4)
- 4 如果参与者对 0 说“是” 4 个变量中的 (0/4)
他们回答“是”的问题的顺序并不重要
我尝试创建这样的内容:
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")
但我肯定错过了一些东西
任何帮助将不胜感激!
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这?
This?