R-计算连续日期的最大数目 [英] R - count maximum number of consecutive dates
问题描述
我正在尝试按组计算连续日期的最大长度,但我一直在努力获取。我已经包含了类似布局的代码的代码。
I'm trying to count the maximum length of consecutive dates by group, but I'm struggling to get it. I have included code for a similarly laid-out tibble to mine.
library(dplyr)
# construct sample tibble:
df <- tibble(
key = rep(1:2, c(6,4)),
Date = c(seq(as.Date('2016-12-17'), as.Date('2016-12-19'), '1 day'),
seq(as.Date('2016-12-21'), as.Date('2016-12-23'), '1 day'),
seq(as.Date('2017-05-18'), as.Date('2017-05-21'), '1 day'))
)
我尝试使用 lag()
添加标签( 1
)以指示日期之间是否有间隔,然后计算的最大长度使用
,但这不适用于 rle
在列中输入0 键中连续日期和间隔的所有可能配置
。
I tried to use lag()
to add a tag (1
) to indicate when there was a gap between dates, and then count the max length of 0
in the column using rle
, but this doesn't work for all possible configurations of consecutive dates and gaps within keys
.
是否可以按组返回最大连续日期数?
Is there a way to return the maximum number of consecutive dates by group?
推荐答案
让我们首先创建一个函数,该函数将找到最大连续天数s,给定一个向量:
Let's first make a function that would find the maximum number of consecutive days, given a vector:
gl <- function(x) {
y <- c(unclass(diff(x))) # c and unclass -- preparing it for rle
r <- rle(y)
with(r, max(lengths[values==1]))
}
现在我们可以使用通常的dplyrry方法:
Now we can use it the usual dplyrry way:
df %>% group_by(key) %>% summarise(max.consecutive = gl(Date))
# A tibble: 2 x 2
# key max.consecutive
# <int> <int>
# 1 1 2
# 2 2 3
这篇关于R-计算连续日期的最大数目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!