2

我正在从 SAS 迁移到 R。我需要帮助弄清楚如何汇总日期范围的天气数据。startdate在 SAS 中,我采用日期范围,使用数据步骤为范围内的每个日期(带, enddate, )创建记录date,与天气合并然后汇总 (VAR hdd cdd; CLASS=startdate enddate sum=) 进行总结日期范围的值。

代码:

startdate <- c(100,103,107)
enddate <- c(105,104,110)
billperiods <-data.frame(startdate,enddate);

要得到:

> billperiods
startdate enddate
1       100     105
2       103     104
3       107     110

代码:

weatherdate <- c(100:103,105:110)
hdd <- c(0,0,4,5,0,0,3,1,9,0)
cdd <- c(4,1,0,0,5,6,0,0,0,10)
weather <- data.frame(weatherdate,hdd,cdd)

要得到:

> weather
   weatherdate hdd cdd
1          100   0   4
2          101   0   1
3          102   4   0
4          103   5   0
5          105   0   5
6          106   0   6
7          107   3   0
8          108   1   0
9          109   9   0
10         110   0  10

注:weatherdate = 104缺失。我可能一天都没有天气。

我不知道如何到达:

> billweather
  startdate enddate sumhdd sumcdd
1       100     105      9     10
2       103     104      5      0
3       107     110     13     10

天气中的从到sumhdd的总和在哪里。hddstartdateenddatedata.frame

有任何想法吗?

4

3 回答 3

3

这是一个使用IRangesand的方法data.table。看起来,对于这个问题,这个答案似乎有点矫枉过正。IRanges但总的来说,我觉得用它来处理间隔很方便,它们可能多么简单。

# load packages
require(IRanges)
require(data.table)

# convert data.frames to data.tables
dt1 <- data.table(billperiods)
dt2 <- data.table(weather)

# construct Ranges to get overlaps
ir1 <- IRanges(dt1$startdate, dt1$enddate)
ir2 <- IRanges(dt2$weatherdate, width=1) # start = end

# find Overlaps
olaps <- findOverlaps(ir1, ir2)

# Hits of length 10
# queryLength: 3
# subjectLength: 10
#    queryHits subjectHits 
#     <integer>   <integer> 
#  1          1           1 
#  2          1           2 
#  3          1           3 
#  4          1           4 
#  5          1           5 
#  6          2           4 
#  7          3           7 
#  8          3           8 
#  9          3           9 
#  10         3          10 

# get billweather (final output)
billweather <- cbind(dt1[queryHits(olaps)], 
                dt2[subjectHits(olaps), 
                list(hdd, cdd)])[, list(sumhdd = sum(hdd), 
                sumcdd = sum(cdd)), 
                by=list(startdate, enddate)]

#    startdate enddate sumhdd sumcdd
# 1:       100     105      9     10
# 2:       103     104      5      0
# 3:       107     110     13     10

最后一行的代码分解:首先我构造 using queryHitssubjectHits然后从cbind中途data.table,我分组startdate, enddate并得到 sum ofhdd和 sum of cdd。为了更好地理解,如下所示单独查看该行更容易。

# split for easier understanding
billweather <- cbind(dt1[queryHits(olaps)], 
            dt2[subjectHits(olaps), 
            list(hdd, cdd)])
billweather <- billweather[, list(sumhdd = sum(hdd), 
            sumcdd = sum(cdd)), 
            by=list(startdate, enddate)]
于 2013-03-25T21:30:15.683 回答
1
billweather <- cbind(billperiods, 
                 t(apply(billperiods, 1, function(x) { 
                   colSums(weather[weather[, 1] %in% c(x[1]:x[2]), 2:3])
               })))
于 2013-03-25T21:16:07.627 回答
1
 cbind(billperiods, t(sapply(apply(billperiods, 1, function(x) 
     weather[weather$weatherdate >= x[1] & 
             weather$weatherdate <= x[2], c("hdd", "cdd")]), colSums)))

  startdate enddate hdd cdd
1       100     105   9  10
2       103     104   5   0
3       107     110  13  10
于 2013-03-25T21:20:26.177 回答