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
Exercise instructions
- Use
read_csv()
to read in the CSV filerversions.csv
asreleases
. - Use
str()
to examine the structure of thedate
column. Notice it's already aDate
object. - We've loaded
anytime
and created an object calledsep_10_2009
. Use theanytime()
function to parsesep_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(___)