【问题标题】:Eliminate duplicates based on conditions from several columns in R根据 R 中几列的条件消除重复项
【发布时间】:2021-06-20 04:52:10
【问题描述】:

这是我的数据集:

df <- data.frame(PatientID = c("3454","3454","3454","345","345","345"), date = c("05/01/2001", "02/06/1997", "29/03/2004", "05/2/2021", "01/06/1960", "29/03/2003"),
                 infarct1 = c(TRUE, NA, TRUE, NA, NA, TRUE),infarct2 = c(TRUE, TRUE, TRUE, TRUE, NA, TRUE,  stringsAsFactors = F)

基本上我只需要保留 1 个患者 ID(又名,消除重复的 PatientID),基于最近的梗塞(最后一个 infarct==TRUE [但任何类型的梗塞] 基于 date)。

所以我想要的结果是这样的:

 df <- data.frame(PatientID = c("3454","345"), date = c("29/03/2004", "05/2/2021"),
                     infarct = c(TRUE,TRUE), stringsAsFactors = F)

希望这是有道理的。

谢谢

【问题讨论】:

    标签: r duplicates conditional-statements conditional-formatting


    【解决方案1】:

    您可以通过PatientIDdate 将日期转换为日期类,arrange 数据并获取infarct = TRUE 的最后日期。

    library(dplyr)
    
    df %>%
      mutate(date = lubridate::dmy(date)) %>%
      arrange(PatientID, date) %>%
      group_by(PatientID) %>%
      summarise(date = date[max(which(infarct))], 
                infract = TRUE)
    
    #  PatientID date       infract
    #  <chr>     <date>     <lgl>  
    #1 345       2003-03-29 TRUE   
    #2 3454      2004-03-29 TRUE   
    

    对于多列获取长格式数据。

    df %>%
      mutate(date = lubridate::dmy(date)) %>%
      tidyr::pivot_longer(cols = starts_with('infarct')) %>%
      arrange(PatientID, date) %>%
      group_by(PatientID) %>%
      slice(max(which(value))) %>%
      ungroup
    
    #  PatientID date       name     value
    #  <chr>     <date>     <chr>    <lgl>
    #1 345       2021-02-05 infarct2 TRUE 
    #2 3454      2004-03-29 infarct2 TRUE 
    

    数据

    我认为您需要在 date 列中为数据加上引号。

    df <- data.frame(PatientID = c("3454","3454","3454","345","345","345"), 
                     date = c("05/01/2001", "02/06/1997", "29/03/2004", "05/2/2021", "01/06/1960", "29/03/2003"),
                     infarct = c(TRUE, NA, TRUE, NA, NA, TRUE), stringsAsFactors = FALSE)
    

    【讨论】:

    • 这太完美了,谢谢!!但是,我刚刚编辑了帖子,因为我犯了一个错误。我有几种类型的事实,但我只对最后一个梗塞感兴趣。你会如何调整这个? - 百万谢谢!
    • 最后一个infarct 是指从所有infarct 列中获取最后一个TRUE 值吗?更新后的答案是否满足您的需求?
    • 给我一个错误...错误:向量内存已用尽(达到限制?)此外:警告消息:mutate() 输入问题date。 ℹ 所有格式解析失败。未找到格式。 ℹ 输入日期`是lubridate::dmy(date).
    • @Lili 表示您在问题中发布的数据不是您实际拥有的数据。如果date 列已经属于Date 类,那么您可以删除mutate(date = lubridate::dmy(date)) 行。
    【解决方案2】:

    试试这个:

    library(dplyr)
    
    df <- df %>% 
      mutate(infarct = infarct1 | infarct2) %>%
      filter(infarct == TRUE) %>%
      group_by(PatientID, infarct) %>%
      summarise(date=max(date))
    
    1. 创建infarct 变量。
    2. 过滤 TRUE 梗塞。
    3. 组。
    4. 寻找最后一次。

    【讨论】:

    • 错误:mutate() 输入 infarct 有问题。 x 操作仅适用于数字、逻辑或复杂类型 ℹ 输入 infarct|...。运行 rlang::last_error() 以查看错误发生的位置。
    • 我有 35 类梗塞,可能是这样吗?
    • 酷我认为它有效?它对我说,我认为“summarise() 已按 'PatientID' 对输出进行分组。您可以使用 .groups 参数覆盖”
    • 如果你对|有错误,那么使用any这样的函数infarct=any(inf1,inf2,inf3,...,na.rm = TRUE))
    猜你喜欢
    • 2020-05-22
    • 2021-09-09
    • 2015-10-15
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    相关资源
    最近更新 更多