Many dates
Creating a single date is nice to know how to do, but with financial data you will often have a large number of dates to work with. When this is the case, you will need to convert multiple dates from character to date format. You can do this all at once using vectors. In fact, if you remembered that a single character is actually a vector of length 1, then you would know that you have been doing this all along!
# Create a vector of daily character dates
dates <- c("2017-01-01", "2017-01-02",
"2017-01-03", "2017-01-04")
as.Date(dates)
[1] "2017-01-01" "2017-01-02" "2017-01-03" "2017-01-04"
Like before, this might look like it returned another character vector, but internally these are all stored as numerics, with some special properties that only dates have.
Deze oefening maakt deel uit van de cursus
Intermediate R for Finance
Oefeninstructies
- Create another vector of dates containing the 4 days from
"2017-02-05"to"2017-02-08"inclusive. Call thisdates. - Assign the days of the week
"Sunday", "Monday", "Tuesday", "Wednesday", in that order, asnames()of the vectordates. - Subset
datesusing[ ]to retrieve only the date for"Monday".
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# Create dates from "2017-02-05" to "2017-02-08" inclusive
dates <- ___
# Add names to dates
___
# Subset dates to only return the date for Monday
___