0

I have a daily data for 31 years from 1984 to 2014. I would like to compute the daily average for 31 years for the variables

date    Min_daily   Max_daily   Rain_daily 
01-01-1984  18.8    3.6          0  
02-01-1984  20.2    3.8          0  
03-01-1984  19      4.2          0
.
.
.
30-12-2014  19.4    2.2          0
31-12-2014  18.5    7            0
01-01-2015  17.2    7.2          0

How to do it in R software?

UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • What do you mean by daily average? You can't compute an average of a group of values using only the min and max values. For example, `mean(c(1, 2, 300))` and `mean(c(1,200, 300))` are not equal. – IceCreamToucan Jan 22 '18 at 16:43
  • 1
    @RobJensen the OP wants to average each day over 31 years for 366 different averaged mins, maxes, and rains. – AdamO Jan 22 '18 at 17:03
  • Dataset is having 31 (years) *365.25 (days per year) ~11,323 rows and I want to have the average of 31 years daily values which will give 365 rows only. – UseR10085 Jan 22 '18 at 17:05
  • @BappaDas it will give 366 days. – AdamO Jan 22 '18 at 17:06
  • Please use `dput(my_data_frame)` (or even just a subset of your data), then include the output in your question. That way, others are more likely to give you the right solution. – Nathan Werth Jan 22 '18 at 19:30

1 Answers1

1

Create a new variable

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

And use your favorite mean aggregator, in base R tapply or aggregate work a treat.

Example: aggregate(cbind(Min_daily, Max_daily, Rain_daily) ~ day, data=yourData)

AdamO
  • 4,283
  • 1
  • 27
  • 39
  • format(yourData$date, format='%m-%d') is returning the day in "1984-01-01" "1984-01-02" "1984-01-03" "1984-01-04" "1984-01-05" format. I am not getting the desired results. I am using the following codes: data$day <- format(data$date, format='%m-%d') aggregate(cbind(Min_daily, Max_daily, Rain_daily) ~ day, data=data,FUN=mean) – UseR10085 Jan 22 '18 at 17:17
  • @BappaDas is "date" in fact a date? If not, make it so or just use `substr`. – AdamO Jan 22 '18 at 17:36
  • can you please elaborate the code please so that I get the desired results – UseR10085 Jan 22 '18 at 18:11
  • 1
    Use `as.Date(data$date, format='%d-%m-%Y')` to transform the vector into a date. Or use `substr(data$date, 1, 5)` – AdamO Jan 22 '18 at 18:15
  • Thanks a lot, @AdamO, the as.Date(data$date, format='%d-%m-%Y') has done the trick for me. – UseR10085 Jan 23 '18 at 02:53