Date Formats in R

Learn via video courses
Topics Covered

Overview

Date manipulation is a crucial aspect of data analysis, and R, a powerful statistical programming language, offers a multitude of functions to handle and format dates. In this article, we'll delve into various date formats in R, ranging from weekdays and dates to years, and explore how to obtain the current date and time from your system.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Date Formats in R

Dates play a pivotal role in data analysis, and R provides a robust suite of functions and formats to handle them efficiently. In this section, we will explore various date format in R, highlighting their significance and usage.

Weekday

Understanding weekdays is crucial for tasks such as analyzing trends over time or grouping data by day of the week. R's weekdays() function comes in handy for converting date objects into their respective weekdays. Additionally, the format() function allows you to customize the output format. Let's delve into a couple of examples to illustrate this:

Output:

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Date

Manipulating dates is a cornerstone of data analysis, and R simplifies this process with the Date class. The as.Date() function allows you to convert character vectors to date objects, and vice versa. Furthermore, the format() function enables you to format dates according to your requirements. Let's look at an example:

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Year

Isolating the year component from dates is often necessary for analyzing long-term trends and patterns. R offers functions like year() from the lubridate package to easily extract the year from a date object. This is particularly useful when working with datasets spanning multiple years. Here's an example:

Output:

Examples of Different Date Formats in R:

SpecifierDescriptionExample
%aAbbreviated weekdayThu
%AFull weekdayThursday
%bAbbreviated monthAug
%BFull monthAugust
%CCentury20
%yYear without century23
%YYear with century2023
%dDay of month (01-31)25
%jDay in Year (001-366)237
%mMonth of year (01-12)08
%DDate in %m/%d/%y format08/25/23
%uWeekday (01-07) Starts Monday04

How to Get the Current Date and Time for System?

Retrieving the current date and time is a fundamental aspect of data analysis, as it's often essential for timestamping, logging events, or synchronizing time-sensitive operations. In R, you can easily obtain the current date and time using built-in functions.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Using the Sys.time() Function

The primary function for obtaining the current date and time in R is Sys.time(). This function returns the current date and time as a POSIXct object, which includes both date and time components, accurate to seconds. Here's how you can use it:

Output:

Formatting the Current Date and Time

Once you have the current date and time using Sys.time(), you can format it to match your desired presentation style using the format() function. This function allows you to specify a format string with various placeholders representing different components of the date and time. Here's an example of formatting the current date and time to include the full date and time with seconds:

Output:

Extracting Components from the Current Date and Time

You might often need to extract specific components, such as the year, month, day, hour, minute, or second, from the current date and time. You can achieve this using the format() function with appropriate format codes. Here's an example of extracting the year, month, and day:

Output:

Conclusion

  • R offers diverse formats to represent weekdays, dates, and years, aiding in effective time-series analysis and data manipulation.
  • The Sys.time() function is instrumental in obtaining the current date and time, serving as a basis for precise timestamps in your analyses.
  • The format() function empowers you to tailor date and time displays, ensuring clear communication of temporal information.
  • Extracting specific components, like years and months, enhances granularity, enabling targeted exploration of time-related data.
  • The lubridate package simplifies date and time handling, offering additional functionalities to streamline complex operations.