将日期列表转换为序列
Convert list of dates to sequences
我正在尝试在日期列表中查找序列并将它们转换为开始日期和结束日期。
我的数据示例如下所示:
id date
1 1 2020-01-01
2 1 2020-01-02
3 1 2020-01-03
4 1 2020-01-06
5 1 2020-01-07
6 2 2020-01-02
7 2 2020-01-03
8 2 2020-01-04
9 2 2020-01-05
10 3 2020-01-04
11 3 2020-01-07
我想创建的是以下内容table:
id start date end date
1 1 2020-01-01 2020-01-03
2 1 2020-01-06 2020-01-07
3 2 2020-01-02 2020-01-05
4 3 2020-01-04 2020-01-04
5 3 2020-01-07 2020-01-07
我一直在摆弄 diff 函数,但我不能完全让它按照我想要的方式工作。
一个dplyr
选项可以是:
df %>%
group_by(id) %>%
mutate(date = as.Date(date, format = "%Y-%m-%d")) %>%
group_by(id, grp = cumsum(c(0, !diff(date) %in% 0:1))) %>%
summarise(start_date = min(date),
end_date = max(date))
id grp start_date end_date
<int> <dbl> <date> <date>
1 1 0 2020-01-01 2020-01-03
2 1 1 2020-01-06 2020-01-07
3 2 0 2020-01-02 2020-01-05
4 3 0 2020-01-04 2020-01-04
5 3 1 2020-01-07 2020-01-07
DT[, grp := cumsum(date - shift(date, 1L, fill = date[1]) > 1), by = id]
DT[, .(start_date = date[1], end_date = date[.N]), by = .(id, grp)][, !"grp"]
# id start_date end_date
# 1: 1 2020-01-01 2020-01-03
# 2: 1 2020-01-06 2020-01-07
# 3: 2 2020-01-02 2020-01-05
# 4: 3 2020-01-04 2020-01-04
# 5: 3 2020-01-07 2020-01-07
可重现的数据
DT <- data.table(
id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L),
date = structure(
c(18262, 18263, 18264, 18267, 18268, 18263, 18264, 18265, 18266, 18265, 18268),
class = "Date"
)
)
使用 by
和 rle
的基础 R 方法。
res <- do.call(rbind, by(DF, DF$id, function(x) {
cbind(id=x[1,1], setNames(
do.call(rbind, Map(function(i, j) data.frame(i, i + j),
x[c(0, diff(x[,2])) != 1, 2],
rle(cumsum(c(0, diff(x[,2])) != 1))$lengths - 1
)), c("start", "end")))
}))
res
# id start end
# 1.1 1 2020-01-01 2020-01-03
# 1.2 1 2020-01-06 2020-01-07
# 2 2 2020-01-02 2020-01-05
# 3.1 3 2020-01-04 2020-01-04
# 3.2 3 2020-01-07 2020-01-07
数据:
DF <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L), date = structure(c(18262, 18263, 18264, 18267, 18268, 18263,
18264, 18265, 18266, 18265, 18268), class = "Date")), row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11"), class = "data.frame")
One-liner(但与其他答案并没有什么不同)
library(data.table)
dt <- data.table(
id = c(1,1,1,1,1,2,2,2,2,3,3),
date = lubridate::ymd('2020-01-01')+c(0:2,5,6,1:4,3,6))
# calculate
dt[, .(start = date[c(T, x <- diff(date) != 1)], end = date[c(x, T)]), id]
#> id start end
#> 1: 1 2020-01-01 2020-01-03
#> 2: 1 2020-01-06 2020-01-07
#> 3: 2 2020-01-02 2020-01-05
#> 4: 3 2020-01-04 2020-01-04
#> 5: 3 2020-01-07 2020-01-07
我正在尝试在日期列表中查找序列并将它们转换为开始日期和结束日期。
我的数据示例如下所示:
id date
1 1 2020-01-01
2 1 2020-01-02
3 1 2020-01-03
4 1 2020-01-06
5 1 2020-01-07
6 2 2020-01-02
7 2 2020-01-03
8 2 2020-01-04
9 2 2020-01-05
10 3 2020-01-04
11 3 2020-01-07
我想创建的是以下内容table:
id start date end date
1 1 2020-01-01 2020-01-03
2 1 2020-01-06 2020-01-07
3 2 2020-01-02 2020-01-05
4 3 2020-01-04 2020-01-04
5 3 2020-01-07 2020-01-07
我一直在摆弄 diff 函数,但我不能完全让它按照我想要的方式工作。
一个dplyr
选项可以是:
df %>%
group_by(id) %>%
mutate(date = as.Date(date, format = "%Y-%m-%d")) %>%
group_by(id, grp = cumsum(c(0, !diff(date) %in% 0:1))) %>%
summarise(start_date = min(date),
end_date = max(date))
id grp start_date end_date
<int> <dbl> <date> <date>
1 1 0 2020-01-01 2020-01-03
2 1 1 2020-01-06 2020-01-07
3 2 0 2020-01-02 2020-01-05
4 3 0 2020-01-04 2020-01-04
5 3 1 2020-01-07 2020-01-07
DT[, grp := cumsum(date - shift(date, 1L, fill = date[1]) > 1), by = id]
DT[, .(start_date = date[1], end_date = date[.N]), by = .(id, grp)][, !"grp"]
# id start_date end_date
# 1: 1 2020-01-01 2020-01-03
# 2: 1 2020-01-06 2020-01-07
# 3: 2 2020-01-02 2020-01-05
# 4: 3 2020-01-04 2020-01-04
# 5: 3 2020-01-07 2020-01-07
可重现的数据
DT <- data.table(
id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L),
date = structure(
c(18262, 18263, 18264, 18267, 18268, 18263, 18264, 18265, 18266, 18265, 18268),
class = "Date"
)
)
使用 by
和 rle
的基础 R 方法。
res <- do.call(rbind, by(DF, DF$id, function(x) {
cbind(id=x[1,1], setNames(
do.call(rbind, Map(function(i, j) data.frame(i, i + j),
x[c(0, diff(x[,2])) != 1, 2],
rle(cumsum(c(0, diff(x[,2])) != 1))$lengths - 1
)), c("start", "end")))
}))
res
# id start end
# 1.1 1 2020-01-01 2020-01-03
# 1.2 1 2020-01-06 2020-01-07
# 2 2 2020-01-02 2020-01-05
# 3.1 3 2020-01-04 2020-01-04
# 3.2 3 2020-01-07 2020-01-07
数据:
DF <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L), date = structure(c(18262, 18263, 18264, 18267, 18268, 18263,
18264, 18265, 18266, 18265, 18268), class = "Date")), row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11"), class = "data.frame")
One-liner(但与其他答案并没有什么不同)
library(data.table)
dt <- data.table(
id = c(1,1,1,1,1,2,2,2,2,3,3),
date = lubridate::ymd('2020-01-01')+c(0:2,5,6,1:4,3,6))
# calculate
dt[, .(start = date[c(T, x <- diff(date) != 1)], end = date[c(x, T)]), id]
#> id start end
#> 1: 1 2020-01-01 2020-01-03
#> 2: 1 2020-01-06 2020-01-07
#> 3: 2 2020-01-02 2020-01-05
#> 4: 3 2020-01-04 2020-01-04
#> 5: 3 2020-01-07 2020-01-07