To format dates in R, use the format() function with various specifications. Having your dates in the proper format allows you to know that they are dates and which calculations should be performed on them.
Conversion specification | Description | Example |
%a | Abbreviated weekday | Sun, Mon |
%A | Full weekday | Sunday, Monday |
%b or %h | Abbreviated month | Sep, Nov |
%B | Full month | September, November |
%d | Day of the month 01-31 | 10, 20 |
%j | Day of the year 001-366 | 250, 310 |
%m | Month 01-12 | 09, 11 |
%U | Week 01-53 with Sunday as the first day of the week | 35, 45 |
%w | Weekday 0-6 Sunday is 0 | 0, 4 |
%W | Week 00-53 with Monday as the first day of the week | 21, 27 |
%x | Date, locale-specific | |
%y | Year without century 00-99 | 84, 05 |
%Y | Year with century on input: 00 to 68 prefixed by 20 69 to 99 prefixed by 19 | 1984, 2011 |
%C | Century | 19, 20 |
%D | Date formatted %m/%d/%y | 09/10/93, 11/20/93 |
%u | Weekday 1-7 Monday is 1 | 7, 4 |
To get today’s date, use the Sys.Date() function.
To get the abbreviated day, pass the format “%a” to the format() function.
For a full day, pass the format argument to the “%A”.
For a weekday, pass the format argument to the “%u”.
# today date
today_date <- Sys.Date()
# Abbreviated Day
format(today_date, format = "%a")
# Full Day
format(today_date, format = "%A")
# Weekday
format(today_date, format = "%u")
# Output
# [1] "Sun"
# [1] "Sunday"
# [1] "0"
To extract the day from the date, pass format = “%d” to the format() function.
For the month number, pass the format argument to the “%m”.
For an abbreviated month, pass the format argument to the “%b”.
For getting full month name from the date, pass the format argument to the “%B”.
today_date <- Sys.Date()
# Default format yyyy-mm-dd
today_date
# Day
format(today_date, format = "%d")
# Month
format(today_date, format = "%m")
# Abbreviated month
format(today_date, format = "%b")
# Full month
format(today_date, format = "%B")
# Output:
# [1] "2023-12-24"
# [1] "24"
# [1] "12"
# [1] "Dec"
# [1] "December"
# [1] "12/24/23"
# [1] "24-Dec-23"
To extract the year from the date, pass format = “%y” to the format() function.
To get the year with the century, pass the format argument to the “%Y”.
To get the only century, pass the format argument to the “%C”.
today_date <- Sys.Date()
# Only year
format(today_date, format = "%y")
# Year with Century
format(today_date, format = "%Y")
# Only Century
format(today_date, format = "%C")
# Output:
# [1] "23"
# [1] "2023"
# [1] "20"
That’s it!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
To rename a file in R, you can use the file.rename() function. It renames a…
The prop.table() function in R calculates the proportion or relative frequency of values in a…
The exp() is a built-in function that calculates the exponential of its input, raising Euler's…
The split() function divides the input data into groups based on some criteria, typically specified…
The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…
The rowMeans() is a built-in, highly vectorized function in R that computes the arithmetic mean…