Introduction to Business Analytics with R

Create a character string representing May 8, 2020

date_string <- “05/08/2020”

Convert the character string to a date object

d <- as.Date(date_string, format = “%m/%d/%Y”) # Print the data type of the date object print(d) # Install the lubridate package if not already installed install.packages(“lubridate”)

Load the lubridate package

library(lubridate) # Extract the year from the date object d_year <- year(d)

Extract the month number from the date object

d_month <- month(d)

Extract the week number from the date object

d_week <- week(d)

Extract the weekday number from the date object

d_day <- wday(d)

Check if the difference is 25 days

if (diff_days == 25) { print(“The difference is 25 days.”) } else { print(“The difference is not 25 days.”) }