Calculating average rainfall for time period of 30 year considering leap year

Dear all,

I would like to calculate the average precipitation per year for the period 1991 to 2020.
Now, of course, I would like the annual precipitation to be divided by 366 for leap years or by 365 for no leap years.
If the year is a leap year, then the annual precipitation is divided by 366.
if not a leap year, then annual precipitation divided by 365.

I would be very grateful for your help. Many thanks in advance and best regards,
Liza

r_leap_year

Here is a simple example.

library(lubridate)
DF <- data.frame(Year=seq(1991,2000,1),Rain=runif(10,40,150))
DF
   Year      Rain
1  1991  87.86195
2  1992 133.48462
3  1993  49.38801
4  1994 145.06642
5  1995 110.06277
6  1996 136.55426
7  1997 111.44178
8  1998  95.93573
9  1999 130.38815
10 2000  63.17043
DF$AvgRain <- ifelse(leap_year(DF$Year),DF$Rain/366,DF$Rain/365)
DF
   Year      Rain   AvgRain
1  1991  87.86195 0.2407177
2  1992 133.48462 0.3647121
3  1993  49.38801 0.1353096
4  1994 145.06642 0.3974422
5  1995 110.06277 0.3015418
6  1996 136.55426 0.3730991
7  1997 111.44178 0.3053199
8  1998  95.93573 0.2628376
9  1999 130.38815 0.3572278
10 2000  63.17043 0.1725968
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.