adplus-dvertising

Dates in R Programming

  • Lalit Salunkhe
  • Aug 28, 2020
  • R Programming
Dates in R Programming title banner

Dates are essential when you are dealing with the real-world data problems. Time will also be a component to look after while working on such data problems in real life. It is sometimes a tough job to deal with dates and times as there are various formats for the same as well as there are different time-zones across the globe. However, working with dates and times in R is extremely easy due to a variety of functions that allow you a smooth working over the same. In this article, we are going to see how to deal with the dates and times in R programming.

 

The as.Date() Function

 

The most basic function we use while dealing with the dates is as.Date() function. This function allows us to create a date value (without time) in R programming. It allows the various input formats of the date value as well through the format = argument.

 

See an example below for the as.Date() function


This image shows how the as.Date() function can be used to create a date value in R programming.

Example 1 for as.Date() function


Remember that this function supports the standard date format as “YYYY-MM-DD.”

 

Now, when we don’t have input value in a standard date format, we still can use the as.Date() function to create a dates value. See an example below:


This image shows how the as.Date() function works even if we don't have the input date in proper format.

Example 2 for as.Date() Function


In this example, if you could see, the input date value is ”01/22/2015”,  which is not the standard date format. However, we have format = argument under the function, which allows it to arrange the date values in a standard form and present it to us.

%d -  means a day of the month in number format

%m - stands for the month in number format

%Y - stands for the year in the “YYYY” format. If we have the year value in two digits, we will use the  “%y” instead of “%Y.” See an example below:


This image shows the use of "%y" when the input data has a year value in two digits.

Example 3 for as.Date() Function


When we have a month name instead of month number under the input value, we can use the %B operator under the format = argument while using the as.Date() function.


This image shows how to use the %B in as.Date() function.

Example 4 for as.Date() function



Getting the Current Date and Time for System

 

Well, the as.Date() function takes a date value as an argument. Meaning, we always have to give a date value as an input. What if we wanted to get the current system date and time? Well, that is possible in R. We have two options to get that done.

 

  1. Using the Sys.Date(), Sys.time() Function

 

  • In R programming, if you use Sys.Date() function, it will give you the system date. You don’t need to add an argument inside the parentheses to this function.

  • There is again a function named Sys.timezone() that allows us to get the timezone based on the location at which the user is running the code on the system.

  • And finally, we have the Sys.time() function. Which, if used, will return the current date as well as the time of the system with the timezone details.

 

Let us see an example of these functions and how they work.


This image shows how the Sys.Date(), Sys.time(), Sys.timezone() functions work and generate the desired output.

Example code with output for Sys.Date(), Sys.time(), and Sys.timezone() functions


  1. Using the lubridate Package

 

Well, there is a package named lubridate, which has a function named now() that can give us the current date, current time, and the current timezone details in a single call (same as the Sys.time() function).

See an example below for better realization.


This image shows how the now() function from the lubridate package generates the current system date, time, and timezone.

Example code with output for the now() function from lubridate package



Well, if you see this code doesn’t work for you, possibly you have missed to install the package “lubridate.” To install the same, use the code below:

 

install.packages(“lubridate”)

 

Extraction and Manipulation of the Parts of the Date

 

Since you already have seen how the “lubridate” package work, it becomes easier to use it for extraction and manipulation of some parts of the date value. 

 

There are various functions under the package that allow us to either extract the year, month, week, etc. from the date.

 

Let us see some of the examples of different functions that are useful in extracting various date components. 


This image shows how the extraction of different components from a date value is done under the lubridate package.

Example code for extraction of different date components


We have create a date variable named “x,” which contains three different date values.

 

  1. In the first example, the year() function allows us to extract the year values for each element of the vector.

  2. Similarly, the month() function takes a single date value or a vector that contains dates as element and extracts the month from those as numbers.

  3. What if we wanted the abbreviated names for each month from dates? We have to add the “label = TRUE” argument under the month() function and could see the month names in abbreviated form. 

  4. If we use the “abbr = FALSE” argument under the month function along with the “label = TRUE,” we will get the full month names.

  5. To extract the days from the given date values, you can use the mday() function. You will get the days as numbers.

  6. The wday() function allows us to get the weekdays in numbers by default. However, when we use the “label = TRUE” and “abbr = FALSE” as additional arguments under the function, we will come to know which day of the given date has which weekday value.

 

Note: Well, I would like to add a point of note here. Every time you open the instance of your RStudio/R software, you need to navigate towards this “lubridate” package to let these functions do the work for you. You can always use the “library(lubridate)” as a piece of code that takes you to the lubridate package. However, installing the package is a one-time process only, as mentioned above.

 

Now, on the manipulation part, there are several functions under this impressive “lubridate” package. Let’s see an example for the same.


This image shows how the date manipulation happens using different functions that are present under the lubridate package.

Example code with output for dates manipulation in R


 

  1. In the first example, we are using ymd() function on the given vector. This function converts the date values from the vector into a format that is suitable for the manipulation.

  2. We can add or subtract the year values from each element of the vector. It is similar to adding or subtracting components from a numeric vector. The function we have used here is years().

  3. In the same way, we can use months() to add or subtract the month values to each vector element.

  4. We can use the mday() function to update the days for each date from the given vector.

  5. The update() function is a combination of these all. This function allows you to add, years, months, and even days to each element of the given vector.

 

 

Conclusion

 

  • Dates are an essential aspect when we are dealing with the real-life data problems.

  • The as.Date() function allows us to convert the string date values into actual dates. The standard date format is “YYYY-MM-DD.”

  • To get the current system date, we can use the Sys.Date() function.

  • Sys.timezone() function allows us to get the timezone of the system.

  • The Sys.time() function allows us to get the current system date, time, with timezone in a single function.

  • The lubridate package has various functions that allow us to work with dates more efficiently.

 

We will stop this article here and make sure to come up with something more interesting when we come up next time. Until then, stay home, stay safe, and keep enhancing! :)

0%

Comments