你可以做一個循環或笛卡爾合併。我不知道任何內置的函數可以做到這一點。
library(dplyr)
x = structure(list(StartDate = structure(c(1389312000, 1389484800,
1390089600, 1390608000, 1390953600, 1391558400, 1391731200, 1391904000,
1392681600), tzone = "UTC", class = c("POSIXct", "POSIXt")),
EndDate = structure(c(1390521600, 1390694400, 1391299200,
1391817600, 1392163200, 1392768000, 1392940800, 1393113600,
1393891200), tzone = "UTC", class = c("POSIXct", "POSIXt"
)), Sample = c(139L, 136L, 422L, 762L, 899L, 850L, 602L,
180L, 866L)), .Names = c("StartDate", "EndDate", "Sample"
), row.names = c(NA, -9L), class = "data.frame")
x2 = x
names(x2)=c('StartDate2','EndDate2','Sample2')
x3 = merge(x,x2,allow.cartesian =T)
x4 = summarise(group_by(x3,StartDate,EndDate),
sumifs=sum(Sample2[EndDate2 >= StartDate & StartDate2 <= EndDate]))
x_sumifs = merge(x,x4,by=c('StartDate','EndDate'))
這就是輸出的樣子。
> x_sumifs
StartDate EndDate Sample sumifs
1 2014-01-10 2014-01-24 139 697
2 2014-01-12 2014-01-26 136 1459
3 2014-01-19 2014-02-02 422 2358
4 2014-01-25 2014-02-08 762 3671
5 2014-01-29 2014-02-12 899 3715
6 2014-02-05 2014-02-19 850 4159
7 2014-02-07 2014-02-21 602 4159
8 2014-02-09 2014-02-23 180 3397
9 2014-02-18 2014-03-04 866 2498
這不會給OP想要的東西,對於每一行,他想看看其他行和總結樣品柱是否符合標準。 – 2014-11-03 20:54:40
非常感謝,但正如卡梅隆所說,這並不完全符合我的希望(儘管他總結得很完美)。我很感激幫助。 – Barnaby1 2014-11-03 21:51:05