R Date

Depending on what purposes we're using R for, we may want to deal with data containing dates and times.

R Provides us various functions to deal with dates and times.


Get Current System Date, and Time in R

In R, we use Sys.Date(), Sys.time() to get the current date and time respectively based on the local system. For example,

# get current system date
Sys.Date() 

# get current system time
Sys.time()

Output

[1] "2022-07-11"
[1] "2022-07-11 04:16:52 UTC"

In the above example, we have used different functions to get the current date, and time based on the local system.

Here,

  • Sys.date() - returns the current date i.e. 2022-07-11
  • Sys.time() - returns the current date, time, and timezone i.e. 2022-07-11 04:16:52 UTC

Using R lubridate Package

The lubridate package in R makes the extraction and manipulation of some parts of the date value more efficient.

There are various functions under this package that can be used to deal with dates.

But first, in order to access the lubridate package, we first need to import the package as:

# access lubridate package
library(lubridate) 

Here, we have successfully imported the lubridate package.

1. Get Current Date Using R lubridate Package

# access lubridate package
library(lubridate) 

# get current date with time and timezone
now()

# Output: "2022-07-11 04: 34: 23 UTC"

Here, we have used the now() function provided by the lubridate package to get the current date with time and timezone.


2. Extraction Years, Months, and Days from Multiple Date Values in R

In R, we use the year(), month(), and mday() function provided by the lubridate package to extract years, months, and days respectively from multiple date values. For example,

#  import lubridate package
library(lubridate)

dates <- c("2022-07-11", "2012-04-19", "2017-03-08")

# extract years from dates
year(dates)

# extract months from dates
month(dates)

# extract days from dates
mday(dates)

Output

[1] 2022 2012 2017
[1] 7 4 3
[1] 11 19 8

Here,

  • year(dates) - returns all years from dates i.e. 2022 2012 2017
  • month(dates) - returns all months from dates i.e. 7 4 3
  • mday(dates) - returns days from dates i.e 11 19 8

3. Manipulate Multiple Date Values in R

The lubridate package in R allows us to manipulate multiple date values all at once. For example,

# import lubridate package
library(lubridate)

dates <- c("2022-07-11", "2012-04-19", "2017-03-08")

# increase each year by one year
print(dates + years(1))

# increase each month by one month 
print(dates + months(1))

# update days
mday(dates) <- c(22, 18, 15)
print(dates) 

Output

[1] "2023-07-11" "2013-04-19" "2018-03-08"
[1] "2022-08-11" "2012-05-19" "2017-04-08"
[1] "2022-07-22" "2012-04-18" "2017-03-15"

Here,

  • dates + years(1) - increases each year in dates by one year
  • dates + months(1) - increases each month in dates by one month
  • mday(dates) <- c(22, 18, 15) - updates each day in dates with a new day.

4. Using update() to Update Multiple dates Values in R

In R, we can use the update() function to update multiple dates values all at once. For example,

# import lubridate package
library(lubridate)

dates <- c("2022-07-11", "2012-04-19", "2017-03-08")

# update all date values using update() 
new_dates <- update(dates, 
  year = c(2022, 2015, 2019), 
  month = c(9, 12, 1), 
  day = c(21, 2, 13)
)

Output

[1] "2022-09-21" "2015-12-02" "2019-01-13"

In the above example, we have used the update() function to update the dates vector containing years, months, and days values with new values.

  • year = c(2022, 2015, 2019) - updates current years of dates with new years.
  • month = c(9, 12,1) - updates current month with new months.
  • day = c(21, 2, 13) - updates current days with new ones.
Did you find this article helpful?