Introduction to Business Analytics with R

creates varible ‘d’ to house date as string

houses date variable ‘d’ as a character string

d <- as.Date.character('05/08/2020', '%m/%d/%y') 

check data type of ‘d’

checks the data type printing variable ‘d’

print(d)
## [1] "2020-05-08"

load Lubridate

loads the lubridate package

library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union

extract date information

uses lubriate functions to extract information from ‘d’ into specified variables

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

date 25 days from now

adds 25 days to the original date ‘d’ and stores it as variable ‘d_25’

d_25 <- d + 25

difference d & d_25

finds the number of days between ‘d_25’ and ‘d’

difftime(d_25,d)
## Time difference of 25 days