假设您想要 3 个连续日期子集的列表。
data <- read.table(textConnection("Date p\n2001-01-04 6.9\n2001-01-05 4.5\n2001-01-06 5.9\n2001-01-08 15.8\n2001-01-24 1.3\n2001-01-25 4.6\n2001-01-26 13.0\n2001-01-27 45.1\n2001-02-01 5.0\n2001-02-05 21.9\n2001-02-06 25.4\n2001-02-09 1.4\n2001-02-10 1.9\n2001-02-13 9.1\n2001-02-14 23.0\n2001-02-15 8.8\n2001-02-22 1.1\n2001-02-28 24.8"),
header = TRUE, colClasses = c("Date", "numeric"))
# find out which dates are 3rd consecutive dates. sel below is logical vector indicating such dates
sel <- c(0, diff(data$Date)) == 1 & c(0, 0, diff(data$Date, 2) == 2)
# get start and end dates
start <- which(sel) - 2
end <- which(sel)
# get all the 3 consecutive dates subsets
mapply(function(start, end) data[start:end, ], start, end, SIMPLIFY = FALSE)
## [[1]]
## Date p
## 1 2001-01-04 6.9
## 2 2001-01-05 4.5
## 3 2001-01-06 5.9
##
## [[2]]
## Date p
## 5 2001-01-24 1.3
## 6 2001-01-25 4.6
## 7 2001-01-26 13.0
##
## [[3]]
## Date p
## 6 2001-01-25 4.6
## 7 2001-01-26 13.0
## 8 2001-01-27 45.1
##
## [[4]]
## Date p
## 14 2001-02-13 9.1
## 15 2001-02-14 23.0
## 16 2001-02-15 8.8
##