IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Intermediate R for Finance

Visualizza il corso

Istruzioni dell'esercizio

  • Create another vector of dates containing the 4 days from "2017-02-05" to "2017-02-08" inclusive. Call this dates.
  • Assign the days of the week "Sunday", "Monday", "Tuesday", "Wednesday", in that order, as names() of the vector dates.
  • Subset dates using [ ] to retrieve only the date for "Monday".

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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
___
Modifica ed esegui il codice