简体   繁体   English

R在其他列的基础上新增一列

[英]Add a new column in R based on other columns

I'm trying to clean up a data sheet that has multiple test results.我正在尝试清理具有多个测试结果的数据表。 We're considering a positive from any of the results to mean the person is positive.我们正在考虑任何结果中的阳性表示该人是阳性的。 So I'm trying to create a code where if any of the test results are positive, then the diagnosis is positive.所以我正在尝试创建一个代码,如果任何测试结果为阳性,则诊断为阳性。 If there are no positives and at least one negative, then the diagnosis is negative (ex. Patients 4, 5 and 6).如果没有阳性且至少有一个阴性,则诊断为阴性(例如患者 4、5 和 6)。 I also want to omit rows where there are no results (ie. NA) for all of the rows (ex. Patient 8).我还想省略所有行(例如患者 8)没有结果的行(即 NA)。 Can anyone help me with this?谁能帮我这个? I tried this ifelse statement, but it's not working我试过这个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

You can use rowSums :您可以使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM