【发布时间】:2013-03-25 21:03:11
【问题描述】:
我正在从 SAS 迁移到 R。我需要帮助来弄清楚如何汇总日期范围内的天气数据。在 SAS 中,我采用日期范围,使用数据步骤为范围内的每个日期(startdate、enddate、date)创建记录,与天气合并然后汇总(VAR hdd cdd; CLASS =startdate enddate sum=) 对日期范围的值求和。
R 代码:
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
R 代码:
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 是hdd 在天气data.frame 中从startdate 到enddate 的总和。
有什么想法吗?
【问题讨论】: