What day is it?
R has a lot to offer in terms of dates and times. The two main classes of data for this are Date and POSIXct. Date is used for calendar date objects like "2015-01-22". POSIXct is a way to represent datetime objects like "2015-01-22 08:39:40 EST", meaning that it is 40 seconds after 8:39 AM Eastern Standard Time.
In practice, the best strategy is to use the simplest class that you need. Often, Date will be the simplest choice. This course will use the Date class almost exclusively, but it is important to be aware of POSIXct as well for storing intraday financial data.
In the exercise below, you will explore your first date and time objects by asking R to return the current date and the current time.
Deze oefening maakt deel uit van de cursus
Intermediate R for Finance
Oefeninstructies
- Type
Sys.Date()to have R return the current date. - Type
Sys.time()to have R return the current date and time. Notice the difference in capitalization of Date vs time. - Store
Sys.Date()in the variabletoday. - Use
class()ontodayto confirm its class.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# What is the current date?
___
# What is the current date and time?
___
# Create the variable today
today <- ___
# Confirm the class of today
___