如何基于应用于大量列的“不等于”条件对数据帧进行子集化?

时间:2019-03-29 12:40:04

标签: r dataframe filter subset

我是R的新手,目前正尝试根据我的预定义排除标准对数据进行子集分析。我目前正在尝试删除ICD-10编码的所有患有痴呆症的病例。问题是,有多个变量包含有关每个人的疾病状况的信息(约70个变量),尽管由于它们以相同的方式编码,所以可以将相同的条件应用于所有变量。

一些模拟数据:

#Create dataframe containing simulated data
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'))

#data is structured as below:

     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


在这里,我正在尝试删除所有在“ disease_code”变量中带有“痴呆症代码”的病例。

#Remove cases with dementia from dataframe (e.g. F023, G20)
Newdata_df <- subset(df, (2:4 != "F023"|"G20"|"F009"|"F002"|"F001"|"F000"|"F00"|    
                    "G309"| "G308"|"G301"|"G300"|"G30"| "F01"|"F018"|"F013"|
                    "F012"| "F011"| "F010"|"F01"))

我收到的错误是:

Error in 2:4 != "F023" | "G20" : 
  operations are possible only for numeric, logical or complex types

理想情况下,子集数据帧如下所示:

     ID disease_code_1 disease_code_2 disease_code_3
1  1001           I802           A071           H250
2  1002           H356             NA             NA
4  1004           D235             NA           I802
5  1005           B178             NA             NA
8  1008           C761             NA             NA
11 1011           J679           A045           D352

我不确定我的代码中有错误,但是我不确定该如何解决。尽管到目前为止还没有运气,但我尝试了其他几种方法(使用dplyr)。

非常感谢您的帮助!

6 个答案:

答案 0 :(得分:4)

我们可以使用要删除的代码创建矢量,然后使用rowSums进行删除,即

codes_to_remove <- c("F023", "G20", "F009", "F002", "F001", "F000", "F00", "G309", "G308",
                "G301", "G300", "G30", "F01", "F018", "F013", "F012", "F011", "F010", "F01")

df[rowSums(sapply(df[-1], `%in%`, codes_to_remove)) == 0,]

给出,

     ID disease_code_1 disease_code_2 disease_code_3
1  1001           I802           A071           H250
2  1002           H356             NA             NA
4  1004           D235             NA           I802
5  1005           B178             NA             NA
8  1008           C761             NA             NA
11 1011           J679           A045           D352

答案 1 :(得分:3)

如何?

> dementia <- c("F023", "G20", "F009", "F002", "F001", "F000", "F00", "G309", "G308",
+               "G301", "G300", "G30", "F01", "F018", "F013", "F012", "F011", "F010", "F01")
> 
> dementia <- apply(sapply(df[, -1], function(x) {x %in% dementia}), 1, any)
> 
> df[!dementia,]
     ID disease_code_1 disease_code_2 disease_code_3
1  1001           I802           A071           H250
2  1002           H356             NA             NA
4  1004           D235             NA           I802
5  1005           B178             NA             NA
8  1008           C761             NA             NA
11 1011           J679           A045           D352
> 

编辑:

一个更优雅的解决方案,感谢@ Ronan Shah:

> df[apply(df[-1], 1, function(x) {!any(x %in% dementia)}),]
     ID disease_code_1 disease_code_2 disease_code_3
1  1001           I802           A071           H250
2  1002           H356             NA             NA
4  1004           D235             NA           I802
5  1005           B178             NA             NA
8  1008           C761             NA             NA
11 1011           J679           A045           D352

希望有帮助。

答案 2 :(得分:3)

一种dplyr可能是:

df %>%
 filter_at(vars(2:4), all_vars(! . %in% c("F023","G20","F009","F002","F001","F000","F00",    
            "G309", "G308","G301","G300","G30", "F01","F018","F013",
            "F012", "F011", "F010","F01")))

    ID disease_code_1 disease_code_2 disease_code_3
1 1001           I802           A071           H250
2 1002           H356             NA             NA
3 1004           D235             NA           I802
4 1005           B178             NA             NA
5 1008           C761             NA             NA
6 1011           J679           A045           D352

在这种情况下,它会检查2:4列中是否有任何给定的代码。

或者:

df %>%
 filter_at(vars(contains("disease_code")), all_vars(! . %in% c("F023","G20","F009","F002","F001","F000","F00",    
            "G309", "G308","G301","G300","G30", "F01","F018","F013",
            "F012", "F011", "F010","F01")))

在这种情况下,它将检查名称为disease_code的列是否包含任何给定的代码。

答案 3 :(得分:3)

如@docendo discimus的评论中所述,我们可以使用gathergroup_by ID将数据帧转换为长格式,并仅选择那些没有ID放入其中,然后dementia_code退回到宽格式。

spread

数据

library(tidyverse)

df %>%
   gather(key, value, -ID) %>%
   group_by(ID) %>%
   filter(!any(value %in% dementia_code)) %>%
   spread(key, value)

#   ID disease_code_1 disease_code_2 disease_code_3
#  <dbl> <chr>          <chr>          <chr>         
#1  1001 I802           A071           H250          
#2  1002 H356           NA             NA            
#3  1004 D235           NA             I802          
#4  1005 B178           NA             NA            
#5  1008 C761           NA             NA            
#6  1011 J679           A045           D352          

答案 4 :(得分:3)

我们可以使用melt/dcast中的data.table

library(data.table)
dcast(melt(setDT(df), id.var = 'ID')[,
     if(!any(value %in% dementia_codes)) .SD, .(ID)], ID ~ variable)
#    ID disease_code_1 disease_code_2 disease_code_3
#1: 1001           I802           A071           H250
#2: 1002           H356             NA             NA
#3: 1004           D235             NA           I802
#4: 1005           B178             NA             NA
#5: 1008           C761             NA             NA
#6: 1011           J679           A045           D352

或者可以在base R中更紧凑地完成此操作而无需重塑

df[!Reduce(`|`, lapply(df[-1], `%in%` , dementia_codes)),]
 #   ID disease_code_1 disease_code_2 disease_code_3
#1  1001           I802           A071           H250
#2  1002           H356             NA             NA
#4  1004           D235             NA           I802
#5  1005           B178             NA             NA
#8  1008           C761             NA             NA
#11 1011           J679           A045           D352

数据

dementia_codes <- c("F023", "G20", "F009", "F002", "F001", "F000", 
  "F00", "G309", "G308", "G301", "G300", "G30", "F01", "F018", "F013", 
   "F012", "F011", "F010", "F01")

答案 5 :(得分:2)

一个for R的base循环版本,如果您愿意的话。

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'), stringsAsFactors = FALSE)

dementia_codes <- c("F023", "G20", "F009", "F002", "F001", "F000", "F00", "G309", "G308", "G301", "G300", "G30", "F01", "F018", "F013", "F012", "F011", "F010", "F01")

new_df <- df[0,]

for(i in 1:nrow(df)){
  currRow <- df[i,]
  if(any(dementia_codes %in% as.character(currRow)) == FALSE){
    new_df <- rbind(new_df, currRow)
  }
}

new_df
#      ID disease_code_1 disease_code_2 disease_code_3
# 1  1001           I802           A071           H250
# 2  1002           H356             NA             NA
# 4  1004           D235             NA           I802
# 5  1005           B178             NA             NA
# 8  1008           C761             NA             NA
# 11 1011           J679           A045           D352