我有一个时间序列对象,其日常值始于19世纪,直到20世纪。那里有很多缺失的值。
我正在尝试计算每周的方法,这是一个最小的例子:
library(zoo)
library(xts)
# Create time series that starts in 19th century
T <- 100 # number of days
myTS <- xts(rnorm(T), as.Date(1:T, origin="1899-11-05"))
# Insert some missing values
myTS[4:7] <- NA
myTS[33:34] <- NA
myTS[67:87] <- NA
# Try calculating weekly means
weekData <- apply.weekly(myTS, colMeans, na.rm = TRUE)
仅返回上周的每周平均值:
1900-02-13 [有些价值]
我使用的是colMeans
而不只是mean
,因为我正在使用包含多个变量的更大数据集进行操作。
我想要所有周的意思。有人知道我做错了吗?
答案 0 :(得分:2)
根据您的评论更新,以使用周年组合:
library(zoo)
library(xts)
# Create time series that starts in 19th century
T <- 100 # number of days
myTS <- xts(rnorm(T), as.Date(1:T, origin="1899-11-05"))
# Insert some missing values
myTS[4:7] <- NA
myTS[33:34] <- NA
myTS[67:87] <- NA
# Let's use a flexible class
myTS <- data.frame(dates=index(myTS),v1=myTS[,1])
# Here's an easy way to transform dates to weeks
require(lubridate)
week_num <- week(myTS[,1])
year_num <- year(myTS[,1])
week_yr <- paste(week_num, year_num)
# Weekly means
aggregate(myTS$v1,by=list(week_yr),mean,na.rm=T)
Group.1 x 1 1 1900 0.05405322 2 2 1900 0.31981319 3 3 1900 NaN 4 4 1900 NaN 5 45 1899 0.85081053 6 46 1899 0.34064255 7 47 1899 0.02880424 8 48 1899 -0.34408119 9 49 1899 -0.38089026 10 5 1900 0.62292188 11 50 1899 -0.59666955 12 51 1899 0.57756987 13 52 1899 -0.41325485 14 53 1899 0.88013634 15 6 1900 0.01514668 16 7 1900 -0.50863942