How can strings be converted to dates in R? Can you provide some examples?

Strings in R can be converted to dates using the “as.Date()” function. This function takes in the string value as the first argument and the desired date format as the second argument. The string must be in a specific format that can be recognized as a date, such as “YYYY-MM-DD”.

For example, if we have a string “2019-12-25”, we can convert it to a date format using the command “as.Date(“2019-12-25”, “%Y-%m-%d”)”. This will output the date “2019-12-25” in the format specified.

Another example is if we have a string “Jan 1 2020”, we can convert it to a date format using the command “as.Date(“Jan 1 2020”, “%b %d %Y”)”. This will output the date “2020-01-01” in the format specified.

In summary, the “as.Date()” function in R is a useful tool for converting strings to dates, as long as the string follows a recognizable date format.

Convert Strings to Dates in R (With Examples)


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.

Additional Resources

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