Introduction to Business Analytics with R

Create a datetime object from a character string using base R

d = as.Date(“05/08/2020”, format=“%m/%d/%Y”) d

Check the data type of the datetime object

print(d)

Load the lubridate package

library(lubridate)

Extract date components using lubridate

d_year <- year(d) d_month <- month(d) d_week <- week(d) d_day <- wday(d)

Display the extracted components

d_year d_month d_week d_day

Create a new datetime object 25 days from the original date

d_25 <- d + days(25) d_25

Calculate the difference between d and d_25

difference <- difftime(d_25, d, units = “days”) difference

Check if the difference is 25 days

is_25_days <- difference == 25 is_25_days