Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating a daily mean in R

Tags:

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.

like image 864
SnowFrog Avatar asked Jan 13 '11 17:01

SnowFrog


2 Answers

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 :

  • Aggregating daily content
  • Compute means of a group by factor

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 )

like image 197
Joris Meys Avatar answered Nov 05 '22 20:11

Joris Meys


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.

like image 33
G. Grothendieck Avatar answered Nov 05 '22 22:11

G. Grothendieck