Count consecutive days by group

人走茶凉 提交于 2021-02-11 17:51:09

问题


I am looking to add a field that counts the number of consecutive days within each group (captured by id field). I start with this:

dt <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), date = c("1/01/2000", "2/01/2000", "2/01/2000", 
"5/01/2000", "6/01/2000", "7/01/2000", "8/01/2000", "13/01/2000", "14/01/2000", 
"18/01/2000", "19/01/2000", "21/01/2000", "25/01/2000", "26/01/2000", 
"30/01/2000", "31/01/2000")), .Names = c("id", "date"), 
row.names = c(NA, -16L), class = "data.frame")

And would like to obtain the following, ideally using data.table:

id date       cons
1  1/01/2000  0
1  2/01/2000  1
1  2/01/2000  1
1  5/01/2000  0
1  6/01/2000  1
1  7/01/2000  2
1  8/01/2000  3
2 13/01/2000  0
2 14/01/2000  1
2 18/01/2000  0
2 19/01/2000  1
2 21/01/2000  0
2 25/01/2000  0
2 26/01/2000  1
2 30/01/2000  0
2 31/01/2000  1

回答1:


Here's one way using dplyr

library(dplyr)

dt %>%
  mutate(date = as.Date(date, "%d/%m/%Y")) %>%
  group_by(id) %>%
  group_by(grp = cumsum(c(TRUE, diff(date) > 1)), add = TRUE) %>%
  mutate(cons = as.integer(date - first(date))) %>%
  ungroup %>%
  select(-grp)

#      id date        cons
#   <int> <date>     <int>
# 1     1 2000-01-01     0
# 2     1 2000-01-02     1
# 3     1 2000-01-02     1
# 4     1 2000-01-05     0
# 5     1 2000-01-06     1
# 6     1 2000-01-07     2
# 7     1 2000-01-08     3
# 8     2 2000-01-13     0
# 9     2 2000-01-14     1
#10     2 2000-01-18     0
#11     2 2000-01-19     1
#12     2 2000-01-21     0
#13     2 2000-01-25     0
#14     2 2000-01-26     1
#15     2 2000-01-30     0
#16     2 2000-01-31     1

As you tagged this data.table, the same can be translated to data.table

library(data.table)

setDT(dt)
dt[, date := as.Date(date, "%d/%m/%Y")]
dt[, cons := as.integer(date - first(date)), .(id, cumsum(c(TRUE, diff(date) > 1)))]



回答2:


I may be complicating things, but here should be a faster option if your dataset is large:

setDT(dt)[, date := as.Date(date, format="%d/%m/%Y")]

#identify consecutive dates
dt[, c("cons", "d", "rr") := .(0L, 
    d <- c(FALSE, diff(date) == 1L), 
    rowid(rleid(id, d)))]

#update rows with consecutive dates
idx <- dt[(d), which=TRUE]
set(dt, idx, "cons", dt[idx, rr])

#handle identical dates
ix <- dt[id==shift(id) & c(FALSE, diff(date)==0L), which=TRUE]
set(dt, ix, "cons", dt[ix - 1L, cons])

output:

    id       date cons     d rr
 1:  1 2000-01-01    0 FALSE  1
 2:  1 2000-01-02    1  TRUE  1
 3:  1 2000-01-02    1 FALSE  1
 4:  1 2000-01-05    0 FALSE  2
 5:  1 2000-01-06    1  TRUE  1
 6:  1 2000-01-07    2  TRUE  2
 7:  1 2000-01-08    3  TRUE  3
 8:  2 2000-01-13    0 FALSE  1
 9:  2 2000-01-14    1  TRUE  1
10:  2 2000-01-18    0 FALSE  1
11:  2 2000-01-19    1  TRUE  1
12:  2 2000-01-21    0 FALSE  1
13:  2 2000-01-25    0 FALSE  2
14:  2 2000-01-26    1  TRUE  1
15:  2 2000-01-30    0 FALSE  1
16:  2 2000-01-31    1  TRUE  1


来源:https://stackoverflow.com/questions/60033440/count-consecutive-days-by-group

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!