We’ll be working on the as.date() function in R today. In any programming language, dealing with dates is very important and R is no exception. R by default reads the dates as the character. You need to convert the class of the data from character to date using the as.date() function to get your data ready for further process.
In this article, we will be focusing on the as.date() function in R programming.
Table of Contents
as.date() function in R programming
In this section, I will show the basic working of as.date() function in R. By the end of this illustration, you will get to know about the dates in R and converting them into numeric form i.e. date format in R language.
#Sample date date <- "31-12-2020" date
#Returns the class of date class(date)
#Returns the class of date new_date <- as.Date("31-12-2020") class(new_date)
That’s cool! You can observe that R by default reads the date as the character. Then we have used as.date() function in R to convert the class of the data to date format. Now, R will treat the dates as numeric dates.
Date formats and Increments in R
Using R, you can easily change the format of the date from one format to another date format. You can also increment the date which will be illustrated below.
#sample date my_date <- "2020-12-31" #display date mydate
Incrementing the date with +1.
#Incrementing date by 1 as.Date("2020-12-31")+1
By adding the +1 or any other specific incrementation you can raise the date. You can also decrement the date by specifying as -1 or any other numbers.
#decrements the date by 1 as.Date("2020-12-31")-1
Format conversion and Error handling in R
If your data includes the date in the format of year-month-date, then it works fine. But, if you have dates in other formats, then you will encounter an error while executing.
my_date <- "2020-12-31" as.Date("2020-12-31")
my_date<- "2020-DEC-31" > as.Date("2020-DeC-31")
Error in charToDate(x): character string is not in a standard unambiguous format
as.Date("31-DEC-20", format = "%d-%b-%y")
As you can observe in the above examples, when you have dates in the format of year-month-date, it looks fine. But when you have the date in a different format, it will throw an error.
To resolve this, you can use as.date() function in R with the format parameter to specify the required format. Then the function will return the date as shown above.
Check the system date and time
With the help of a single line of code, you can check the system date and time. You have to use functions sys.date() and date() to get the required information in the R programming language.
#returns today's date Sys.Date()
#returns today's date, time and day date()
"Thu Dec 31 14:16:45 2020"
In the below table you can see all date formats.
The as.date() function in R is the most useful function when it comes to date format conversions. R by default reads the date as the character.
So you need to convert it as a date with the help of this function. You can also increment or decrement the date and you can convert in as many formats as you want.
More read: R documentation