LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Working with Dates and Times in R

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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(___)
Code bearbeiten und ausführen