The code chunk creates an object for the date string. It is then formatted so the numeric values can be read into different functions/codes.
d <- '05/08/2020'
d <- as.Date(d, format = '%m/%d/%y')
The code chunk displays the type and the value of the object.
class(d)
## [1] "Date"
print(d)
## [1] "2020-05-08"
The lubridate package provides functions that make calcuations of dates easier in R. The code chunk displays the value for the requested function of d. In addition, it creates a new object name for that value.
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
year(d)
## [1] 2020
month(d)
## [1] 5
week(d)
## [1] 19
wday(d)
## [1] 6
d_year <- year(d)
d_month <- month(d)
d_week <- week(d)
d_day <- wday(d)
The code chunk creates an object that represents object d plus 25 days. This value is displayed using the print function
d_25 <- d + 25
print(d_25)
## [1] "2020-06-02"
The difftime function displays the time difference of 25 days between object d and d_25.
difftime(d_25, d, units = 'days')
## Time difference of 25 days