根据R

时间:2019-04-01 18:59:40

标签: r recode

不幸的是,我正在努力弄清楚如何基于一系列分类变量(具有缺失值)创建新变量。

我有以下数据集(模拟数据)

df = data.frame(ID = c(1001, 1002, 1003, 1004, 1005,1006,1007,  1008,1009,1010,1011),
                    Disease_code_1 = c('I802', 'H356','G560','D235', 'B178', 'F011', 'F023', 'C761', 'H653', 'A049', 'J679'),
                    Disease_code_2 = c('A071','NA','G20','NA','NA', 'A049','NA', 'NA','G300','G308','A045'),
                    Disease_code_3 = c('H250', 'NA','NA','I802','NA', 'A481', 'NA','NA','NA','NA','D352'))

哪个给:

     ID Disease_code_1 Disease_code_2 Disease_code_3
1  1001           I802           A071           H250
2  1002           H356             NA             NA
3  1003           G560            G20             NA
4  1004           D235             NA           I802
5  1005           B178             NA             NA
6  1006           F011           A049           A481
7  1007           F023             NA             NA
8  1008           C761             NA             NA
9  1009           H653           G300             NA
10 1010           A049           G308             NA
11 1011           J679           A045           D352

我想创建一个新变量,为那些带有疾病代码子集(例如F023,G20,G300)的人分配1(存在疾病)。我试图遵循先前回答的堆栈溢出问题,但获得的成功有限:

df$test <- NA
df$test <-sapply(df[ , 2:4] , 
                 FUN = function(x) recode(x, "'G20' =1; 'G300' =1",
                                          as.factor.result=FALSE))

哪个会导致错误:

 Error: Argument 2 must be named, not unnamed 

理想情况下,我希望我的数据集看起来像这样:

     ID Disease_code_1 Disease_code_2 Disease_code_3 Disease_present
1  1001           I802           A071           H250               0
2  1002           H356             NA             NA               0
3  1003           G560            G20             NA               1
4  1004           D235             NA           I802               0
5  1005           B178             NA             NA               0
6  1006           F011           A049           A481               0
7  1007           F023             NA             NA               0
8  1008           C761             NA             NA               0
9  1009           H653           G300             NA               1
10 1010           A049           G308             NA               0
11 1011           J679           A045           D352               0

真的很感谢任何建议!

1 个答案:

答案 0 :(得分:1)

您可以如下使用apply

df$Disease_present <- apply(df[, -1], 1, function(x) {
  if(any(x %in% c("G20", "G300"))) {
    return(1)
  } else {
    return(0)
  }
})
df
     ID Disease_code_1 Disease_code_2 Disease_code_3 Disease_present
1  1001           I802           A071           H250               0
2  1002           H356             NA             NA               0
3  1003           G560            G20             NA               1
4  1004           D235             NA           I802               0
5  1005           B178             NA             NA               0
6  1006           F011           A049           A481               0
7  1007           F023             NA             NA               0
8  1008           C761             NA             NA               0
9  1009           H653           G300             NA               1
10 1010           A049           G308             NA               0
11 1011           J679           A045           D352               0