我正在從SAS遷移到R.我需要幫助瞭解如何總結日期範圍的天氣數據。在SAS中,我使用日期範圍,使用數據步驟爲範圍內的每個日期創建記錄(使用startdate
,enddate
,date
),合併天氣然後彙總(VAR hdd cdd; CLASS = startdate enddate sum =)總結日期範圍的值。使用一個數據幀對R中另一個數據幀的數據範圍求和
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
的從startdate
到enddate
天氣data.frame
。
任何想法?
感謝您的快速響應!我試圖對付更大的數據幀(12356行),它花了7.89秒,結果很好!我很驚訝人們迴應的速度有多快。這是我第一次在這裏問一個問題。 – 2013-03-25 22:08:47