How to Convert Strings to Dates in R (With Examples)

The process of converting strings to dates in R is relatively simple. It can be done using the as.Date() function, which takes an argument of the string that needs to be converted, and the format in which it is written, and then returns a date object which can be used for further analysis. Examples of this process are provided in the body of this article.


Often when you import date and time data into R, values will be imported as strings.

The easiest way to convert strings to dates in R is with the as.Date() function, which uses the following syntax:

as.Date(x, format)

where:

  • x: A single string value or a vector of string values.
  • format: The format to use for the date. The default is YYYY-MM-DD.

You can use the ?strftime command in R to view a complete list of arguments available to use for the date format, but the most common ones include:

  • %d: Day of the month as decimal number (01-31)
  • %m: Month as decimal number (01-12)
  • %y: Year without century (e.g. 04)
  • %Y: Year with century (e.g. 2004)

This tutorial shows several examples of how to use the as.Date() function in practice.

Example 1: Convert a Single String to a Date

The following code shows how to convert a single string value to a date:

#create string value
x <- c("2021-07-24")

#convert string to date
new <- as.Date(x, format="%Y-%m-%d")
new

[1] "2021-07-24"

#check class of new variable
class(new)

[1] "Date"

Example 2: Convert a Vector of Strings to Dates

The following code shows how to convert a vector of strings to dates:

#create vector of strings
x <- c("2021-07-24", "2021-07-26", "2021-07-30")

#convert string to date
new <- as.Date(x, format="%Y-%m-%d")
new

[1] "2021-07-24" "2021-07-26" "2021-07-30"

#check class of new variable
class(new)

[1] "Date"

Example 3: Convert a Data Frame Column to Dates

The following code shows how to convert a data frame column of strings to dates:

#create data frame
df <- data.frame(day = c("2021-07-24", "2021-07-26", "2021-07-30"),
                 sales=c(22, 25, 28),
                 products=c(3, 6, 7))

#view structure of data frame
str(df)

'data.frame':	3 obs. of  3 variables:
 $ day     : Factor w/ 3 levels "2021-07-24","2021-07-26",..: 1 2 3
 $ sales   : num  22 25 28
 $ products: num  3 6 7

#convert day variable to date
df$day <- as.Date(df$day, format="%Y-%m-%d")

#view structure of new data frame
str(df)

'data.frame':	3 obs. of  3 variables:
 $ day     : Date, format: "2021-07-24" "2021-07-26" ...
 $ sales   : num  22 25 28
 $ products: num  3 6 7

Example 4: Convert Multiple Date Frame Columns to Dates

The following code shows how to convert multiple data frame column of strings to dates:

#create data frame
df <- data.frame(start = c("2021-07-24", "2021-07-26", "2021-07-30"),
                 end = c("2021-07-25", "2021-07-28", "2021-08-02"),
                 products=c(3, 6, 7))

#view structure of data frame
str(df)

'data.frame':	3 obs. of  3 variables:
 $ start   : Factor w/ 3 levels "2021-07-24","2021-07-26",..: 1 2 3
 $ end     : Factor w/ 3 levels "2021-07-25","2021-07-28",..: 1 2 3
 $ products: num  3 6 7

#convert start and end variables to date
df[,c('start', 'end')] = lapply(df[,c('start', 'end')],
                                function(x) as.Date(x, format="%Y-%m-%d"))

#view structure of new data frame
str(df)

'data.frame':	3 obs. of  3 variables:
 $ start   : Date, format: "2021-07-24" "2021-07-26" ...
 $ end     : Date, format: "2021-07-25" "2021-07-28" ...
 $ products: num  3 6 7

You can learn more about the lapply() function used in this example here.

The following tutorials offer additional information on how to work with dates in R:

How to Sort a Data Frame by Date in R
How to Extract Year from Date in R

x