Date formats (2)
Not only can you convert characters to dates, but you can convert objects that are already dates to differently formatted dates using format():
# The best point move in stock market history. A +936 point change in the Dow!
best_date
[1] "2008-10-13"
format(best_date, format = "%Y/%m/%d")
[1] "2008/10/13"
format(best_date, format = "%B %d, %Y")
[1] "October 13, 2008"
As a reminder, here are the formats:
%Y: 4-digit year (1982)%y: 2-digit year (82)%m: 2-digit month (01)%d: 2-digit day of the month (13)%A: weekday (Wednesday)%a: abbreviated weekday (Wed)%B: month (January)%b: abbreviated month (Jan)
Cet exercice fait partie du cours
Intermediate R for Finance
Instructions
- Create the vector
datesfromchar_date, specifying theformatso R reads them correctly. - Modify
datesusingformat()so that each date looks like"Jan 04, 17". - Modify
datesusingformat()so that each date looks like"01,04,2017".
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
char_dates <- c("1jan17", "2jan17", "3jan17", "4jan17", "5jan17")
# Create dates using as.Date() and the correct format
dates <- ___
# Use format() to go from "2017-01-04" -> "Jan 04, 17"
___
# Use format() to go from "2017-01-04" -> "01,04,2017"
___