months() and weekdays() and quarters(), oh my!
As a final lesson on dates, there are a few functions that are useful for extracting date components. One of those is months()
.
my_date <- as.Date("2017-01-02")
months(my_date)
[1] "January"
Two other useful functions are weekdays()
to extract the day of the week that your date falls on, and quarters()
to determine which quarter of the year (Q1-Q4) that your date falls in.
This is a part of the course
“Intermediate R for Finance”
Exercise instructions
- A vector of
dates
has been created for you. - Extract the
months()
from these dates. - Extract the
quarters()
from these dates. - Another vector,
dates2
has also been created for you. - Use
weekdays()
to determine what day of the week the dates fell on, and assign them to the names ofdates2
usingnames()
. - Print
dates2
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# dates
dates <- as.Date(c("2017-01-02", "2017-05-03", "2017-08-04", "2017-10-17"))
# Extract the months
___
# Extract the quarters
___
# dates2
dates2 <- as.Date(c("2017-01-02", "2017-01-03", "2017-01-04", "2017-01-05"))
# Assign the weekdays() of dates2 as the names()
___
# Print dates2
___