【问题标题】:r nested indicator 1st of 1st and 2nd of 1st and 2nd of 2ndr 嵌套指示器 1st of 1st 和 2nd of 1st 和 2nd of 2nd
【发布时间】:2022-11-04 14:44:40
【问题描述】:

我有一个像这样重复观察的数据集。

  Id    Date        Group  Diagnosis    
  1     8/16/2004   Red    A
  1     8/16/2004   Red    B
  1     8/16/2004   Red    C

  2     4/23/2010   Blue    A
  2     4/23/2010   Blue    C

  3     5/13/2006   Blue    A
  3     5/13/2006   Blue    B
  3     5/13/2006   Blue    C
  3     6/05/2011   Blue    A
  3     6/05/2011   Blue    B
  3     6/05/2011   Blue    C

  4     10/06/2009   Blue    A
  4     10/06/2009   Blue    B
  4     10/06/2009   Blue    C
  4     7/22/2010    Blue    A
  4     7/22/2010    Blue    B

我喜欢创建一个新的指标值来跟踪哪些观察值只有一组观察值,哪些观察值有两个。在那些有两组观察值的人中,指标应该指出哪一组是第一组和第二组,依此类推。

Expected output

      Id    Date        Group  Diagnosis   I   
      1     8/16/2004   Red    A           1-1
      1     8/16/2004   Red    B           1-1
      1     8/16/2004   Red    C           1-1

      2     4/23/2010   Blue    A          1-1
      2     4/23/2010   Blue    C          1-1

      3     5/13/2006   Blue    A          2-1
      3     5/13/2006   Blue    B          2-1      
      3     5/13/2006   Blue    C          2-1 
      3     6/05/2011   Blue    A          2-2 
      3     6/05/2011   Blue    B          2-2
      3     6/05/2011   Blue    C          2-2

      4     10/06/2009   Blue    A         2-1
      4     10/06/2009   Blue    B         2-1
      4     10/06/2009   Blue    C         2-1
      4     7/22/2010    Blue    A         2-2
      4     7/22/2010    Blue    B         2-2

Id 1 和 Id 2 列中的值为 1-1,因为这两个 Id 都只有一次诊断集,Id 1 于 2004 年 8 月 16 日评估,Id 2 于 2010 年 4 月 23 日评估

Id 3 和 Id 4 列中的值为 2-1,因为这两个 Id 都有两组诊断,Id 3 分别于 2006 年 5 月 13 日和 2011 年 6 月 5 日评估,Id 4 评估于 2009 年 6 月 10 日和2010 年 7 月 22 日。 Id 3 和 Id 4 的第一组观察是在 2006 年 5 月 13 日和 2009 年 6 月 10 日,所以 2-1。 Id 3 和 Id 4 的第二组观察是在 2011 年 6 月 5 日和 2010 年 7 月 22 日,所以 2-2

我使用group_by( Id,Group, Diagnosis) 尝试了n()n_distinct,但这不起作用。因此,非常感谢任何建议或帮助。谢谢。

【问题讨论】:

    标签: r dplyr counter counting


    【解决方案1】:

    我们可以做的:

    library(dplyr)
    
    df |> 
      group_by(Id) |> 
      mutate(I = paste0(n_distinct(Date), "-", match(Date, unique(Date)))) |> 
      ungroup()
    

    使用paste0 粘贴组中不同日期的数量,使用match 将日期与第一个匹配项匹配

          Id Date       Group Diagnosis I    
       <int> <chr>      <chr> <chr>     <chr>
     1     1 8/16/2004  Red   A         1-1  
     2     1 8/16/2004  Red   B         1-1  
     3     1 8/16/2004  Red   C         1-1  
     4     2 4/23/2010  Blue  A         1-1  
     5     2 4/23/2010  Blue  C         1-1  
     6     3 5/13/2006  Blue  A         2-1  
     7     3 5/13/2006  Blue  B         2-1  
     8     3 5/13/2006  Blue  C         2-1  
     9     3 6/05/2011  Blue  A         2-2  
    10     3 6/05/2011  Blue  B         2-2  
    11     3 6/05/2011  Blue  C         2-2  
    12     4 10/06/2009 Blue  A         2-1  
    13     4 10/06/2009 Blue  B         2-1  
    14     4 10/06/2009 Blue  C         2-1  
    15     4 7/22/2010  Blue  A         2-2  
    16     4 7/22/2010  Blue  B         2-2
    

    【讨论】:

    • 谢谢詹姆斯,这是什么语法df |&gt;
    • 它被称为管道,一种将第一个参数传递给函数的方式。使代码更易于阅读。如果这解决了您的问题,请不要犹豫,单击复选标记来解决问题。
    • 谢谢。这和 %>% 一样吗?
    • 是的,它是相似的,它只是独立于任何库。在大多数情况下,它可以替换 dplyr 管道
    【解决方案2】:

    这是dplyr 方式。

    df1 <- "Id    Date        Group  Diagnosis    
      1     8/16/2004   Red    A
      1     8/16/2004   Red    B
      1     8/16/2004   Red    C
    
      2     4/23/2010   Blue    A
      2     4/23/2010   Blue    C
    
      3     5/13/2006   Blue    A
      3     5/13/2006   Blue    B
      3     5/13/2006   Blue    C
      3     6/05/2011   Blue    A
      3     6/05/2011   Blue    B
      3     6/05/2011   Blue    C
    
      4     10/06/2009   Blue    A
      4     10/06/2009   Blue    B
      4     10/06/2009   Blue    C
      4     7/22/2010    Blue    A
      4     7/22/2010    Blue    B"
    df1 <- read.table(textConnection(df1), header = TRUE)
    
    suppressPackageStartupMessages({
      library(dplyr)
    })
    
    df1 %>%
      group_by(Id, Group) %>%
      mutate(I2 = duplicated(Diagnosis),
             I1 = any(I2) + 1L,
             I2 = I2 + 1L,
             I = paste(I1, I2, sep = "-")) %>%
      ungroup() %>%
      select(-I1, -I2)
    #> # A tibble: 16 × 5
    #>       Id Date       Group Diagnosis I    
    #>    <int> <chr>      <chr> <chr>     <chr>
    #>  1     1 8/16/2004  Red   A         1-1  
    #>  2     1 8/16/2004  Red   B         1-1  
    #>  3     1 8/16/2004  Red   C         1-1  
    #>  4     2 4/23/2010  Blue  A         1-1  
    #>  5     2 4/23/2010  Blue  C         1-1  
    #>  6     3 5/13/2006  Blue  A         2-1  
    #>  7     3 5/13/2006  Blue  B         2-1  
    #>  8     3 5/13/2006  Blue  C         2-1  
    #>  9     3 6/05/2011  Blue  A         2-2  
    #> 10     3 6/05/2011  Blue  B         2-2  
    #> 11     3 6/05/2011  Blue  C         2-2  
    #> 12     4 10/06/2009 Blue  A         2-1  
    #> 13     4 10/06/2009 Blue  B         2-1  
    #> 14     4 10/06/2009 Blue  C         2-1  
    #> 15     4 7/22/2010  Blue  A         2-2  
    #> 16     4 7/22/2010  Blue  B         2-2
    

    创建于 2022-11-04,reprex v2.0.2

    【讨论】:

    • 谢谢瑞,我会测试的。
    猜你喜欢
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 2020-09-07
    • 2011-07-12
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多