【问题标题】:Create binary categorical variables based on contents of a column delimited list根据列分隔列表的内容创建二进制分类变量
【发布时间】:2020-07-18 23:45:20
【问题描述】:

我的数据框中有一个名为“心脏合并症类型”的变量,其中包含 NA 或各种心脏合并症类型的列分隔列表。如何为每种可能的合并症制作一列,然后用 1/0 填写观察结果,其中 1 = 表示存在合并症,0 = 没有合并症。

dput(head(et1$`Cardiac Comorbidity Types`,20))
c("MI,", NA, "CAD, Previous CABG or PTCA, MI, Pacemaker,", "Arrhythmia,", 
"CAD, Previous CABG or PTCA, MI, Arrhythmia,", NA, "CAD, Previous CABG or PTCA, MI,", 
"CAD, Previous CABG or PTCA, CHF, Pacemaker,", "CAD, Previous CABG or PTCA,", 
"CAD, Previous CABG or PTCA, Arrhythmia,", "CAD, Previous CABG or PTCA,", 
"CAD, Previous CABG or PTCA, MI,", "CAD, Previous CABG or PTCA, CHF, Arrhythmia,", 
"CAD, Previous CABG or PTCA, Pacemaker,", "CAD, Previous CABG or PTCA, MI, CHF,", 
"CAD, Previous CABG or PTCA, MI, CHF,", NA, "CAD, Previous CABG or PTCA, PVD, Pacemaker,", 
"PVD,", "CAD, Previous CABG or PTCA,")

另外,如果数据是用分号分隔的,我该怎么做?

【问题讨论】:

    标签: r database data-cleaning csv medical


    【解决方案1】:

    我们可以使用splitstackshape 中的cSplit_e 转换为二进制列。

    splitstackshape::cSplit_e(et1, "Cardiac.Comorbidity.Types", 
                              type = "character", fill = 0)
    

    cSplit_e 中的默认 sep 参数是 ",",如果您有分号分隔的数据,您可以明确提及。

    splitstackshape::cSplit_e(et1, "Cardiac.Comorbidity.Types", sep = ";", 
                              type = "character", fill = 0)
    

    【讨论】:

      【解决方案2】:

      我们可以使用来自tidyrunnestpivot_wider 的组合。

      library(dplyr)
      library(tidyr)
      library(stringr)
      data <- data %>% mutate(ID = 1:nrow(data))
      
      data %>% 
        mutate(Cardiac.Comorbidity.Types = str_split(Cardiac.Comorbidity.Types, ", ?")) %>%
        unnest(Cardiac.Comorbidity.Types) %>%
        filter(Cardiac.Comorbidity.Types != "") %>%
        pivot_wider(id_cols = "ID", names_from = Cardiac.Comorbidity.Types, values_from = Cardiac.Comorbidity.Types) %>%
        right_join(data, by="ID") %>%
        mutate_at(vars(-ID,-Cardiac.Comorbidity.Types), ~ as.integer(!is.na(.x))) %>% select(-ID)
      # A tibble: 20 x 8
      #      MI   CAD `Previous CABG or PTCA` Pacemaker Arrhythmia   CHF   PVD Cardiac.Comorbidity.Types                   
      #   <int> <int>                   <int>     <int>      <int> <int> <int> <fct>                                       
      # 1     1     0                       0         0          0     0     0 MI,                                         
      # 2     0     0                       0         0          0     0     0 NA                                          
      # 3     1     1                       1         1          0     0     0 CAD, Previous CABG or PTCA, MI, Pacemaker,  
      # 4     0     0                       0         0          1     0     0 Arrhythmia,                                 
      # 5     1     1                       1         0          1     0     0 CAD, Previous CABG or PTCA, MI, Arrhythmia, 
      ...
      

      数据

      data <- c("MI,", NA, "CAD, Previous CABG or PTCA, MI, Pacemaker,", "Arrhythmia,", 
      "CAD, Previous CABG or PTCA, MI, Arrhythmia,", NA, "CAD, Previous CABG or PTCA, MI,", 
      "CAD, Previous CABG or PTCA, CHF, Pacemaker,", "CAD, Previous CABG or PTCA,", 
      "CAD, Previous CABG or PTCA, Arrhythmia,", "CAD, Previous CABG or PTCA,", 
      "CAD, Previous CABG or PTCA, MI,", "CAD, Previous CABG or PTCA, CHF, Arrhythmia,", 
      "CAD, Previous CABG or PTCA, Pacemaker,", "CAD, Previous CABG or PTCA, MI, CHF,", 
      "CAD, Previous CABG or PTCA, MI, CHF,", NA, "CAD, Previous CABG or PTCA, PVD, Pacemaker,", 
      "PVD,", "CAD, Previous CABG or PTCA,")
      data <- data.frame(Cardiac.Comorbidity.Types = data)
      

      【讨论】:

      • 当我在整个数据帧上运行代码时,我的控制台中的表头似乎在最后读取:Atrial fibrillationatrial fibrillationMI; Arrhythmia; CHF...有没有办法解决这个问题你知道吗?
      • 我如何将这些列添加到我的原始数据框中?
      • 对分号有效,但我仍然有Atrial fibrillation atrial fibrillation 在顶部,我该如何解决a/A 交易?此外,它的行数与我的原始数据帧相同,这对所有这一切都有点新意,但是在此代码的上下文中您将如何使用 cbind 或 mutate。非常感谢您的帮助
      • 对不起,我的意思是我的控制台中的标题
      • 基本上它的混淆心房颤动和心房颤动
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      相关资源
      最近更新 更多