通过组移动窗口来区分计数

时间:2019-10-03 15:54:24

标签: r group-by dplyr sum distinct

比方说,我有一个数据集,其中包含医院就诊次数。我的目标是生成一个变量,该变量计算访问者在访问日期之前见过的唯一患者的数量。我经常与dplyr的group_by一起工作,但这似乎有些棘手。我猜我将不得不使用group_by,n_distinct和sum或某种移动窗口命令。我需要“目标”变量。

visitor visitdt patient goal
125469  1/12/2018   15200   1
125469  1/19/2018   15200   1
125469  2/16/2018   15200   1
125469  2/23/2018   52607   2
125469  3/9/2018    52607   2
125469  3/16/2018   52607   2
125469  3/23/2018   15200   2
125469  3/29/2018   15200   2
125469  3/30/2018   20589   3
125469  4/6/2018    20589   3

谢谢, 马文

3 个答案:

答案 0 :(得分:3)

您可以这样做:

with(df, ave(patient, visitor, FUN = function(x) cumsum(!duplicated(x))))

 [1] 1 1 1 2 2 2 2 2 3 3

本质上,它是每个组中非重复值的累积总和。

您也可以使用dplyr做同样的事情:

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

答案 1 :(得分:2)

我们可以使用dplyr

library(dplyr)   
df1 %>%
   group_by(visitor) %>%
    mutate(goal = cummax(match(patient, unique(patient))))
    #or with factor
    # mutate(goal1 = cummax(as.integer(factor(patient, levels = unique(patient)))))

# A tibble: 10 x 4
# Groups:   visitor [1]
#   visitor visitdt   patient  goal
#     <int> <chr>       <int> <int>
# 1  125469 1/12/2018   15200     1
# 2  125469 1/19/2018   15200     1
# 3  125469 2/16/2018   15200     1
# 4  125469 2/23/2018   52607     2
# 5  125469 3/9/2018    52607     2
# 6  125469 3/16/2018   52607     2
# 7  125469 3/23/2018   15200     2
# 8  125469 3/29/2018   15200     2
# 9  125469 3/30/2018   20589     3
#10  125469 4/6/2018    20589     3

数据

df1 <- structure(list(visitor = c(125469L, 125469L, 125469L, 125469L, 
125469L, 125469L, 125469L, 125469L, 125469L, 125469L), visitdt = c("1/12/2018", 
"1/19/2018", "2/16/2018", "2/23/2018", "3/9/2018", "3/16/2018", 
"3/23/2018", "3/29/2018", "3/30/2018", "4/6/2018"), patient = c(15200L, 
15200L, 15200L, 52607L, 52607L, 52607L, 15200L, 15200L, 20589L, 
20589L), goal = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L)),
class = "data.frame", row.names = c(NA, 
-10L))

答案 2 :(得分:2)

与您所追踪的声音很重要的声音。使用data.table的另一种选择是使用非等额联接,然后通过引用进行更新:

DT[, goal2 :=
    DT[.SD, on=.(visitor, visitdt<=visitdt), allow.cartesian=TRUE, 
        length(unique(patient)), by=.EACHI]$V1]

输出:

    visitor    visitdt patient goal goal2
 1:  125469 2018-01-12   15200    1     1
 2:  125469 2018-01-19   15200    1     1
 3:  125469 2018-02-16   15200    1     1
 4:  125469 2018-02-23   52607    2     2
 5:  125469 2018-03-09   52607    2     2
 6:  125469 2018-03-16   52607    2     2
 7:  125469 2018-03-23   15200    2     2
 8:  125469 2018-03-29   15200    2     2
 9:  125469 2018-03-30   20589    3     3
10:  125469 2018-04-06   20589    3     3

数据:

library(data.table)
DT <- fread("visitor visitdt patient goal
125469  1/12/2018   15200   1
125469  1/19/2018   15200   1
125469  2/16/2018   15200   1
125469  2/23/2018   52607   2
125469  3/9/2018    52607   2
125469  3/16/2018   52607   2
125469  3/23/2018   15200   2
125469  3/29/2018   15200   2
125469  3/30/2018   20589   3
125469  4/6/2018    20589   3")
DT[, visitdt := as.Date(visitdt, "%m/%d/%Y")]