2011-09-09 23 views
2

我有一個非常奇怪的問題...我使用to.weeklyto.period函數將每天的xts對象轉換爲每週數據。在大多數情況下,我會將週末結束日期定爲星期五(day.of.week函數將返回5)(例如,"2010-01-08","2011-02-11"),但有幾種情況我除了週五(週六/週日/週四/ )在'xts'包中使用'to.weekly'函數錯誤的週末結束日期

我試過to.weeklyto.period(x, period = 'weeks'),兩者都返回相同的問題。

這是怎麼發生的?有沒有解決這個問題?

謝謝!

[編輯:下面的示例]

test.dates <- as.Date(c("2010-04-27","2010-04-28","2010-04-29","2010-04-30","2010-05-03","2010-05-04","2010-05-05","2010-05-06","2010-05-07","2010-05-10","2010-05-11","2010-05-12","2010-05-13","2010-05-14","2010-05-17","2010-05-18","2010-05-19","2010-05-20","2010-05-21","2010-05-22","2010-05-24","2010-05-25","2010-05-26","2010-05-27","2010-05-28","2010-06-01","2010-06-02","2010-06-03","2010-06-04")) 

test.data <- rnorm(length(test.dates),mean=1,sd=2) 

test.xts <- xts(x=test.data,order.by=test.dates) 

#Function that takes in a vector of zoo/xts objects (e.g. "2010-01-08") and returns the day of the week for each 
dayofweek <- function(x) { 
placeholder <- vector("list",length=length(x)) 
names(placeholder) <- x 

for(i in 1:length(x)) {placeholder[[i]] <- month.day.year(x[i])} 
placeholder2 <- rep(NA,times=length(x)) 

for(i in 1:length(x)) {placeholder2[i] <- day.of.week(placeholder[[i]][[1]],placeholder[[i]][[2]],placeholder[[i]][[3]])} 
return(placeholder2)} 

這將返回不在週五的日期(S):time(to.weekly(test.xts))[dayofweek(time(to.weekly(test.xts))) != 5]

+2

請提供一個最小可重現的例子。 –

+0

@Dirk,上面包含的示例!謝謝! – Ray

+0

@Ray寶請包括您使用的軟件包。 'day.of.week'和'month.day.year'不是基本R中的函數(也不是'xts',但我知道它來自哪個包) – Zach

回答

2

你有2個問題,你的例子:

  1. 你的dayofweek函數有點麻煩,並且可能結果不正確。
  2. 您的示例日期缺少一些日期,例如2010年5月23日。

這裏是你的代碼的清理後的版本:

library(xts) 
test.dates <- as.Date(c("2010-04-27","2010-04-28","2010-04-29","2010-04-30","2010-05-03","2010-05-04","2010-05-05","2010-05-06","2010-05-07","2010-05-10","2010-05-11","2010-05-12","2010-05-13","2010-05-14","2010-05-17","2010-05-18","2010-05-19","2010-05-20","2010-05-21","2010-05-22","2010-05-24","2010-05-25","2010-05-26","2010-05-27","2010-05-28","2010-06-01","2010-06-02","2010-06-03","2010-06-04")) 
test.data <- rnorm(length(test.dates),mean=1,sd=2) 
test.xts <- xts(x=test.data,order.by=test.dates) 
test.weekly <- to.weekly(test.xts) 

library(lubridate) 
test.weekly[wday(test.weekly, label = TRUE, abbr = TRUE) != "Fri"] 

此函數的唯一結果是

  test.xts.Open test.xts.High test.xts.Low test.xts.Close 
2010-05-22  -1.705749  1.273982 -2.084203  -1.502611 

當然,問題是,本週05-23-2010結束,但該日期不在時間序列中。因此,to.weekly使用下一個最接近的日期作爲終點,即05-22-2010。這是你問題的根源。

這是一個比較好的例子,它沒有顯示to.weekly函數的問題。

library(lubridate); library(xts) 
test.dates <- seq(as.Date("1900-01-01"),as.Date("2011-10-01"),by='days') 
test.dates <- test.dates[wday(test.dates)!=1 & wday(test.dates)!=7] #Remove weekends 
test.data <- rnorm(length(test.dates),mean=1,sd=2) 
test.xts <- xts(x=test.data,order.by=test.dates) 
test.weekly <- to.weekly(test.xts) 
test.weekly[wday(test.weekly, label = TRUE, abbr = TRUE) != "Fri"] 
+1

我認爲即使在不存在的情況下,「週末到週末」總是返回星期五在數據向量中加上星期五標記的日期 - 但現在我意識到情況並非如此。 – Ray

+1

@ Ray Bao:這解釋了你的問題。我很高興你解決了你的問題。如果我的答案足以讓您找到解決方案,請點擊複選框以「接受」它。 – Zach

相關問題