Create and format dates
To create a Date
object from a simple character string in R, you can use the as.Date()
function. The character string has to obey a format that can be defined using a set of symbols (the examples correspond to 13 January, 1982):
%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)
The following R commands will all create the same Date
object for the 13th day in January of 1982:
as.Date("1982-01-13")
as.Date("Jan-13-82", format = "%b-%d-%y")
as.Date("13 January, 1982", format = "%d %B, %Y")
Notice that the first line here did not need a format argument, because by default R matches your character string to the formats "%Y-%m-%d"
or "%Y/%m/%d"
.
In addition to creating dates, you can also convert dates to character strings that use a different date notation. For this, you use the format()
function. Try the following lines of code:
today <- Sys.Date()
format(Sys.Date(), format = "%d %B, %Y")
format(Sys.Date(), format = "Today is a %A!")
This is a part of the course
“Intermediate R”
Exercise instructions
- Three character strings representing dates have been created for you. Convert them to dates using
as.Date()
, and assign them todate1
,date2
, anddate3
respectively. The code fordate1
is already included. - Extract useful information from the dates as character strings using
format()
. From the first date, select the weekday. From the second date, select the day of the month. From the third date, you should select the abbreviated month and the 4-digit year, separated by a space.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Definition of character strings representing dates
str1 <- "May 23, '96"
str2 <- "2012-03-15"
str3 <- "30/January/2006"
# Convert the strings to dates: date1, date2, date3
date1 <- as.Date(str1, format = "%b %d, '%y")
# Convert dates to formatted strings
format(date1, "%A")
This exercise is part of the course
Intermediate R
Continue your journey to becoming an R ninja by learning about conditional statements, loops, and vector functions.
Mastering R programming is not only about understanding its programming concepts. Having a solid understanding of a wide range of R functions is also important. This chapter introduces you to many useful functions for data structure manipulation, regular expressions, and working with times and dates.
Exercise 1: Useful FunctionsExercise 2: Mathematical utilitiesExercise 3: Find the errorExercise 4: Data UtilitiesExercise 5: Find the error (2)Exercise 6: Beat Gauss using RExercise 7: Regular ExpressionsExercise 8: grepl & grepExercise 9: grepl & grep (2)Exercise 10: sub & gsubExercise 11: sub & gsub (2)Exercise 12: Times & DatesExercise 13: Right here, right nowExercise 14: Create and format datesExercise 15: Create and format timesExercise 16: Calculations with DatesExercise 17: Calculations with TimesExercise 18: Time is of the essenceWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.