Reputation: 1
I am working with a 30 year weather data and I have a list of temperature values:
tempC = c(-2,-3,-2,.......-5)
These are temperature values of 11322 days
(number of days in 30 years). Now, I want to find out how many days were there every year where temperature > 30degC
.
Year1 = 2001, Year30 = 2031
It's been driving me crazy as to how I would take into account the leap years and how can I get the correct answer.
Would highly appreciate any help! Thanks~
Upvotes: 0
Views: 138
Reputation: 6639
Assuming you have the dates and can get the year, the syntax for it using dplyr would be:
library(dplyr)
df %>% group_by(year) %>% filter(tempC > 30)
where df
is your dataframe, year
is the column which has values like 2001,..,2030 and finally, tempC
is the column with the values of temperature.
Look below to know how to extract the year from the dates, and to generate dates between Jan 1, 2001 to Dec 31, 2031 if required.
tempC date
1 30 2012-12-29
2 27 2012-12-30
3 41 2012-12-31
4 22 2013-01-01
5 32 2013-01-02
6 44 2013-01-03
7 33 2013-01-04
8 22 2013-01-05
Getting year column using lubridate
:
library(lubridate)
df$year <- year(ymd(df$date))
You now get:
tempC date year
1 30 2012-12-29 2012
2 27 2012-12-30 2012
3 41 2012-12-31 2012
4 22 2013-01-01 2013
5 32 2013-01-02 2013
6 44 2013-01-03 2013
7 33 2013-01-04 2013
8 22 2013-01-05 2013
Running the above dplyr code solution, final output:
tempC date year
<dbl> <date> <dbl>
1 41 2012-12-31 2012
2 32 2013-01-02 2013
3 44 2013-01-03 2013
4 33 2013-01-04 2013
df$date <- seq(as.Date('2001-01-01'), by = 'day', len = 11322)
This will create dates between 1st Jan, 2001 until 31st Dec, 2031.
Upvotes: 1