2017-02-06 24 views
1

我不熟悉時間序列分析和使用xts(以及一般的R),所以請原諒問題的基本性質。按時間和秒(因子)變量彙總xts對象中的數據

我想按時間範圍(例如幾個月)和第二個因子變量聚合數據。爲了說明我的問題,請參閱以下內容:

require(xts) 

# Create example df and convert it to an xts object 

date <- sample(seq(as.Date("2015/01/01"), as.Date("2016/12/31"), by="day"),12) 
colour <- c("Red", "Red", "Blue", "Blue", "Blue", "Blue", "Red", "Red", "Red", 
      "Red", "Blue", "Blue") 
value <- sample(1:10, 12, replace = TRUE) 
df <- cbind.data.frame(date, colour, value) 
df <- xts(df[,-1], order.by = df$date) 

這將創建一個樣本數據框,看起來像這樣:

  colour value 
2015-01-30 "Blue" "2" 
2015-03-15 "Blue" "9" 
2015-03-22 "Blue" "9" 
2015-08-13 "Blue" "5" 
2015-09-01 "Blue" "8" 
2015-11-10 "Red" "7" 
2016-04-26 "Blue" "2" 
2016-07-06 "Red" "9" 
2016-07-07 "Red" "6" 
2016-07-08 "Red" "2" 
2016-10-01 "Red" "6" 
2016-11-07 "Red" "2" 

我可以用概括的「值」變量:

apply.monthly(df$value, FUN = mean) 

給我:

   value 
2015-01-30 2.000000 
2015-03-22 9.000000 
2015-08-13 5.000000 
2015-09-01 8.000000 
2015-11-10 7.000000 
2016-04-26 2.000000 
2016-07-08 5.666667 
2016-10-01 6.000000 
2016-11-07 2.000000 

但我不太明白如何聚合(在這種情況下)顏色變量(我想每個月的顏色總和)。任何幫助將不勝感激。

回答

1

這個怎麼樣?

aggregate(as.numeric(df$value), 
      list(Month = format(index(df), "%Y-%m"), 
       Colour = df$colour), 
      mean) 

在回答下面的評論:

# You can replace the format with the following to get a year month object 
zoo::as.yearmon(index(df)) 

# Or you can covert to date by using the first of every month 
as.Date(paste(format(index(df), "%Y-%m"), "-01", sep = "")) 

你可能會在這裏找到更多的想法:Converting year and month ("yyyy-mm" format) to a date in R?

+0

非常有幫助@NBATrends Thi s解決方案有效,但會創建一個「Month」變量,它是「Character」類。有沒有辦法確保輸出保持爲「Date」類?我確實嘗試過'as.Date(df $ Month,format =「%Y-%m」),但無濟於事。感謝你的幫助。 – drgregmartin

2

如果你想通過顏色子集後XTS對象的工作,很容易工作與每個時間序列(顏色)分開在這樣的列表中:

df <- cbind.data.frame(date, colour, value) 

> class(df) 
#[1] "data.frame" 

# data.frame split (not xts split) to separate data by colour in a list object: 
l_out <- split(df, colour) 

> class(l_out[[1]]) 
[1] "data.frame" 

mthly_mean <- function(x) { 
    apply.monthly(as.xts(x[, "value"], x[, "date"]), mean)  
} 

# Each element in the list is an xts object (not a data.frame) containing the mean of the data for each month: 
l_res <- lapply(l_out, FUN = mthly_mean) 
# or more succinctly: 
# l_res <- lapply(l_out, FUN = function(x) apply.monthly(as.xts(x[, "value"], x[, "date"]), mean)) 

> l_res 
# $Blue 
# [,1] 
# 2015-01-15 8.0 
# 2015-07-21 4.5 
# 2016-01-28 5.0 
# 2016-04-28 4.0 
# 2016-05-08 2.0 
# 
# $Red 
# [,1] 
# 2015-11-30 3 
# 2016-01-18 7 
# 2016-02-25 5 
# 2016-04-17 1 
# 2016-05-23 6 
# 2016-07-14 5 

> class(l_res[[1]]) 
[1] "xts" "zoo"