-3

I need some help with R timeseries. I have daily values of temperature for a 30 year period = 365*30 days = 10950 days (if bisiest years are not considered) . I want to create a "daily climatology", that is, the average of each (the 30 values) 1st of January, 2nd of January, etc.., to create a timesieres with 365 values. Could anyone help me with this topic?. Thanks in advance.

  • Could you show the format of your date column/provide a sample of your data? – guscht Oct 16 '17 at 15:30
  • 2
    Welcome to SO! Questions like this *really benefit* from providing sample data (no need for all of it) and relevant code you've already tried. The point is to make it easy for us (as potential answerers) to copy some text from your question, run it in my own R session, tweak the code or write new code, and paste back as an answer. Good references for you (please read them!) are [reproducible questions](https://stackoverflow.com/questions/5963269/) and [SO help: minimal, verifiable examples](https://stackoverflow.com/help/mcve). Please read them and edit your question with more info. – r2evans Oct 16 '17 at 15:35
  • It may be easier to do this by converting the time series to a data frame, then computing means over aggregations of days of the year as integers (which you can return for dates using `lubridate::yday`). There are lots ways to do that aggregating and averaging. – ulfelder Oct 16 '17 at 16:17
  • Does this answer your question? [Daily average calculation from multiple year daily weather data?](https://stackoverflow.com/questions/48386324/daily-average-calculation-from-multiple-year-daily-weather-data) – UseR10085 Jul 09 '20 at 04:43

2 Answers2

3

Something like this with dplyr + lubridate:

library(dplyr)
library(lubridate)
df %>%
  group_by(month = month(date), day = day(date)) %>%
  summarize(avg_value = mean(value)) %>%
  pull(avg_value) %>%
  ts() %>%
  plot(ylab = "avg_value")

Result:

> df %>%
+   group_by(month = month(date), day = day(date)) %>%
+   summarize(avg_value = mean(value))
# A tibble: 366 x 3
# Groups:   month [?]
   month   day   avg_value
   <dbl> <int>       <dbl>
 1     1     1  0.19750444
 2     1     2  0.30492408
 3     1     3  0.16760465
 4     1     4 -0.09357058
 5     1     5  0.10606383
 6     1     6 -0.14456526
 7     1     7  0.23384988
 8     1     8 -0.11987095
 9     1     9 -0.01166687
10     1    10 -0.08134161
# ... with 356 more rows

enter image description here

Data:

df = data.frame(date = seq.Date(as.Date("1970-1-1"), as.Date("2000-12-31"), "days"),
                value = rnorm(length(seq.Date(as.Date("1970-1-1"), as.Date("2000-12-31"), "days"))))
acylam
  • 18,231
  • 5
  • 36
  • 45
  • not sure why this is downvoted. If you think there is something wrong with this solution, feel free to leave a comment so I can improve my answer. – acylam Oct 16 '17 at 17:18
0

I had the same probleme to solve and found an answer here: Daily average calculation from multiple year daily weather data?

It took some time for me to understand and reorder all the comments beacause there was no straight code.

So here I give an complete example based on the link above.

As an example 3 years of random precipitation and temperature data:

test_data <- data.frame("date"= seq(from = as.Date("1990/1/1"), to = as.Date("1992/12/31"), by = "day"),"prec" =runif(1096, 0, 10),"temp" = runif(1096, 0, 10))

Next step is to ad a new column with a variable on which base the average will be calculated. One Day in this example:

test_data$day <- format(test_data$date, format='%m-%d')

In this column everyday of a year appears 3 times because of the 3 years. So we can calculate the mean for every day:

test_data_daily_mean <- aggregate(cbind(prec, temp) ~ (day), data=test_data, FUN=mean)

Hint: For this solution the date column really has to have dates inside. Otherwise you have to format them to R dates like this:

as.Date(data$date, format='%d-%m-%Y') 

This answer is a little late, but maybe it helps someone else!

Rob
  • 11
  • 2