date_string <- “05/08/2020”
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”)
library(lubridate) # Extract the year from the date object d_year <- year(d)
d_month <- month(d)
d_week <- week(d)
d_day <- wday(d)
print(d_year) print(d_month) print(d_week) print(d_day) # Create a date 25 days later d_25 <- d + 25 print(d_25) # Calculate the difference between d and d_25 diff_days <- difftime(d_25, d, units = “days”)
if (diff_days == 25) { print(“The difference is 25 days.”) } else { print(“The difference is not 25 days.”) }