Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)
I'm trying to get the total rainfall that occurs between, for example, December and April of the following year. Another complexity is that the day can change, say Dec-15 to March-15.

I know that we can use aggregate or group_by if I don't have to go across the years. But I don't know how to solve this year's crossing problem.

1 Answer

0 votes
by (108k points)

Let say that 'x' is a Date class object or vector of Dates and 'ym' is the corresponding year/months, 'start_year' is the like years for the start of the season, and the 'n_season' is TRUE if the corresponding date is within Dec-Mar:

library(zoo)

x <- as.Date("2020-02-03")  # test data

ym <- as.yearmon(x)

start_year <- as.integer(ym - 3/12)

in_season <- cycle(ym) %in% c(12, 1:3)

You can subset to those dates in R programming, for which in_season is TRUE and then perform the aggregate() by the start_year.

Browse Categories

...