2016-08-18 89 views
2

我有一個時間系列對象,每日價值觀開始於19世紀,並進入20世紀。那裏有很多缺失的值。從時間序列中計算每週均值與缺失數據在R

我試圖計算每週手段,這裏有一個小例子:

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年2月13日[一定的價值]

我使用colMeans而不是mean,因爲我正在使用包含多個變量的較大數據集。

我想所有星期的意思。有人知道我做錯了什麼嗎?

回答

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 
+0

對不起,我想是不是跨年week1,week2跨年等的平均值。但我希望分開這幾個星期的意思。所以week1_1899,week2_1899,...,week52_1900,week53_1900。 –

+0

@ ulima2_好吧,我剛剛更新它,以這種方式做到這一點 –

+1

奇妙的是,這樣做的工作! –