[R] How to monthly,daily,yearly average

Gabor Grothendieck ggrothendieck at gmail.com
Wed Mar 11 15:03:10 CET 2009


Here is an example.  See the three vignettes in zoo:
vignette("zoo")
and R News 4/1 for info on dates.

# assume this input

Lines <- "Date Time Value
01/01/08 00:00:00 1
01/01/08 00:30:00 2
01/01/08 01:00:00 3
01/01/08 01:30:00 4
01/01/08 02:00:00 5
01/01/08 02:30:00 6
01/01/08 03:00:00 7
01/01/08 03:30:00 8
01/01/08 04:00:00 9"

# read in the data, convert it to zoo, aggregate it and plot

library(zoo)
library(chron)

# DF <- read.table("myfile.dat", header = TRUE, as.is = TRUE)
DF <- read.table(textConnection(Lines), header = TRUE, as.is = TRUE)

# next line may need to specify formats, if different
z <- zoo(DF$Value, chron(DF$Date, DF$Time))

# hourly aggregation
z.hr <- aggregate(z, trunc(time(z), "01:00:00"), mean)
plot(z.hr, type = "o")

# daily aggregation
z.day <- aggregate(z, trunc, mean)
plot(z.day, type = "o")

# monthly aggregation
z.mo <- aggregate(z, as.yearmon, mean)
plot(z.mo, type = "o")

# yearly aggregation
z.yr <- aggregate(z.mo, floor, mean)
plot(z.yr, type = "o")


On Wed, Mar 11, 2009 at 9:25 AM, Qianfeng Li <qflichem at yahoo.com> wrote:
> Sorry, this is my first time to post.
>
> I have a big data set: first colume is date (ex: 2008-2-150, the second is time (10:30:00), and the following columes are variaty measurement data. Every 30 min, I have one data.
>
> I want to find an effecient way to calculate the hourly, daily, monthly and yearly average, and plot them, and eventually use these average data to do further analysis.
>
> Thanks!
> Jeff
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list