【问题标题】:R - Create new indices based on group_id and conditional statementR - 根据 group_id 和条件语句创建新索引
【发布时间】:2021-02-02 12:59:04
【问题描述】:

我正在使用如下所示的数据框(让我们调用 MyData)。我想做的是按 PatientKey 分组并创建一个名为 NewID 的新 ID。每次对于 TimeBetweenTests > 14 的同一个 PatientKey,新的 Id 应该增加 1,并保持在该特定的新值上,直到出现新的 PatientKey 或对于相同的 PatientKey,出现新的 TimeBetweenTests > 14。

PatientKey             TimeBetweenTests     NewId        
1                      0                    NewId should be 1 (first patient)
1                      0                    NewId should be 1
1                      1                    NewId should be 1                                                                
1                      2                    NewId should be 1
2                      3                    NewId should be 2 (new patient)                                                                          
3                      4                    NewId should be 3 (new patient)      
3                      16                   NewId should be 4 (same patient but TimeBetweenTests > 14)                                                                                              
3                      80                   NewId should be 5 (same patient but TimeBetweenTests > 14)
4                      3                    NewId should be 6 (new patient)
4                      0                    NewId should be 6 (new patient)                                                                            
4                      90                   NewId should be 7 (same patient but TimeBetweenTests > 14)        
4                      110                  NewId should be 8 (same patient but TimeBetweenTests > 14) 
5                      3                    NewId should be 9 (new patient)
5                      3                    NewId should be 9
5                      3                    NewId should be 9

etc    
                                                                       

我已尝试为此使用 dplyr,但问题是当我尝试类似以下代码时后续值不会改变:

MyData % group_by(PatientKey) %>% mutate(NewId = ifelse(TimeBetweenTests > 14, lag(NewId), NewId))

任何人都有一个方便的 dplyr 或 data.table 解决方案,或者 for 循环方法。

【问题讨论】:

    标签: r dplyr data.table


    【解决方案1】:

    试试这个

    library(dplyr)
    df %>% mutate(NewID = cumsum(lag(PatientKey, default = 0) != PatientKey | TimeBetweenTests > 14)
    

    输出

       PatientKey TimeBetweenTests NewID
            <dbl>            <dbl> <int>
     1          1                0     1
     2          1                0     1
     3          1                1     1
     4          1                2     1
     5          2                3     2
     6          3                4     3
     7          3               16     4
     8          3               80     5
     9          4                3     6
    10          4                0     6
    11          4               90     7
    12          4              110     8
    13          5                3     9
    14          5                3     9
    15          5                3     9
    

    【讨论】:

    • 我试过了,但问题是它只会从第一次超过 14 的值开始改变。当有新的 patientKey 时,它也应该改变 ID。
    • @PontusHedberg 嗨,检查更新。这是你需要的吗?
    • 嘿,感谢您的努力,但仍然是一个问题,并且不会导致基于新患者和/或值 > 14 的正确累积索引。Best Pontus
    • @PontusHedberg 也许我误解了你的意思。每当PatientKeyTimeBetweenTests &gt; 14 发生变化时,您都需要累积索引增加1,对吗?那么,如果所有两个条件同时发生(例如,在我上面显示的输入的第 13 行)怎么办?在这种情况下,您希望累积索引增加12
    • 嗨!是的,你理解正确!如果发生这种情况,它应该增加 1,而不是 2。谢谢,Pontus
    猜你喜欢
    • 2023-02-23
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2016-12-10
    相关资源
    最近更新 更多