This code chunk creates an object “d” which is the date May 8, 2020. It then formatted so that is can be used in other lines of code.
d1 <- "05/08/2020"
d <- as.Date(d1, format = "%m/%d/%Y")
This section of code ensures that the object “d” is in the right format so R can utilize it effectively.
print(d)
## [1] "2020-05-08"
This section of cod loads the lubridate packages which allows us to use all of the functions in that packag.
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
This code chunk allows to extract the year, month, week of the year, and day of the week from object “d”. It also displays four new objects with those items.
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)
This code chunk creates and object that represent object “d” plus 25 days. The value is then displayed using the print funtion.
d_25 <- d + 25
print(d_25)
## [1] "2020-06-02"
In the last section of code, using the difftime function we are able to display the time differece of 25 days between object “d” and object “d_25”.
difftime(d_25, d, units = 'days')
## Time difference of 25 days