私は本当に奇妙な問題を抱えています...私はto.weekly
andto.period
関数を使用して毎日のxts
オブジェクトを毎週のデータに変換しています。ほとんどの場合、週末の日付は金曜日として取得されます(day.of.week
関数は5を返します)(たとえば"2010-01-08"
、"2011-02-11"
)が、金曜日以外のものを取得する場合があります(土曜日/日曜日/木曜日など)。
私は試しましたがto.weekly
、to.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)}
これにより、金曜日以外の日付が返されます。time(to.weekly(test.xts))[dayofweek(time(to.weekly(test.xts))) != 5]