SnowFrog
SnowFrog

Reputation: 1162

Calculating a daily mean in R

Say I have the following matrix:

x1 = 1:288
x2 = matrix(x1,nrow=96,ncol=3)

Is there an easy way to get the mean of rows 1:24,25:48,49:72,73:96 for column 2?

Basically I have a one year time series and I have to average some data every 24 hours.

Upvotes: 7

Views: 20963

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 269461

1) ts. Since this is a regularly spaced time series, convert it to a ts series and then aggregate it from frequency 24 to frequency 1:

aggregate(ts(x2[, 2], freq = 24), 1, mean)

giving:

Time Series:
Start = 1
End = 4
Frequency = 1
[1] 108.5 132.5 156.5 180.5

2) zoo. Here it is using zoo. The zoo package can also handle irregularly spaced series (if we needed to extend this). Below day.hour is the day number (1, 2, 3, 4) plus the hour as a fraction of the day so that floor(day.hour) is just the day number:

library(zoo)

day.hour <- seq(1, length = length(x2[, 2]), by = 1/24)
z <- zoo(x2[, 2], day.hour)
aggregate(z, floor, mean)
##          1     2     3     4 
##      108.5 132.5 156.5 180.5

If zz is the output from aggregate then coredata(zz) and time(zz) are the values and times, respectively, as ordinary vectors.

Upvotes: 6

Matti Pastell
Matti Pastell

Reputation: 9283

Quite compact and computationally fast way of doing this is to reshape the vector into a suitable matrix and calculating the column means.

colMeans(matrix(x2[,2],nrow=24))

Upvotes: 4

Joris Meys
Joris Meys

Reputation: 108523

There is.

Suppose we have the days :

Days <- rep(1:4,each=24)

you could do easily

tapply(x2[,2],Days,mean)

If you have a dataframe with a Date variable, you can use that one. You can do that for all variables at once, using aggregate :

x2 <- as.data.frame(cbind(x2,Days))
aggregate(x2[,1:3],by=list(Days),mean)

Take a look at the help files of these functions to start with. Also do a search here, there are quite some other interesting answers on this problem :

PS : If you're going to do a lot of timeseries, you should take a look at the zoo package (on CRAN : http://cran.r-project.org/web/packages/zoo/index.html )

Upvotes: 10

Related Questions