Open In App

Convert a String into Date Format in R Programming – as.Date() Function

Improve
Improve
Like Article
Like
Save
Share
Report

as.Date() function in R Language is used to convert a string into date format.

Syntax: as.Date(x, format)

Parameters:
x: string variable
format: Format in which string is declared(%m/%d/%y)

Example 1:




# R program to convert string into date
  
# Creating a string vector
dates <- c("27 / 02 / 92"
    
# Conversion into date format 
result<-as.Date(dates, "% d/% m/% y"
    
# Print result 
print(result) 


Output:

[1] "1992-02-27"

Example 2:




# R program to convert string into date
  
# Creating a string vector
dates <- c("02 / 27 / 92", "02 / 27 / 92",  
           "01 / 14 / 92", "02 / 28 / 92",  
           "02 / 01 / 92"
    
# Conversion into date format 
result<-as.Date(dates, "% m/% d/% y"
    
# Print result 
print(result) 


Output:

[1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28" "1992-02-01"

Last Updated : 30 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads