I am a new user of "R", and I couldn't find a good solution to solve it. I got a timeseries in the following format:
>dates temperature depth salinity
>12/03/2012 11:26 9.7533 0.48073 37.607
>12/03/2012 11:56 9.6673 0.33281 37.662
>12/03/2012 12:26 9.6673 0.33281 37.672
I have an irregular frequency for variable measurements, done every 15 or every 30 minutes depending on the period. I would like to calculate annual, monthly and daily averages for each of my variables, whatever the number of data in a day/month/year is. I read a lot of things about the packages zoo, timeseries, xts, etc. but I can't get a clear vision of what I nead (maybe cause I'm not skilled enough with R...).
I hope my post is clear, don't hesitate to tell me if it's not.
The package
hydroTSM
holds a multiple functions to creat annual and other summaries:I'd add the day, month and year into the data frame and then use
aggregate()
.First convert your
date
column into a POSIXct objet:Then get the date (e.g. 12/03/2012) into a column called
Date
, try this:Next, aggregate by the date:
Similarly, you can get the month into a column (let's call it
M
for month), and then...or if you want year-month
If you have any NA values in your data, you may need to account for those:
Finally, if you want to average by week, you can do that as well. First generate the week number, and then use
aggregate()
again.This version of week number defines week 1 as being the week with the first Monday of the year. The weeks are from Monday to Sunday.
Yet, another method using plyr:
Convert your data to an xts object, then use
apply.daily
et al to calculate whatever values you want.