as.Date Function in R (Example)
In this tutorial, I’ll illustrate how to convert a character to the Date class using the as.Date function in R.
The tutorial will contain the following content:
Let’s take a look at some R codes in action.
Definition & Basic R Syntax of as.Date Function
Definition: The as.Date R function converts character strings to the Date class.
Basic R Syntax: Please find the basic R programming syntax of the as.Date function below.
as.Date("2020-12-10") # Basic R syntax of as.Date function |
as.Date("2020-12-10") # Basic R syntax of as.Date function
In the remaining post, I’ll show an example for the application of the as.Date function in the R programming language.
Example Data
The following data will be used as basement for this R programming tutorial:
my_date <- "2020-10-05" # Create example date my_date # Print example date # "2020-10-05" |
my_date <- "2020-10-05" # Create example date my_date # Print example date # "2020-10-05"
The previous RStudio console output shows the structure of our example data: It’s a data object containing a single character string with year-month-day format.
Let’s check the data type of our example data:
class(my_date) # Check class of example date # "character" |
class(my_date) # Check class of example date # "character"
It’s a character string…
Example: Convert Character String to Date Using as.Date() Function
The following R syntax illustrates how to convert a character string to a Date object in R. For this task, we can use the as.Date function as shown below:
my_date_new1 <- as.Date(my_date) # Convert character string my_date_new1 # Print new date # "2020-10-05" |
my_date_new1 <- as.Date(my_date) # Convert character string my_date_new1 # Print new date # "2020-10-05"
The previous output of the RStudio console looks exactly as the output of our original input data. However, by checking the data class we see the difference:
class(my_date_new1) # Check class # "Date" |
class(my_date_new1) # Check class # "Date"
The updated data object has the class Date. Looks good!
Video, Further Resources & Summary
Have a look at the following video of my YouTube channel. In the video, I’m explaining the contents of the present tutorial.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you may want to read some of the related articles that I have published on this homepage.
- Convert Character String to Date Object
- Convert Date to Numeric Time Object
- How to Create a Range of Dates
- Convert Date to Day of Week
- R Functions List (+ Examples)
- The R Programming Language
At this point you should have learned how to change characters to dates in R programming. Let me know in the comments section below, in case you have additional questions. Besides that, please subscribe to my email newsletter to receive updates on the newest tutorials.
Subscribe to my free statistics newsletter: