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)

This exercise is part of the course

Working with Dates and Times in R

IntermediateSkill Level
4.5+
13 reviews

Learn the essentials of parsing, manipulating and computing with dates and times in R.

You now know most of what you need to tackle data that includes dates and times, but there are a few other problems you might encounter in practice. In this final chapter you'll learn a little more about these problems by returning to some of the earlier data examples and learning how to handle time zones, deal with times when you don't care about dates, parse dates quickly, and output dates and times.

Exercise 1: Time zonesExercise 2: Setting the timezoneExercise 3: Viewing in a timezoneExercise 4: Timezones in the weather dataExercise 5: Times without dates
Exercise 6: More on importing and exporting datetimesExercise 7: Fast parsing with fasttimeExercise 8: Fast parsing with lubridate::fast_strptimeExercise 9: Outputting pretty dates and timesExercise 10: Wrap-up

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free