SlideShare a Scribd company logo
Manipulating Dates
Using as.Date, difftime(), as.POSIXct(), as.POSIXlt(),
lubridate()
Rupak Roy
#load the data
>datedata<-read.csv(file.choose(),header = TRUE)
>str(datedata)
#using as.Date(): A factor or a character data type can be easily converted into
a date format where as.Date(column_name, orginal_date_format_of the
dataset)
>datedata$Date.received<-as.Date(datedata$Date.received,"%m/%d/%Y")
>datedata$Date.sent.to.company<-
as.Date(datedata$Date.sent.to.company,"%m/%d/%Y")
>str(datedata) #code for identify different combinations of date format
#example:
15/aug/2016:“ %d/%b/%Y ”
13-JUNE-16: “ %d-%B-%y ”
as.Date()
%d Day of the month %m Month (in decimal)
%Y Year (4 digits) %b Month (abbreviated)
%y Year (2 digits) %B Month (full name)
#once the data is in date format we can easily extract the months and the
weeks.
>months(datedata$Date.received)
>weekdays(datedata$Date.received)
#even we can filter the data using months() and weekdays()
#sub-setting using base R package
>datadate2<-datedata[(weekdays(datedata$Date.received)=="Sunday"),]
as.Date()
Rupak Roy
#using difftime() function we can calculate the time interval / differences
without being converted into minutes, hour, weeks, months or years.
>difftime(time2, time1, tz, units = c("auto", "secs", "mins", "hours", "days",
"weeks"))
Where tz= an optional time zone specification to be used for the conversion
units= Units in which the results are desired.
>difftime(datedata$Date.received,datedata$Date.sent.to.company,units =
"days") #this will give the output in a negative value because we are doing
time1-time2 rather than time2-time1 i.e 5- 10 = -5 rather than 10-5 =5
#therefore
>difftime(datedata$Date.sent.to.company,datedata$Date.received,units =
"days")
To know more about the difftime() function use >?difftime
difftime()
 These are date-time conversion functions to manipulate data or the objects
of classes "POSIXlt" and "POSIXct" having time information along with the
date.
 POSIXct is similar to POSIXlt the only difference is POSIXlt stores the data in
a list format
#saving the current date-time information as an example
>datetime<-Sys.time()
>class(datetime)
>weekdays(datetime) >months(datetime)
#convert the date and time into POSIXlt()
>datetime<-as.POSIXlt(datetime)
>datetime$zone >datetime$hour >datetime$wday
as.POSIXct() and as.POSIXlt()
Rupak Roy
 Lubridate() is an another function that makes it easier to parse and
manipulate dates.
#install and load the package
>install.packages(lubridate)
>library(lubridate)
>datedata<-read.csv(file.choose(),header = TRUE)
>str(datedata)
#converting the factor data type into date data type
>datedata$Date.received<-mdy(datedata$Date.received)
>str(datedata)
Where, mdy =
lubridate()
Code Format dmy() 3/10/2017
mdy() 10/3/2017 dmy_hm() 3/10/2017 23:40
ydm() 2017/3/10 dmy_hms() mdy_hm() mdy_hms() ydm_hm()
Next:
We will learn an another important function that is combining two
or more data sets using merge().
Manipulating Dates
Rupak Roy

More Related Content

PDF
Manipulating Data using base R package
PDF
Transpose and manipulate character strings
PDF
Handling Missing Values
PDF
Data Preparation- handling missing value
PDF
Data Visualization using base graphics
PDF
Apache PIG Relational Operations
PDF
Manipulating Data using DPLYR in R Studio
PDF
Data Types and Structures in R
Manipulating Data using base R package
Transpose and manipulate character strings
Handling Missing Values
Data Preparation- handling missing value
Data Visualization using base graphics
Apache PIG Relational Operations
Manipulating Data using DPLYR in R Studio
Data Types and Structures in R

What's hot (19)

PDF
R code for data manipulation
PDF
Apache Pig Relational Operators - II
PDF
Data manipulation on r
PDF
Data manipulation with dplyr
PPTX
3. R- list and data frame
PDF
R code descriptive statistics of phenotypic data by Avjinder Kaler
PDF
SAS and R Code for Basic Statistics
PDF
Data handling in r
PPTX
5. working on data using R -Cleaning, filtering ,transformation, Sampling
PDF
4 R Tutorial DPLYR Apply Function
PDF
Stata cheatsheet transformation
PDF
Stata cheat sheet: data transformation
PPTX
DPLYR package in R
PDF
Stata Programming Cheat Sheet
PDF
Stata cheat sheet: data processing
PDF
Grouping & Summarizing Data in R
PDF
PPTX
2. R-basics, Vectors, Arrays, Matrices, Factors
PDF
Stata Cheat Sheets (all)
R code for data manipulation
Apache Pig Relational Operators - II
Data manipulation on r
Data manipulation with dplyr
3. R- list and data frame
R code descriptive statistics of phenotypic data by Avjinder Kaler
SAS and R Code for Basic Statistics
Data handling in r
5. working on data using R -Cleaning, filtering ,transformation, Sampling
4 R Tutorial DPLYR Apply Function
Stata cheatsheet transformation
Stata cheat sheet: data transformation
DPLYR package in R
Stata Programming Cheat Sheet
Stata cheat sheet: data processing
Grouping & Summarizing Data in R
2. R-basics, Vectors, Arrays, Matrices, Factors
Stata Cheat Sheets (all)
Ad

Similar to Manipulating data with dates (20)

PDF
SessionSeven_WorkingWithDatesandTime
DOCX
Pumps, Compressors and Turbine Fault Frequency Analysis
PDF
Pumps, Compressors and Turbine Fault Frequency Analysis
DOCX
Dynamic date parameters in bi report
PDF
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
PPT
MySQL Functions
PPTX
Chris Seebacher Portfolio
TXT
Data20161007
PPT
MySQL Built-In Functions
 
ODP
Introducing Modern Perl
PPT
My Portfolio
PPT
My Portfolio
PPTX
Extending Slate Queries & Reports with JSON & JQUERY
KEY
Apache Velocity 1.6
PDF
Introduction to R
PPT
Chris Mc Glothen Sql Portfolio
PDF
Data Exploration with Apache Drill: Day 2
PDF
R code for data manipulation
DOC
Webtechnology lab
PPT
3.pointers in c programming language.ppt
SessionSeven_WorkingWithDatesandTime
Pumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency Analysis
Dynamic date parameters in bi report
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
MySQL Functions
Chris Seebacher Portfolio
Data20161007
MySQL Built-In Functions
 
Introducing Modern Perl
My Portfolio
My Portfolio
Extending Slate Queries & Reports with JSON & JQUERY
Apache Velocity 1.6
Introduction to R
Chris Mc Glothen Sql Portfolio
Data Exploration with Apache Drill: Day 2
R code for data manipulation
Webtechnology lab
3.pointers in c programming language.ppt
Ad

More from Rupak Roy (20)

PDF
Hierarchical Clustering - Text Mining/NLP
PDF
Clustering K means and Hierarchical - NLP
PDF
Network Analysis - NLP
PDF
Topic Modeling - NLP
PDF
Sentiment Analysis Practical Steps
PDF
NLP - Sentiment Analysis
PDF
Text Mining using Regular Expressions
PDF
Introduction to Text Mining
PDF
Apache Hbase Architecture
PDF
Introduction to Hbase
PDF
Apache Hive Table Partition and HQL
PDF
Installing Apache Hive, internal and external table, import-export
PDF
Introductive to Hive
PDF
Scoop Job, import and export to RDBMS
PDF
Apache Scoop - Import with Append mode and Last Modified mode
PDF
Introduction to scoop and its functions
PDF
Introduction to Flume
PDF
Passing Parameters using File and Command Line
PDF
Apache PIG casting, reference
PDF
Pig Latin, Data Model with Load and Store Functions
Hierarchical Clustering - Text Mining/NLP
Clustering K means and Hierarchical - NLP
Network Analysis - NLP
Topic Modeling - NLP
Sentiment Analysis Practical Steps
NLP - Sentiment Analysis
Text Mining using Regular Expressions
Introduction to Text Mining
Apache Hbase Architecture
Introduction to Hbase
Apache Hive Table Partition and HQL
Installing Apache Hive, internal and external table, import-export
Introductive to Hive
Scoop Job, import and export to RDBMS
Apache Scoop - Import with Append mode and Last Modified mode
Introduction to scoop and its functions
Introduction to Flume
Passing Parameters using File and Command Line
Apache PIG casting, reference
Pig Latin, Data Model with Load and Store Functions

Recently uploaded (20)

PPTX
history of c programming in notes for students .pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
System and Network Administraation Chapter 3
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
assetexplorer- product-overview - presentation
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
history of c programming in notes for students .pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
ai tools demonstartion for schools and inter college
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Reimagine Home Health with the Power of Agentic AI​
Designing Intelligence for the Shop Floor.pdf
System and Network Administraation Chapter 3
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
How to Migrate SBCGlobal Email to Yahoo Easily
Softaken Excel to vCard Converter Software.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
assetexplorer- product-overview - presentation
PTS Company Brochure 2025 (1).pdf.......
Odoo Companies in India – Driving Business Transformation.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool

Manipulating data with dates

  • 1. Manipulating Dates Using as.Date, difftime(), as.POSIXct(), as.POSIXlt(), lubridate() Rupak Roy
  • 2. #load the data >datedata<-read.csv(file.choose(),header = TRUE) >str(datedata) #using as.Date(): A factor or a character data type can be easily converted into a date format where as.Date(column_name, orginal_date_format_of the dataset) >datedata$Date.received<-as.Date(datedata$Date.received,"%m/%d/%Y") >datedata$Date.sent.to.company<- as.Date(datedata$Date.sent.to.company,"%m/%d/%Y") >str(datedata) #code for identify different combinations of date format #example: 15/aug/2016:“ %d/%b/%Y ” 13-JUNE-16: “ %d-%B-%y ” as.Date() %d Day of the month %m Month (in decimal) %Y Year (4 digits) %b Month (abbreviated) %y Year (2 digits) %B Month (full name)
  • 3. #once the data is in date format we can easily extract the months and the weeks. >months(datedata$Date.received) >weekdays(datedata$Date.received) #even we can filter the data using months() and weekdays() #sub-setting using base R package >datadate2<-datedata[(weekdays(datedata$Date.received)=="Sunday"),] as.Date() Rupak Roy
  • 4. #using difftime() function we can calculate the time interval / differences without being converted into minutes, hour, weeks, months or years. >difftime(time2, time1, tz, units = c("auto", "secs", "mins", "hours", "days", "weeks")) Where tz= an optional time zone specification to be used for the conversion units= Units in which the results are desired. >difftime(datedata$Date.received,datedata$Date.sent.to.company,units = "days") #this will give the output in a negative value because we are doing time1-time2 rather than time2-time1 i.e 5- 10 = -5 rather than 10-5 =5 #therefore >difftime(datedata$Date.sent.to.company,datedata$Date.received,units = "days") To know more about the difftime() function use >?difftime difftime()
  • 5.  These are date-time conversion functions to manipulate data or the objects of classes "POSIXlt" and "POSIXct" having time information along with the date.  POSIXct is similar to POSIXlt the only difference is POSIXlt stores the data in a list format #saving the current date-time information as an example >datetime<-Sys.time() >class(datetime) >weekdays(datetime) >months(datetime) #convert the date and time into POSIXlt() >datetime<-as.POSIXlt(datetime) >datetime$zone >datetime$hour >datetime$wday as.POSIXct() and as.POSIXlt() Rupak Roy
  • 6.  Lubridate() is an another function that makes it easier to parse and manipulate dates. #install and load the package >install.packages(lubridate) >library(lubridate) >datedata<-read.csv(file.choose(),header = TRUE) >str(datedata) #converting the factor data type into date data type >datedata$Date.received<-mdy(datedata$Date.received) >str(datedata) Where, mdy = lubridate() Code Format dmy() 3/10/2017 mdy() 10/3/2017 dmy_hm() 3/10/2017 23:40 ydm() 2017/3/10 dmy_hms() mdy_hm() mdy_hms() ydm_hm()
  • 7. Next: We will learn an another important function that is combining two or more data sets using merge(). Manipulating Dates Rupak Roy