Count distinct by group- moving window

前端 未结 3 1296
没有蜡笔的小新
没有蜡笔的小新 2021-01-15 14:02

Let\'s say I have a dataset contain visits in a hospital. My goal is to generate a variable that counts the number of unique patients the visitor has seen before at the date

3条回答
  •  无人及你
    2021-01-15 14:14

    You can do:

    with(df, ave(patient, visitor, FUN = function(x) cumsum(!duplicated(x))))
    
     [1] 1 1 1 2 2 2 2 2 3 3
    

    Essentially, it is a cumulative sum of non-duplicated values per group.

    And you can also do the same with dplyr:

    df %>%
     group_by(visitor) %>%
     mutate(res = cumsum(!duplicated(patient)))
    

提交回复
热议问题