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.
Cet exercice fait partie du cours
Intermediate R for Finance
Instructions
- A vector of
dateshas been created for you. - Extract the
months()from these dates. - Extract the
quarters()from these dates. - Another vector,
dates2has also been created for you. - Use
weekdays()to determine what day of the week the dates fell on, and assign them to the names ofdates2usingnames(). - Print
dates2.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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
___