基于开始日期和结束日期数组聚合数据帧中的值-R

基于开始日期和结束日期数组聚合数据帧中的值-R,r,window,aggregate,subset,R,Window,Aggregate,Subset,示例数据: Date_End <- c("1999-08-30","1999-09-07","1999-09-20","1999-09-27","1999-10-04","1999-10-12") Date_Start <- c("1999-08-24" ,"1999-08-30" ,"1999-09-13" ,"1999-09-20" ,"1999-09-27" ,"1999-10-04") as.Date(Date_Start, "%Y-%m-%d"

示例数据:

    Date_End   <- c("1999-08-30","1999-09-07","1999-09-20","1999-09-27","1999-10-04","1999-10-12")
    Date_Start <- c("1999-08-24" ,"1999-08-30" ,"1999-09-13" ,"1999-09-20" ,"1999-09-27" ,"1999-10-04") 
    as.Date(Date_Start, "%Y-%m-%d" )
    as.Date(Date_End, "%Y-%m-%d" )
    df1 <- data.frame(Date_Start,Date_End)  
    c1 <- data.frame(seq(as.Date('1999-08-24'), as.Date('1999-10-12'), by = 1))
    c2 <- sample(100, size = nrow(c1), replace = TRUE)
    df2 <- data.frame(c2,c1)
    names(df2) <- c("unit","date")
    df2 <- zoo(df2)
我已尝试使用两种窗口功能:

窗口(df2,start=df1$Date\u start,end=df1$Date\u end)

创建一个序列,然后创建索引:


seq_a可能应该使用函数而不是循环,但对于快速而肮脏的循环,您可以执行以下操作:

Date_End   <- c("1999-08-30","1999-09-07","1999-09-20","1999-09-27","1999-10-04","1999-10-12")
Date_Start <- c("1999-08-24" ,"1999-08-30" ,"1999-09-13" ,"1999-09-20" ,"1999-09-27" ,"1999-10-04") 
Date_Start <- as.Date(Date_Start, "%Y-%m-%d" )
Date_End   <- as.Date(Date_End, "%Y-%m-%d" )
df1 <- data.frame(Date_Start,Date_End)
c1 <- data.frame(seq(as.Date('1999-08-24'), as.Date('1999-10-12'), by = 1))
c2 <- sample(100, size = nrow(c1), replace = TRUE)
df2 <- data.frame(c2,c1)
names(df2) <- c("unit","date")

for (i in 1:nrow(df1)) {
  df1$sum[i] <- sum(df2$unit[df2$date > df1$Date_Start[i] & df2$date < df1$Date_End[i]])
}

Date\u End可能应该使用函数而不是循环,但对于快速而肮脏的循环,您可以执行以下操作:

Date_End   <- c("1999-08-30","1999-09-07","1999-09-20","1999-09-27","1999-10-04","1999-10-12")
Date_Start <- c("1999-08-24" ,"1999-08-30" ,"1999-09-13" ,"1999-09-20" ,"1999-09-27" ,"1999-10-04") 
Date_Start <- as.Date(Date_Start, "%Y-%m-%d" )
Date_End   <- as.Date(Date_End, "%Y-%m-%d" )
df1 <- data.frame(Date_Start,Date_End)
c1 <- data.frame(seq(as.Date('1999-08-24'), as.Date('1999-10-12'), by = 1))
c2 <- sample(100, size = nrow(c1), replace = TRUE)
df2 <- data.frame(c2,c1)
names(df2) <- c("unit","date")

for (i in 1:nrow(df1)) {
  df1$sum[i] <- sum(df2$unit[df2$date > df1$Date_Start[i] & df2$date < df1$Date_End[i]])
}

Date\u End此解决方案不能将
df2
用作
zoo
对象,但它可能仍然对您有用:

Date_End   <- as.Date(c("1999-08-30","1999-09-07","1999-09-20","1999-09-27","1999-10-04","1999-10-12"))
Date_Start <- as.Date(c("1999-08-24" ,"1999-08-30" ,"1999-09-13" ,"1999-09-20" ,"1999-09-27" ,"1999-10-04")) 
df1 <- data.frame(Date_Start,Date_End)  
c1 <- seq(as.Date('1999-08-24'), as.Date('1999-10-12'), by = 1)
c2 <- sample(100, size = length(c1), replace = TRUE)
df2 <- data.frame(unit = c2, date = c1)

library(sqldf)
> sqldf("select Date_Start, Date_End, sum(unit) as units 
      from df1, 
           df2 
      where df1.Date_Start <= df2.date 
      and df2.date <= df1.Date_end 
      group by Date_Start")
Date_Start   Date_End units
1 1999-08-24 1999-08-30   258
2 1999-08-30 1999-09-07   493
3 1999-09-13 1999-09-20   423
4 1999-09-20 1999-09-27   432
5 1999-09-27 1999-10-04   433
6 1999-10-04 1999-10-12   584

Date\u End此解决方案不能将
df2
用作
zoo
对象,但它可能仍然对您有用:

Date_End   <- as.Date(c("1999-08-30","1999-09-07","1999-09-20","1999-09-27","1999-10-04","1999-10-12"))
Date_Start <- as.Date(c("1999-08-24" ,"1999-08-30" ,"1999-09-13" ,"1999-09-20" ,"1999-09-27" ,"1999-10-04")) 
df1 <- data.frame(Date_Start,Date_End)  
c1 <- seq(as.Date('1999-08-24'), as.Date('1999-10-12'), by = 1)
c2 <- sample(100, size = length(c1), replace = TRUE)
df2 <- data.frame(unit = c2, date = c1)

library(sqldf)
> sqldf("select Date_Start, Date_End, sum(unit) as units 
      from df1, 
           df2 
      where df1.Date_Start <= df2.date 
      and df2.date <= df1.Date_end 
      group by Date_Start")
Date_Start   Date_End units
1 1999-08-24 1999-08-30   258
2 1999-08-30 1999-09-07   493
3 1999-09-13 1999-09-20   423
4 1999-09-20 1999-09-27   432
5 1999-09-27 1999-10-04   433
6 1999-10-04 1999-10-12   584

Date\u结束感谢您的回答和链接!这真的很有帮助。谢谢你的回答和链接!这真的很有帮助。