这里有两种方法可以做到这一点。第一个使用sqldf,但需要(??)重命名列(因为SQL 不喜欢使用“.”的列名)。但从概念上讲,它更简单。
weather <- structure(list(weather.station = c("A", "A"), date = c("11/30/2013 ", "12/01/2013 "), temp = c(30, 28), HDD = c(35L, 37L)), .Names = c("weather.station", "date", "temp", "HDD"), class = "data.frame", row.names = c(NA, -2L))
home.bills <- structure(list(home.id = c(1, 1, 2, 2), start.date = c(" 11/15/2013 ", " 12/15/2013 ", " 11/18/2013 ", " 12/16/2013 "), end.date = c(" 12/14/2013 ", " 1/14/2014 ", " 12/15/2013 ", " 1/13/2014 "), electric.usage = c(80, 85, 60, 57), weather.station = c("A", "A", "A", "A")), .Names = c("home.id", "start.date", "end.date", "electric.usage", "weather.station"), class = "data.frame", row.names = c(NA, -4L))
# dates need to be Dates, not character
weather$date <- as.Date(weather$date,format="%m/%d/%Y")
home.bills$start.date <- as.Date(home.bills$start.date,format="%m/%d/%Y")
home.bills$end.date <- as.Date(home.bills$end.date,format="%m/%d/%Y")
# sqldf does not like "." in column names!!!
colnames(weather) <- gsub(".","_",colnames(weather),fixed=T)
colnames(home.bills) <- gsub(".","_",colnames(home.bills),fixed=T)
library(sqldf)
sqldf("select a.*, avg(temp) as mean_temp, sum(HDD) as sum_HDD
from [home.bills] a join weather b
on b.date>=a.start_date and b.date<=a.end_date
and a.weather_station=b.weather_station
group by home_id, start_date, end_date")
# home_id start_date end_date electric_usage weather_station mean_temp sum_HDD
# 1 1 2013-11-15 2013-12-14 80 A 29 72
# 2 2 2013-11-18 2013-12-15 60 A 29 72
第二个使用data.table 包中的foverlaps(...) 功能。这是非常强大且非常快速的。请注意,此解决方案与 @akrun 几个小时前发布的解决方案几乎相同,然后被删除(我很想知道为什么??)。
# data.table solution
weather <- structure(list(weather.station = c("A", "A"), date = c("11/30/2013 ", "12/01/2013 "), temp = c(30, 28), HDD = c(35L, 37L)), .Names = c("weather.station", "date", "temp", "HDD"), class = "data.frame", row.names = c(NA, -2L))
home.bills <- structure(list(home.id = c(1, 1, 2, 2), start.date = c(" 11/15/2013 ", " 12/15/2013 ", " 11/18/2013 ", " 12/16/2013 "), end.date = c(" 12/14/2013 ", " 1/14/2014 ", " 12/15/2013 ", " 1/13/2014 "), electric.usage = c(80, 85, 60, 57), weather.station = c("A", "A", "A", "A")), .Names = c("home.id", "start.date", "end.date", "electric.usage", "weather.station"), class = "data.frame", row.names = c(NA, -4L))
library(data.table) >= 1.9.4
# convert to data.tables and convert date to Date
setDT(weather)[,date:=as.Date(date,format="%m/%d/%Y")]
setDT(home.bills)[,(2:3):=lapply(.SD,as.Date,format="%m/%d/%Y"),.SDcols=2:3]
# need start.date and end.date in weather data.table (both = date)
weather[,c("start.date","end.date"):=list(date,date)]
setkey(home.bills,weather.station,start.date,end.date)
# calaculate overlaps
result <- foverlaps(weather,home.bills,nomatch=0)
# aggregate
result[,list(mean.temp=mean(temp),sum.HDD=sum(HDD)),
by=list(home.id,start.date,end.date,electric.usage,weather.station)]
# home.id start.date end.date electric.usage weather.station mean.temp sum.HDD
# 1: 1 2013-11-15 2013-12-14 80 A 29 72
# 2: 2 2013-11-18 2013-12-15 60 A 29 72