我正在尝试清理具有多个测试结果的数据表。我们从任何结果中都认为是积极的,表示该人是积极的。因此,我正在尝试创建一个代码,其中如果任何测试结果为阳性,则诊断为阳性。如果没有阳性,至少有一个阴性,则诊断为阴性(例如患者4、5和6)。我还想省略所有行(例如患者8)都没有结果(即NA)的行。谁能帮我这个?我尝试了此ifelse
语句,但是没有用
practice$Diagnosis = ifelse((testresult_1 == "1"|testresult_2 == "1"|testresult_3 == "1"), "Positive", "Negative")
Patient ID testresult_1 testresult_2 testresult_3 Diagnosis
1 Positive Negative Negative Positive
2 Positive Positive Negative Positive
3 Negative Negative Positive Positive
4 Negative Negative Negative Negative
5 Negative Negative NA Negative
6 Negative NA NA Negative
7 Positive NA NA Positive
8 NA NA NA NA
答案 0 :(得分:0)
您可以使用rowSums
:
cols <- grep('testresult', names(df))
practice$Diagnosis <- ifelse(rowSums(practice[cols] == 'Positive',
na.rm = TRUE) > 0, "Positive", "Negative")
#Turn all NA to 0
practice$Diagnosis[rowSums(!is.na(practice[cols])) == 0] <- NA
practice
# PatientID testresult_1 testresult_2 testresult_3 Diagnosis
#1 1 Positive Negative Negative Positive
#2 2 Positive Positive Negative Positive
#3 3 Negative Negative Positive Positive
#4 4 Negative Negative Negative Negative
#5 5 Negative Negative <NA> Negative
#6 6 Negative <NA> <NA> Negative
#7 7 Positive <NA> <NA> Positive
#8 8 <NA> <NA> <NA> <NA>