Rounding with the weather data

When is rounding useful? In a lot of the same situations extracting date components is useful. The advantage of rounding over extracting is that it maintains the context of the unit. For example, extracting the hour gives you the hour the datetime occurred, but you lose the day that hour occurred on (unless you extract that too), on the other hand, rounding to the nearest hour maintains the day, month and year.

As an example you'll explore how many observations per hour there really are in the hourly Auckland weather data.

This exercise is part of the course

Working with Dates and Times in R

View Course

Exercise instructions

  • Create a new column called day_hour that is datetime rounded down to the nearest hour.
  • Use count() on day_hour to count how many observations there are in each hour. What looks like the most common value?
  • Extend the pipeline, so that after counting, you filter for observations where n is not equal to 2.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create day_hour, datetime rounded down to hour
akl_hourly <- akl_hourly %>%
  mutate(
    day_hour = ___(datetime, unit = ___)
  )

# Count observations per hour  
akl_hourly %>% 
  count(___) 

# Find day_hours with n != 2  
akl_hourly %>% 
  count(___) %>%
  filter(___) %>% 
  arrange(desc(n))