Extracting for filtering and summarizing
Another reason to extract components is to help with filtering observations or creating summaries. For example, if you are only interested in observations made on weekdays (i.e. not on weekends) you could extract the weekdays then filter out weekends, e.g. wday(date) %in% 2:6
.
In the last exercise you saw that January, February and March were great times to visit Auckland for warm temperatures, but will you need a raincoat?
In this exercise you'll find out! You'll use the hourly data to calculate how many days in each month there was any rain during the day.
This exercise is part of the course
Working with Dates and Times in R
Exercise instructions
- Create new columns for the hour and month of the observation from
datetime
. Make sure you label the month. - Filter to just daytime observations, where the hour is greater than or equal to
8
and less than or equal to22
. - Group the observations first by
month
, then bydate
, and summarise by usingany()
on therainy
column. This results in one value per day - Summarise again by summing
any_rain
. This results in one value per month
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create new columns hour, month and rainy
akl_hourly <- akl_hourly %>%
mutate(
___ = ___(datetime),
___ = ___(datetime, ___),
rainy = weather == "Precipitation"
)
# Filter for hours between 8am and 10pm (inclusive)
akl_day <- akl_hourly %>%
filter(___, ___)
# Summarise for each date if there is any rain
rainy_days <- akl_day %>%
group_by(___, ___) %>%
summarise(
any_rain = ___(rainy)
)
# Summarise for each month, the number of days with rain
rainy_days %>%
summarise(
days_rainy = ___(any_rain)
)