Press "Enter" to skip to content

How to work with data and time in R?

Zigya Acadmey 0

In this article, you’ll learn how to add date and time in R. Dates and times are very frequently used concepts in data analysis. R provides a broad range of capabilities to deal with times and dates which are quite fundamental to R programming. Some of the best ways of using them come within add-on packages.

There are three types of date and time classes which arrive with R programming:

POSIXct
POSIXlt and
Date.

We’ll go through these classes one by one:

POSIX Date and Time Classes:

POSIX dates and times are thorough in their implementation, navigating all sorts of obscure technical issues.

The function Sys.time() is used to return the current date and time in POSIXct notation:

> Sys.time()
[1] "2020-12-11 22:32:01 IST"

Here, ct is the short form for calendar time. When the date needs to be printed, you just see a formatted version of it. By using ‘unclass’, you can see where it is indeed just a number:

> unclass(Sys.time())
[1] 1607706364

Date Class:

This class in R keeps dates as if the number of days since the starting of 1970. Fractional days are probable which can get generated by computing a mean Date (suppose), but the POSIX classes are better for those situations like:

> now_time <- Sys.time()
> date <- as.Date(now_time)
> date
[1] "2020-12-25"
> class(date)
[1] "Date"

Lubridate

As the name implies, put in some much-needed lubrication to the practice of date manipulation. Lubridate package is not part of core tidyverse because you only need it when you’re working with dates/times.

Usage

library (lubridate)

Creating Date/Time

There are three types of date/time data that refer to an instant in time:

  • date. Tibbles print this as <date>.
  • time within a day. Tibbles print this as <time>.
  • date-time is a date plus a time: it uniquely identifies an instant in time (typically to the nearest second). Tibbles print this as <dttm>. Elsewhere in R these are called POSIXct, but I don’t think that’s a very useful name.

R doesn’t have a native class for storing times.

To get the current date or date-time you can use today() or now():

[1] "Date"
> today()
[1] "2020-12-11"
> now()
[1] "2020-12-11 22:57:42 IST"

Otherwise, there are three ways you’re likely to create a date/time:

Date/time data often comes as strings. Another approach is to use the helpers provided by lubridate. They automatically work out the format once you specify the order of the component. To use them, identify the order in which year, month, and day appear in your dates, then arrange “y”, “m”, and “d” in the same order. That gives you the name of the lubridate function that will parse your date. For example:

> ymd("20200530")
[1] "2020-05-30"
> mdy("January 31st, 2020")
[1] "2020-01-31"
> dmy("25-Dec-2020")
[1] "2020-12-25"

Conclusion

Hence, we saw how to access data and time in R, with base R functions and also with lubridate library and also saw a different representation of date and time.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

In this article, you’ll learn how to add date and time in R. Dates and times are very frequently used concepts in data analysis. R provides a broad range of capabilities to deal with times and dates which are quite fundamental to R programming. Some of the best ways of using them come within add-on packages.

There are three types of date and time classes which arrive with R programming:

POSIXct
POSIXlt and
Date.

We’ll go through these classes one by one:

POSIX Date and Time Classes:

POSIX dates and times are thorough in their implementation, navigating all sorts of obscure technical issues.

The function Sys.time() is used to return the current date and time in POSIXct notation:

> Sys.time()
[1] "2020-12-11 22:32:01 IST"

Here, ct is the short form for calendar time. When the date needs to be printed, you just see a formatted version of it. By using ‘unclass’, you can see where it is indeed just a number:

> unclass(Sys.time())
[1] 1607706364

Date Class:

This class in R keeps dates as if the number of days since the starting of 1970. Fractional days are probable which can get generated by computing a mean Date (suppose), but the POSIX classes are better for those situations like:

> now_time <- Sys.time()
> date <- as.Date(now_time)
> date
[1] "2020-12-25"
> class(date)
[1] "Date"

Lubridate

As the name implies, put in some much-needed lubrication to the practice of date manipulation. Lubridate package is not part of core tidyverse because you only need it when you’re working with dates/times.

Usage

library (lubridate)

Creating Date/Time

There are three types of date/time data that refer to an instant in time:

  • date. Tibbles print this as <date>.
  • time within a day. Tibbles print this as <time>.
  • date-time is a date plus a time: it uniquely identifies an instant in time (typically to the nearest second). Tibbles print this as <dttm>. Elsewhere in R these are called POSIXct, but I don’t think that’s a very useful name.

R doesn’t have a native class for storing times.

To get the current date or date-time you can use today() or now():

[1] "Date"
> today()
[1] "2020-12-11"
> now()
[1] "2020-12-11 22:57:42 IST"

Otherwise, there are three ways you’re likely to create a date/time:

Date/time data often comes as strings. Another approach is to use the helpers provided by lubridate. They automatically work out the format once you specify the order of the component. To use them, identify the order in which year, month, and day appear in your dates, then arrange “y”, “m”, and “d” in the same order. That gives you the name of the lubridate function that will parse your date. For example:

> ymd("20200530")
[1] "2020-05-30"
> mdy("January 31st, 2020")
[1] "2020-01-31"
> dmy("25-Dec-2020")
[1] "2020-12-25"

Conclusion

Hence, we saw how to access data and time in R, with base R functions and also with lubridate library and also saw a different representation of date and time.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

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