根据 r 中多个其他列的条件更新一列中的值

Update value in one column based on criteria from multiple other columns in r

R 版本 3.3.2(2016-10-31)

R Studio 版本 1.0.136

平台:X86_64-apple-darwin13.4.0(64 位)

数据框

Subject Drug Death_3MONTHS, Death_6MONTHS, Death_12MONTHS
1        1       0            0               NaN
2        1       1            NaN             NaN
3        2       0            0                0
4        2       0            0                1

NaN - 缺失值 = 失去跟进

问题

我想再创建两列

  1. 创建名为 1 year mortality

    的列

    如果Death_3months,或Death_6months,或Death_12 months = 1

    填写1,否则填写0

  2. 创建名为 Time to Event

    的列

    如果Death_3months = NaN填写0

    如果Death_3months = 1 填入3,

    if Death_3months = 0 检查 Death_6months

    如果Death_6months = NaN3

    如果Death_6months = 1 填入6,

    if Death_6months = 0检查Death_12months

    如果Death_12months = NaN6

    如果Death_12months = 1 填入12,

    如果Death_12months = 012

提前致谢

使用 data.table 包的解决方案:

library(data.table)

#initilize sample data.table
DT <- data.table(c(1,2,3,4), c(1,1,2,2),c(0,1,0,0),c(0,NaN,0,0),c(NaN,NaN,0,1))
colnames(DT) <- c("Subject","Drug","Death_3Months","Death_6months","Death_12months")

#Add row together using death columns and exclude NA's
DT[, "1 year mortality" := rowSums(.SD, na.rm=TRUE), .SDcols = 3:5]

#run through ifelse logic as described
DT[, "Time to Event" := ifelse(!is.finite(Death_3Months), 0,
                               ifelse(Death_3Months == 1, 3,
                                      ifelse(!is.finite(Death_6months), 3, 
                                             ifelse(Death_6months == 1, 6,
                                                    ifelse(!is.finite(Death_12months), 6, 12)))))]

DT

> DT
   Subject Drug Death_3Months Death_6months Death_12months 1 year mortality Time to Event
1:       1    1             0             0            NaN                0             6
2:       2    1             1           NaN            NaN                1             3
3:       3    2             0             0              0                0            12
4:       4    2             0             0              1                1            12