function in R programming language

Filed Under: R Programming
As Date() Function In R

We’ll be working on the 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 function to get your data ready for further process.

In this article, we will be focusing on the function in R programming. function in R programming

In this section, I will show the basic working of 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"
#Returns the class of date
#Returns the class of date
new_date <- as.Date("31-12-2020")

That’s cool! You can observe that R by default reads the date as the character. Then we have used 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

Incrementing the date with +1.

#Incrementing date by 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

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"
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 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 and date() to get the required information in the R programming language. 

#returns today's date
#returns today's date, time and day
"Thu Dec 31 14:16:45 2020"

In the below table you can see all date formats. function in r

Ending note

The 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages