我有一个时间序列对象,其每日值从 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 [some value]
我使用 colMeans
而不仅仅是 mean
,因为我正在处理包含多个变量的更大数据集。
我想要所有几周的平均值。有人知道我做错了什么吗?
最佳答案
根据您的评论进行更新,以使用周年组合:
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
关于r - 根据 R 中缺失数据的时间序列计算周平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39018982/