从r中的先前列中创建分类变量
我一直在尝试从以前的二进制变量创建一个新的分类变量。我想包括的所有二进制变量(咳嗽,发烧等)都有“ 1'或0”的值。 这是我一直在尝试的代码:
symptoms<-ifelse(cough==1,"cough",ifelse(fever==1,"fever",ifelse(diarrhea==1,"diarrhea",ifelse(dispnea==1,"dispnea",ifelse(neurologic==1,"neurologic",ifelse(otherSymp==1,"other symtomes", NA))))))
问题在于,输出仅为4个类别,而不是6类。
我知道这是一个基本问题,对不起,并提前感谢您。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如评论中所述,
case_when
是一个有用的选择到许多嵌套的
ifelse
调用:输出
在2022-07-11创建的(v2.0.1)
但是,这是一个非常基本的解决方案,假设您的症状不能超过一个(不合理) - 如果您有一个以上的症状,
症状< /代码>只会提及第一个。
As mentioned in the comments,
case_when
is a helpful alternative to many nestedifelse
calls:Output
Created on 2022-07-11 by the reprex package (v2.0.1)
However, this is a very basic solution, which assumes that you can't have more than one symptom (unreasonable) - if you have more than one,
symptom
will only mention the first.