[英]Aggregating, restructuring hourly time series data in R
我在R的數據框中有一年的每小時數據價值:
> str(df.MHwind_load) # compactly displays structure of data frame
'data.frame': 8760 obs. of 6 variables:
$ Date : Factor w/ 365 levels "2010-04-01","2010-04-02",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Time..HRs. : int 1 2 3 4 5 6 7 8 9 10 ...
$ Hour.of.Year : int 1 2 3 4 5 6 7 8 9 10 ...
$ Wind.MW : int 375 492 483 476 486 512 421 396 456 453 ...
$ MSEDCL.Demand: int 13293 13140 12806 12891 13113 13802 14186 14104 14117 14462 ...
$ Net.Load : int 12918 12648 12323 12415 12627 13290 13765 13708 13661 14009 ...
在保留小時結構的同時,我想知道如何提取
我嘗試使用“切割”但沒有結果,並且在網上瀏覽后認為“潤滑”可能可以,但沒有找到合適的例子。 非常感謝您在此問題上的幫助。
編輯:數據框中的數據示例如下:
Date Hour.of.Year Wind.MW datetime
1 2010-04-01 1 375 2010-04-01 00:00:00
2 2010-04-01 2 492 2010-04-01 01:00:00
3 2010-04-01 3 483 2010-04-01 02:00:00
4 2010-04-01 4 476 2010-04-01 03:00:00
5 2010-04-01 5 486 2010-04-01 04:00:00
6 2010-04-01 6 512 2010-04-01 05:00:00
7 2010-04-01 7 421 2010-04-01 06:00:00
8 2010-04-01 8 396 2010-04-01 07:00:00
9 2010-04-01 9 456 2010-04-01 08:00:00
10 2010-04-01 10 453 2010-04-01 09:00:00
.. .. ... .......... ........
8758 2011-03-31 8758 302 2011-03-31 21:00:00
8759 2011-03-31 8759 378 2011-03-31 22:00:00
8760 2011-03-31 8760 356 2011-03-31 23:00:00
編輯:我想對同一數據集執行其他基於時間的操作1.對所有數據點進行逐小時平均,即一年中每天第一個小時中所有值的平均值。 輸出將是整個年度(24個時間點)的“每小時配置文件”。2.每周和每月執行相同的操作,即分別獲取52和12個每小時配置文件。3.執行季節性平均值,例如6月至9月
將日期轉換為lubridate可以理解的格式,然后分別使用month
, mday
和wday
函數。
假設您有一個data.frame,其時間存儲在Date
列中,那么您的問題的答案將是:
###dummy data.frame
df <- data.frame(Date=c("2012-01-01","2012-02-15","2012-03-01","2012-04-01"),a=1:4)
##1. Select rows for particular month
subset(df,month(Date)==1)
##2a. Select the first day of each month
subset(df,mday(Date)==1)
##2b. Select the first week of each month
##get the week numbers which have the first day of the month
wkd <- subset(week(df$Date),mday(df$Date)==1)
##select the weeks with particular numbers
subset(df,week(Date) %in% wkd)
##3. Select all mondays
subset(df,wday(Date)==1)
Date
表示形式: as.Date(df.MHwind_load$Date)
weekdays
以獲取標有星期幾的新因子 months
以獲取一個標記為month的新因子 years
變量(請參見下文)。 現在,使用這些元素的相關組合對數據幀進行subset
設置。 步驟2。獲得任務3的答案。步驟3和步驟4。獲得任務1的任務。任務2可能需要R的一行或兩行。或者只選擇對應於一個月中所有星期一和星期一的行。調用unique
,或者在結果上duplicated
其alter-ego。
為了讓你走...
newdf <- df.MHwind_load ## build an augmented data set
newdf$d <- as.Date(newdf$Date)
newdf$month <- months(newdf$d)
newdf$day <- weekdays(newdf$d)
## for some reason R has no years function. Here's one
years <- function(x){ format(as.Date(x), format = "%Y") }
newdf$year <- years(newdf$d)
# get observations from January to March of every year
subset(newdf, month %*% in c('January', 'February', 'March'))
# get all Monday observations
subset(newdf, day == 'Monday')
# get all Mondays in 1999
subset(newdf, day == 'Monday' & year == '1999')
# slightly fancier: _first_ Monday of each month
# get the first weeks
first.week.of.month <- !duplicated(cbind(newdf$month, newdf$day))
# now pull out the mondays
subset(newdf, first.monday.of.month & day=='Monday')
由於您不是在詢問數據的時間(小時)部分,因此最好將數據存儲為Date對象。 否則,您可能會對chron感興趣,它還具有一些便捷功能,如下所示。
關於Conjugate Prior的答案,您應該將日期數據存儲為Date對象。 由於您的數據已經遵循默認格式('yyyy-mm-dd'),因此您可以在其上調用as.Date。 否則,您將必須指定您的字符串格式。 我還要在您的因素上使用as.character,以確保您不會內聯錯誤。 我知道出於這個原因我已經遇到了因素因素(可能在當前版本中已解決)。
df.MHwind_load <- transform(df.MHwind_load, Date = as.Date(as.character(Date)))
現在,您可以很好地創建包裝函數,以提取所需的信息。 您可以像上面一樣使用transform來簡單地添加代表月,日,年等的列,然后在邏輯上對其進行子集化。 或者,您可以執行以下操作:
getMonth <- function(x, mo) { # This function assumes w/in single year vector
isMonth <- month(x) %in% mo # Boolean of matching months
return(x[which(isMonth)] # Return vector of matching months
} # end function
或者,簡寫形式
getMonth <- function(x, mo) x[month(x) %in% mo]
這只是在存儲該信息(轉換幀)或在需要時對其進行處理(使用訪問器方法)之間的權衡。
例如,一個更復雜的過程是您需要一個月的第一天。 但是,這並不完全困難。 下面是一個將返回所有這些值的函數,但僅對給定月份的排序后的值向量進行子集並采用它們的第一個就相當簡單。
getFirstDay <- function(x, mo) {
isMonth <- months(x) %in% mo
x <- sort(x[isMonth]) # Look at only those in the desired month.
# Sort them by date. We only want the first day.
nFirsts <- rle(as.numeric(x))$len[1] # Returns length of 1st days
return(x[seq(nFirsts)])
} # end function
更容易的選擇是
getFirstDayOnly <- function(x, mo) {sort(x[months(x) %in% mo])[1]}
由於您沒有提供任何數據樣本,因此我沒有為它們提供原型,但這是可以幫助您獲取所需信息的一種方法。 由您自己決定如何將它們放入您的工作流程中。 例如,假設您要獲得給定年份每個月的第一天(假設我們只查看一年;您可以創建包裝器或將向量預先處理為一年)。
# Return a vector of first days for each month
df <- transform(df, date = as.Date(as.character(date)))
sapply(unique(months(df$date)), # Iterate through months in Dates
function(month) {getFirstDayOnly(df$date, month)})
上面的內容也可以設計為使用其他訪問器功能的單獨的便捷功能。 這樣,您可以創建一系列直接而簡潔的方法來獲取所需的信息。 然后,您只需將它們組合在一起即可創建非常簡單易懂的函數,您可以在腳本中使用這些函數,從而以最有效的方式使您精確地掌握所需的內容。
您應該能夠使用上面的示例來弄清楚如何為其他包裝器提供原型,以訪問所需的日期信息。 如果您需要這些方面的幫助,請隨時在評論中提問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.