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 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
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:
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:
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.
Example 4 for as.Date() function
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.
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.
Example code with output for Sys.Date(), Sys.time(), and Sys.timezone() functions
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.
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:
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.
Example code for extraction of different date components
We have create a date variable named “x,” which contains three different date values.
In the first example, the year() function allows us to extract the year values for each element of the vector.
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.
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.
If we use the “abbr = FALSE” argument under the month function along with the “label = TRUE,” we will get the full month names.
To extract the days from the given date values, you can use the mday() function. You will get the days as numbers.
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.
Example code with output for dates manipulation in R
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.
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().
In the same way, we can use months() to add or subtract the month values to each vector element.
We can use the mday() function to update the days for each date from the given vector.
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.
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! :)
Reliance Jio and JioMart: Marketing Strategy, SWOT Analysis, and Working EcosystemREAD MORE
6 Major Branches of Artificial Intelligence (AI)READ MORE
Top 10 Big Data TechnologiesREAD MORE
8 Most Popular Business Analysis Techniques used by Business AnalystREAD MORE
7 types of regression techniques you should know in Machine LearningREAD MORE
Deep Learning - Overview, Practical Examples, Popular AlgorithmsREAD MORE
Introduction to Time Series Analysis in Machine learningREAD MORE
What is the OpenAI GPT-3?READ MORE
How Does Linear And Logistic Regression Work In Machine Learning?READ MORE
7 Types of Activation Functions in Neural NetworkREAD MORE