我想为个人创建一个感染状态列。在我的数据集中,Malaria_RDT是一种疟疾诊断测试,CCA_test是一种血吸虫病诊断测试。
我希望我的新感染状态变量是:只感染疟疾,只感染血吸虫病,既不感染疟疾也不感染血吸虫,或者同时感染疟疾和血吸虫病。
我的数据集名为co.inf
if Malaria_RDT==0 and CCA_test==0, then inf.st = 0;
if Malaria_RDT >0 and CCA_test==0, then inf.st = 1;
if Malaria_RDT==0 and CCA_test >0, then inf.st = 2; and
if Malaria_RDT >=1 and CCA_test >=1, then inf.st = 3发布于 2020-01-30 22:28:53
这是dplyr::case_when()的一个用例:
co.inf %>%
mutate(inf.st = case_when(Malariad_RDT == 0 & CC_test == 0 ~ 0,
Malariad_RDT > 0 & CC_test == 0 ~ 1,
Malariad_RDT == 0 & CC_test > 0 ~ 0,
Malariad_RDT >= 1 & CC_test >= 1 ~ 0
))发布于 2020-01-30 20:57:51
这应该是可行的:
co.inf$inf.st <- if (co.inf$Malaria_RDT == 0 & co.inf$CCA_test == 0) {
0
} else if (co.inf$Malaria_RDT > 0 & co.inf$CCA_test == 0) {
1
} else if (co.inf$Malaria_RDT == 0 & co.inf$CCA_test > 0) {
2
} else {
3
}https://stackoverflow.com/questions/59986397
复制相似问题