Get startedGet started for free

Automatic import

Sometimes you'll need to input a couple of dates by hand using as.Date() but it's much more common to have a column of dates in a data file.

Some functions that read in data will automatically recognize and parse dates in a variety of formats. In particular the import functions, like read_csv(), in the readr package will recognize dates in a few common formats.

There is also the anytime() function in the anytime package whose sole goal is to automatically parse strings as dates regardless of the format.

Try them both out in this exercise.

This exercise is part of the course

Working with Dates and Times in R

View Course

Exercise instructions

  • Use read_csv() to read in the CSV file rversions.csv as releases.
  • Use str() to examine the structure of the date column. Notice it's already a Date object.
  • We've loaded anytime and created an object called sep_10_2009. Use the anytime() function to parse sep_10_2009.

Hands-on interactive exercise

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

# Load the readr package
library(readr)

# Use read_csv() to import rversions.csv
releases <- read_csv(___)

# Examine the structure of the date column
str(___)

# Load the anytime package
library(anytime)

# Various ways of writing Sep 10 2009
sep_10_2009 <- c("September 10 2009", "2009-09-10", "10 Sep 2009", "09-10-2009")

# Use anytime() to parse sep_10_2009
anytime(___)
Edit and Run Code