Get Started

Times without dates

For this entire course, if you've ever had a time, it's always had an accompanying date, i.e. a datetime. But sometimes you just have a time without a date.

If you find yourself in this situation, the hms package provides an hms class of object for holding times without dates, and the best place to start would be with as.hms().

In fact, you've already seen an object of the hms class, but I didn't point it out to you. Take a look in this exercise.

This is a part of the course

“Working with Dates and Times in R”

View Course

Exercise instructions

  • Use read_csv() to read in "akl_weather_hourly_2016.csv". readr knows about the hms class, so if it comes across something that looks like a time it will use it.
  • In this case the time column has been parsed as a time without a date. Take a look at the structure of the time column to verify it has the class hms.
  • hms objects print like times should. Take a look by examining the head of the time column.
  • You can use hms objects in plots too. Create a plot with time on the x-axis, temperature on the y-axis, with lines grouped by date.

Hands-on interactive exercise

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

# Import auckland hourly data 
akl_hourly <- read_csv(___)

# Examine structure of time column
str(___)

# Examine head of time column
head(___)

# A plot using just time
ggplot(akl_hourly, aes(x = ___, y = ___)) +
  geom_line(aes(group = make_date(year, month, mday)), alpha = 0.2)
Edit and Run Code