SlideShare ist ein Scribd-Unternehmen logo
1 von 60
Downloaden Sie, um offline zu lesen
Today we will use the ggplot2 and lubridate
packages
install.packages("lubridate")
library(ggplot2) #load ïŹrst!
library(lubridate) #load second!
Garrett Grolemund
Phd Student / Rice University
Department of Statistics
Dates and
Times
1. four more table rules
2. manipulating date-times
3. manipulating time spans (i.e, math
with date-times)
Table style
guidelines
1. When to use a table
Use a table instead of a graphic if:
a) there is only a small amount of data, or
b) precision is important
2. SigniïŹcant digits
Pick an appropriate amount of signiïŹcant
digits (at most 4 or 5)
Use signif( ) to round data to that amount
3. Align decimals
Ensure that decimal points line up so that
differences in order of magnitude are easy
to spot.
3. Align decimals
Ensure that decimal points line up so that
differences in order of magnitude are easy
to spot.
4. Include captions
Always include a captions.
Less enthusiastic readers will only look at
your ïŹgures, so try to summarize the
whole story there.
Captions should explain what data the
ïŹgure shows and highlight the important
ïŹnding.
date-times
in R
Time is a measurement system similar to
the number system (which measures
quantity). Just as numbers can be
arranged on a number line, times can be
arranged on a time line
monotonically increasing
0 CEBC AD
0 CE
A date-time is a speciïŹc instant of time. It
refers to an exact point on the time line.
For example,
January 1st, 2000 12:34:00, or
right now
January 1st,
2000 12:34:00
right now
0 CE
2000-01-01 12:34:00
Identifying instants
x seconds since 0 CE
Instants of time are commonly identiïŹed in two
ways:
1) as the number of seconds since a reference time
2) by a unique combination of year, month, day,
hour, minute, second, and time zone values
R stores date-times as either POSIXct or a POSIXlt
objects.
POSIXct objects are stored as the number of seconds
since a reference time (by default 1970-01-01 00:00:00
UTC)
unclass(now())
POSIXlt objects are stored as a unique combination of
year, month, day, hour, minute, second, and time zone
values
unclass(as.POSIXlt(now())
Parsing dates
Parse character strings into date-times
with the ymd() type functions in lubridate
e.g, ymd("2010-11-01")*
*note: this is also a quick way to create new dates
Parsing dates
use the function whose name matches the
order of the elements in the date
ymd("2010-11-02")
dmy("02/11/2010")
mdy("11.02.10")
ymd_hms("2010-11-02 04:22:58")
Your turn
Parse the column of dates in emails.csv
into POSIXct objects.
Access a speciïŹc element of a date-time with the
function that has the element’s name
now()
year(now())
hour(now())
day(now())
yday(now())
wday(now())
wday(now(), label = TRUE)
wday(now(), label = TRUE, abbr = FALSE)
month(now(), label = TRUE, abbr = FALSE)
Determine which day of
the week you were born on
Your turn
Use the email data to determine at which
hour of the day Professor Wickham is
most likely to respond to your email.
em <- read.csv("emails.csv")
em$hour <- hour(em$time)
qplot(hour, data = em, binwidth = 1)
Accessor functions can also be used to
change the elements of a date-time
now()
year(now()) <- 1999
hour(now()) <- 23
day(now()) <- 45
tz(now()) <- "UTC"
Determine what day of
the week your birthday
will be on next year
Time zones
Different clock times. All refer to the same
instant of time
2010-11-01
03:53:06 PDT
2010-11-01
04:53:06 MDT
2010-11-01
05:53:06 PDT
2010-11-01
06:53:06 EDT
Switch between time zones with
with_tz()
with_tz(now(), "UTC")
with_tz(now(), "America/New_York")
Same clock times. All refer to the different
instants of time
2010-11-01
03:53:06 PDT
2010-11-01
03:53:06 MDT
2010-11-01
03:53:06 PDT
2010-11-01
03:53:06 EDT
Alter time zone with force_tz()
Recall that force_tz() returns a new instant
of time
force_tz(now(), "UTC")
force_tz(now(), "America/New_York")
What time is it now where Hadley is
(London)? What time was it here when
London clocks displayed our current
time?
rounding instants
We can round an instant to a speciïŹed
unit using ïŹ‚oor_date(), ceiling_date(),
round_date()
e.g.
round_date(now(), "day")
round_date(now(), "month")
Your turn
Use ddply to calculate how many emails
Hadley received per day since September
1st. How will you subset? How will you
pair up emails sent on the same day?
Plot the number of emails per day over
time. Compare the number of emails sent
by day of the week.
# just the recent emails
recent <- subset(em, time > ymd("2010-09-01"))
# binning into days
recent$day <- lubridate::ïŹ‚oor_date(recent$time,
"day")
# calculating daily totals
> daily <- ddply(recent, "day", summarise, words
= sum(words), emails = length(day))
qplot(day, emails, data = daily, geom = "line")
qplot(wday(day, label = T), emails, data = daily,
geom = "boxplot")
Time spans/
math with
date-times
0 CE
A time span is a period of time. It refers to
an interval on the time line. For example,
19 months, or
one century
one century 19 months
What does time
measure?
- one half of something called space-time?
- position of the sun?
- tilt of the Earth’s axis?
- number of days left until the weekend?
- All of the above (and poorly at that)?
The month suggests the tilt of the Earth’s
axis (but requires a leap day to get back
in sync)
The hour suggests where the sun is in the
sky (but requires time zones and daylight
savings)
The Earth’s movement is decelerating, but
space-time is constant (which requires
random leap seconds)
as.period(diff(.leap.seconds))
Consider
Suppose we wish to record the opening value of
the S&P 500 everyday for a month. Since the
stock market opens at 8:30 CST, we could
calculate:
force_tz(ymd_hms("2010-01-01_08:30:00"), "") +
ddays(0:30)
how about
force_tz(ymd_hms("2010-03-01_08:30:00"), "") +
ddays(0:30)
What went wrong?
Why does this matter?
What do we mean by exactly one month
from now?
How long is an hour?
How long will an hour be at 2:00 this
sunday morning?
According to clock times, the time line
looks more like this
day light savings leap day
day light savings
3 types of time spans
We can still do math on the time line, as
long as we’re speciïŹc about how we want
to measure time.
We can use 3 types of time spans, each
measures time differently: durations,
periods, and intervals
DST <- force_tz(ymd_hms("2010-11-7 01:06:39"), "")
durations
Durations measure the exact amount of
seconds that pass between two time
points.
DST + ddays(1)
Use new_duration() or a helper function to create a
durations object. Helper functions are named d + the
plural of the object you are trying to create.
new_duration(3601)
new_duration(minute = 5)
dminutes(5)
dhours(278)
dmonths(4) #no dmonths()
Durations are appropriate when you wish
to measure a time span exactly, or
compare tow time spans. For example,
- the radioactive half life of an atom
- the lifespan of two brands of lightbulb
- the speed of a baseball pitch
Periods measure time spans in units
larger than seconds. Periods pay no
attention to how many sub-units occur
during the unit of measurement.
periods
DST + days(1)
{
{
No surprises.
2010-11-01 00:00:00 + months(1) will always
equal 2010-12-01 00:00:00 no matter how many
leap seconds, leap days or changes in DST
occur in between
Why use periods?
=
Why not use periods?
We cannot accurately compare two
periods unless we know when they occur.
1 month = 31 days
January = 31 days
February = 31 days
?
Use new_period() or a helper function to create a
period object. Helper functions are simply the plural
of the object you are trying to create.
new_period(3601)
new_period(minute = 5)
minutes(5)
hours(278)
months(4) # months are not a problem
Periods are appropriate when you wish to
model events that depend on the clock
time. For example,
- the opening bell of a stock market
- quarterly earnings reports
- reoccurring deadlines
parsing time spans
a time span that contains only hours,
minutes, and seconds information can be
parsed as a period with hms(), hm(), ms(),
hours(), minutes(), or seconds()
e.g ms("11:45")
Intervals measure a time span by
recording its endpoints. Since we know
when the time span occurs, we can
calculate the lengths of all the units
involved.
intervals
{
Intervals retain all of the information
available about a time span, but cannot be
generalized to other spots on the time line.
Intervals can be accurately converted to
either periods or durations with as.period()
and as.duration()
Create an interval with new_interval() or by
subtracting two dates.
int <- ymd("2010-01-01") - ymd("2009-01-01")
Access and set the endpoints with start() and end().
Note that setting preserves length (in seconds).
start(int)
end(int) <- ymd("2010-03-14")
Intervals are always positive
converting between
time spans
Periods can be converted to durations by
using the most common lengths (in
seconds) of each time unit.
These are just estimates. For accuracy,
convert a period to a interval ïŹrst and
then convert the interval to a duration.
arithmetic with
date-times
time spans are meant to be added and
subtracted to both each other and instants
e.g, now() + days(1) - minutes(25:30)
Challenge
Write a function that takes a date and
returns the last day of the month the date
occurs in.
multiplication/division
Multiplication and division of time spans works as
expected.
Dividing periods with durations or other periods
can only provide an estimate. Convert to intervals
for accuracy.
Dividing intervals by durations creates an exact
answer.
Dividing intervals by periods performs integer
division.
modulo arithmetic and
integer division
integer division
128 %/% 5 = 25
modulo
128 %% 5 = 3
Your turn
Calculate your age in minutes. In hours. In
days. How old is someone who was born
on June 4th, 1958?
lifetime <- now() - ymd("1981-01-22")
lifetime / dminutes(1)
lifetime / dhours(1)
lifetime / ddays(1)
(now() - ymd("1981-01-22")) / years(1)
This work is licensed under the Creative
Commons Attribution-Noncommercial 3.0 United
States License. To view a copy of this license,
visit http://creativecommons.org/licenses/by-nc/
3.0/us/ or send a letter to Creative Commons,
171 Second Street, Suite 300, San Francisco,
California, 94105, USA.

Weitere Àhnliche Inhalte

Andere mochten auch

Graphical inference
Graphical inferenceGraphical inference
Graphical inferenceHadley Wickham
 
Correlations, Trends, and Outliers in ggplot2
Correlations, Trends, and Outliers in ggplot2Correlations, Trends, and Outliers in ggplot2
Correlations, Trends, and Outliers in ggplot2Chris Rucker
 
Model Visualisation (with ggplot2)
Model Visualisation (with ggplot2)Model Visualisation (with ggplot2)
Model Visualisation (with ggplot2)Hadley Wickham
 
R workshop iii -- 3 hours to learn ggplot2 series
R workshop iii -- 3 hours to learn ggplot2 seriesR workshop iii -- 3 hours to learn ggplot2 series
R workshop iii -- 3 hours to learn ggplot2 seriesVivian S. Zhang
 
23 data-structures
23 data-structures23 data-structures
23 data-structuresHadley Wickham
 
Reshaping Data in R
Reshaping Data in RReshaping Data in R
Reshaping Data in RJeffrey Breen
 
Machine learning in R
Machine learning in RMachine learning in R
Machine learning in Rapolol92
 
4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply FunctionSakthi Dasans
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyrRomain Francois
 
Data Manipulation Using R (& dplyr)
Data Manipulation Using R (& dplyr)Data Manipulation Using R (& dplyr)
Data Manipulation Using R (& dplyr)Ram Narasimhan
 
Introducing natural language processing(NLP) with r
Introducing natural language processing(NLP) with rIntroducing natural language processing(NLP) with r
Introducing natural language processing(NLP) with rVivian S. Zhang
 
Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in RJeffrey Breen
 
Elegant Graphics for Data Analysis with ggplot2
Elegant Graphics for Data Analysis with ggplot2Elegant Graphics for Data Analysis with ggplot2
Elegant Graphics for Data Analysis with ggplot2yannabraham
 
Rsplit apply combine
Rsplit apply combineRsplit apply combine
Rsplit apply combineMichelle Darling
 

Andere mochten auch (20)

01 Intro
01 Intro01 Intro
01 Intro
 
Graphical inference
Graphical inferenceGraphical inference
Graphical inference
 
Correlations, Trends, and Outliers in ggplot2
Correlations, Trends, and Outliers in ggplot2Correlations, Trends, and Outliers in ggplot2
Correlations, Trends, and Outliers in ggplot2
 
24 modelling
24 modelling24 modelling
24 modelling
 
16 Sequences
16 Sequences16 Sequences
16 Sequences
 
21 spam
21 spam21 spam
21 spam
 
03 Conditional
03 Conditional03 Conditional
03 Conditional
 
Model Visualisation (with ggplot2)
Model Visualisation (with ggplot2)Model Visualisation (with ggplot2)
Model Visualisation (with ggplot2)
 
R workshop iii -- 3 hours to learn ggplot2 series
R workshop iii -- 3 hours to learn ggplot2 seriesR workshop iii -- 3 hours to learn ggplot2 series
R workshop iii -- 3 hours to learn ggplot2 series
 
23 data-structures
23 data-structures23 data-structures
23 data-structures
 
R packages
R packagesR packages
R packages
 
Reshaping Data in R
Reshaping Data in RReshaping Data in R
Reshaping Data in R
 
Machine learning in R
Machine learning in RMachine learning in R
Machine learning in R
 
4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyr
 
Data Manipulation Using R (& dplyr)
Data Manipulation Using R (& dplyr)Data Manipulation Using R (& dplyr)
Data Manipulation Using R (& dplyr)
 
Introducing natural language processing(NLP) with r
Introducing natural language processing(NLP) with rIntroducing natural language processing(NLP) with r
Introducing natural language processing(NLP) with r
 
Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in R
 
Elegant Graphics for Data Analysis with ggplot2
Elegant Graphics for Data Analysis with ggplot2Elegant Graphics for Data Analysis with ggplot2
Elegant Graphics for Data Analysis with ggplot2
 
Rsplit apply combine
Rsplit apply combineRsplit apply combine
Rsplit apply combine
 

Ähnlich wie 20 date-times

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysisNisha Soms
 
Data Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing ItData Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing Itkanaugust
 
17 ruby date time
17 ruby date time17 ruby date time
17 ruby date timeWalker Maidana
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4Kenji HASUNUMA
 
The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184Mahmoud Samir Fayed
 
Unit5_Time Series Analysis.pdf
Unit5_Time Series  Analysis.pdfUnit5_Time Series  Analysis.pdf
Unit5_Time Series Analysis.pdfKaranvhatkar1
 
Date object.pptx date and object v
Date object.pptx date and object        vDate object.pptx date and object        v
Date object.pptx date and object v22x026
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4Kenji HASUNUMA
 
SessionSeven_WorkingWithDatesandTime
SessionSeven_WorkingWithDatesandTimeSessionSeven_WorkingWithDatesandTime
SessionSeven_WorkingWithDatesandTimeHellen Gakuruh
 
SessionFour_DataTypesandObjects
SessionFour_DataTypesandObjectsSessionFour_DataTypesandObjects
SessionFour_DataTypesandObjectsHellen Gakuruh
 
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)Faysal Shaarani (MBA)
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 
(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_types(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_typesNico Ludwig
 
Making the Right Decision at the Right Time - Introducing Temporal Reasoning ...
Making the Right Decision at the Right Time - Introducing Temporal Reasoning ...Making the Right Decision at the Right Time - Introducing Temporal Reasoning ...
Making the Right Decision at the Right Time - Introducing Temporal Reasoning ...Denis Gagné
 

Ähnlich wie 20 date-times (20)

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Data Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing ItData Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing It
 
Python time
Python timePython time
Python time
 
jkfdlsajfklafj
jkfdlsajfklafjjkfdlsajfklafj
jkfdlsajfklafj
 
doc
docdoc
doc
 
10x10
10x1010x10
10x10
 
17 ruby date time
17 ruby date time17 ruby date time
17 ruby date time
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
 
The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184
 
Unit5_Time Series Analysis.pdf
Unit5_Time Series  Analysis.pdfUnit5_Time Series  Analysis.pdf
Unit5_Time Series Analysis.pdf
 
Date object.pptx date and object v
Date object.pptx date and object        vDate object.pptx date and object        v
Date object.pptx date and object v
 
R time series analysis
R   time series analysisR   time series analysis
R time series analysis
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
 
SessionSeven_WorkingWithDatesandTime
SessionSeven_WorkingWithDatesandTimeSessionSeven_WorkingWithDatesandTime
SessionSeven_WorkingWithDatesandTime
 
SessionFour_DataTypesandObjects
SessionFour_DataTypesandObjectsSessionFour_DataTypesandObjects
SessionFour_DataTypesandObjects
 
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_types(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_types
 
Making the Right Decision at the Right Time - Introducing Temporal Reasoning ...
Making the Right Decision at the Right Time - Introducing Temporal Reasoning ...Making the Right Decision at the Right Time - Introducing Temporal Reasoning ...
Making the Right Decision at the Right Time - Introducing Temporal Reasoning ...
 

Mehr von Hadley Wickham (20)

27 development
27 development27 development
27 development
 
27 development
27 development27 development
27 development
 
22 spam
22 spam22 spam
22 spam
 
19 tables
19 tables19 tables
19 tables
 
18 cleaning
18 cleaning18 cleaning
18 cleaning
 
17 polishing
17 polishing17 polishing
17 polishing
 
16 critique
16 critique16 critique
16 critique
 
15 time-space
15 time-space15 time-space
15 time-space
 
14 case-study
14 case-study14 case-study
14 case-study
 
13 case-study
13 case-study13 case-study
13 case-study
 
12 adv-manip
12 adv-manip12 adv-manip
12 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
10 simulation
10 simulation10 simulation
10 simulation
 
10 simulation
10 simulation10 simulation
10 simulation
 
09 bootstrapping
09 bootstrapping09 bootstrapping
09 bootstrapping
 
08 functions
08 functions08 functions
08 functions
 
07 problem-solving
07 problem-solving07 problem-solving
07 problem-solving
 
06 data
06 data06 data
06 data
 
05 subsetting
05 subsetting05 subsetting
05 subsetting
 

KĂŒrzlich hochgeladen

Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Neil Kimberley
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with CultureSeta Wicaksana
 
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...Any kyc Account
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...Aggregage
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxWorkforce Group
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756dollysharma2066
 
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Delhi Call girls
 
Regression analysis: Simple Linear Regression Multiple Linear Regression
Regression analysis:  Simple Linear Regression Multiple Linear RegressionRegression analysis:  Simple Linear Regression Multiple Linear Regression
Regression analysis: Simple Linear Regression Multiple Linear RegressionRavindra Nath Shukla
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Dipal Arora
 
M.C Lodges -- Guest House in Jhang.
M.C Lodges --  Guest House in Jhang.M.C Lodges --  Guest House in Jhang.
M.C Lodges -- Guest House in Jhang.Aaiza Hassan
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayNZSG
 
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒anilsa9823
 
Value Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and painsValue Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and painsP&CO
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdfRenandantas16
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMRavindra Nath Shukla
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageMatteo Carbone
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxpriyanshujha201
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesDipal Arora
 

KĂŒrzlich hochgeladen (20)

Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptx
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
 
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
 
Regression analysis: Simple Linear Regression Multiple Linear Regression
Regression analysis:  Simple Linear Regression Multiple Linear RegressionRegression analysis:  Simple Linear Regression Multiple Linear Regression
Regression analysis: Simple Linear Regression Multiple Linear Regression
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
 
M.C Lodges -- Guest House in Jhang.
M.C Lodges --  Guest House in Jhang.M.C Lodges --  Guest House in Jhang.
M.C Lodges -- Guest House in Jhang.
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒
 
Value Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and painsValue Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and pains
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSM
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usage
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
 

20 date-times

  • 1. Today we will use the ggplot2 and lubridate packages install.packages("lubridate") library(ggplot2) #load ïŹrst! library(lubridate) #load second!
  • 2. Garrett Grolemund Phd Student / Rice University Department of Statistics Dates and Times
  • 3. 1. four more table rules 2. manipulating date-times 3. manipulating time spans (i.e, math with date-times)
  • 5. 1. When to use a table Use a table instead of a graphic if: a) there is only a small amount of data, or b) precision is important
  • 6. 2. SigniïŹcant digits Pick an appropriate amount of signiïŹcant digits (at most 4 or 5) Use signif( ) to round data to that amount
  • 7. 3. Align decimals Ensure that decimal points line up so that differences in order of magnitude are easy to spot.
  • 8. 3. Align decimals Ensure that decimal points line up so that differences in order of magnitude are easy to spot.
  • 9. 4. Include captions Always include a captions. Less enthusiastic readers will only look at your ïŹgures, so try to summarize the whole story there. Captions should explain what data the ïŹgure shows and highlight the important ïŹnding.
  • 11. Time is a measurement system similar to the number system (which measures quantity). Just as numbers can be arranged on a number line, times can be arranged on a time line monotonically increasing 0 CEBC AD
  • 12. 0 CE A date-time is a speciïŹc instant of time. It refers to an exact point on the time line. For example, January 1st, 2000 12:34:00, or right now January 1st, 2000 12:34:00 right now
  • 13. 0 CE 2000-01-01 12:34:00 Identifying instants x seconds since 0 CE Instants of time are commonly identiïŹed in two ways: 1) as the number of seconds since a reference time 2) by a unique combination of year, month, day, hour, minute, second, and time zone values
  • 14. R stores date-times as either POSIXct or a POSIXlt objects. POSIXct objects are stored as the number of seconds since a reference time (by default 1970-01-01 00:00:00 UTC) unclass(now()) POSIXlt objects are stored as a unique combination of year, month, day, hour, minute, second, and time zone values unclass(as.POSIXlt(now())
  • 15. Parsing dates Parse character strings into date-times with the ymd() type functions in lubridate e.g, ymd("2010-11-01")* *note: this is also a quick way to create new dates
  • 16. Parsing dates use the function whose name matches the order of the elements in the date ymd("2010-11-02") dmy("02/11/2010") mdy("11.02.10") ymd_hms("2010-11-02 04:22:58")
  • 17. Your turn Parse the column of dates in emails.csv into POSIXct objects.
  • 18. Access a speciïŹc element of a date-time with the function that has the element’s name
  • 19. now() year(now()) hour(now()) day(now()) yday(now()) wday(now()) wday(now(), label = TRUE) wday(now(), label = TRUE, abbr = FALSE) month(now(), label = TRUE, abbr = FALSE) Determine which day of the week you were born on
  • 20. Your turn Use the email data to determine at which hour of the day Professor Wickham is most likely to respond to your email.
  • 21. em <- read.csv("emails.csv") em$hour <- hour(em$time) qplot(hour, data = em, binwidth = 1)
  • 22. Accessor functions can also be used to change the elements of a date-time now() year(now()) <- 1999 hour(now()) <- 23 day(now()) <- 45 tz(now()) <- "UTC" Determine what day of the week your birthday will be on next year
  • 24. Different clock times. All refer to the same instant of time 2010-11-01 03:53:06 PDT 2010-11-01 04:53:06 MDT 2010-11-01 05:53:06 PDT 2010-11-01 06:53:06 EDT Switch between time zones with with_tz()
  • 26. Same clock times. All refer to the different instants of time 2010-11-01 03:53:06 PDT 2010-11-01 03:53:06 MDT 2010-11-01 03:53:06 PDT 2010-11-01 03:53:06 EDT Alter time zone with force_tz()
  • 27. Recall that force_tz() returns a new instant of time force_tz(now(), "UTC") force_tz(now(), "America/New_York") What time is it now where Hadley is (London)? What time was it here when London clocks displayed our current time?
  • 28. rounding instants We can round an instant to a speciïŹed unit using ïŹ‚oor_date(), ceiling_date(), round_date() e.g. round_date(now(), "day") round_date(now(), "month")
  • 29. Your turn Use ddply to calculate how many emails Hadley received per day since September 1st. How will you subset? How will you pair up emails sent on the same day? Plot the number of emails per day over time. Compare the number of emails sent by day of the week.
  • 30. # just the recent emails recent <- subset(em, time > ymd("2010-09-01")) # binning into days recent$day <- lubridate::ïŹ‚oor_date(recent$time, "day") # calculating daily totals > daily <- ddply(recent, "day", summarise, words = sum(words), emails = length(day))
  • 31. qplot(day, emails, data = daily, geom = "line") qplot(wday(day, label = T), emails, data = daily, geom = "boxplot")
  • 33. 0 CE A time span is a period of time. It refers to an interval on the time line. For example, 19 months, or one century one century 19 months
  • 34. What does time measure? - one half of something called space-time? - position of the sun? - tilt of the Earth’s axis? - number of days left until the weekend? - All of the above (and poorly at that)?
  • 35. The month suggests the tilt of the Earth’s axis (but requires a leap day to get back in sync) The hour suggests where the sun is in the sky (but requires time zones and daylight savings) The Earth’s movement is decelerating, but space-time is constant (which requires random leap seconds) as.period(diff(.leap.seconds))
  • 36. Consider Suppose we wish to record the opening value of the S&P 500 everyday for a month. Since the stock market opens at 8:30 CST, we could calculate: force_tz(ymd_hms("2010-01-01_08:30:00"), "") + ddays(0:30) how about force_tz(ymd_hms("2010-03-01_08:30:00"), "") + ddays(0:30) What went wrong?
  • 37. Why does this matter? What do we mean by exactly one month from now? How long is an hour? How long will an hour be at 2:00 this sunday morning?
  • 38. According to clock times, the time line looks more like this day light savings leap day day light savings
  • 39. 3 types of time spans We can still do math on the time line, as long as we’re speciïŹc about how we want to measure time. We can use 3 types of time spans, each measures time differently: durations, periods, and intervals
  • 40. DST <- force_tz(ymd_hms("2010-11-7 01:06:39"), "") durations Durations measure the exact amount of seconds that pass between two time points. DST + ddays(1)
  • 41. Use new_duration() or a helper function to create a durations object. Helper functions are named d + the plural of the object you are trying to create. new_duration(3601) new_duration(minute = 5) dminutes(5) dhours(278) dmonths(4) #no dmonths()
  • 42. Durations are appropriate when you wish to measure a time span exactly, or compare tow time spans. For example, - the radioactive half life of an atom - the lifespan of two brands of lightbulb - the speed of a baseball pitch
  • 43. Periods measure time spans in units larger than seconds. Periods pay no attention to how many sub-units occur during the unit of measurement. periods DST + days(1) {
  • 44. { No surprises. 2010-11-01 00:00:00 + months(1) will always equal 2010-12-01 00:00:00 no matter how many leap seconds, leap days or changes in DST occur in between Why use periods? =
  • 45. Why not use periods? We cannot accurately compare two periods unless we know when they occur. 1 month = 31 days January = 31 days February = 31 days ?
  • 46. Use new_period() or a helper function to create a period object. Helper functions are simply the plural of the object you are trying to create. new_period(3601) new_period(minute = 5) minutes(5) hours(278) months(4) # months are not a problem
  • 47. Periods are appropriate when you wish to model events that depend on the clock time. For example, - the opening bell of a stock market - quarterly earnings reports - reoccurring deadlines
  • 48. parsing time spans a time span that contains only hours, minutes, and seconds information can be parsed as a period with hms(), hm(), ms(), hours(), minutes(), or seconds() e.g ms("11:45")
  • 49. Intervals measure a time span by recording its endpoints. Since we know when the time span occurs, we can calculate the lengths of all the units involved. intervals {
  • 50. Intervals retain all of the information available about a time span, but cannot be generalized to other spots on the time line. Intervals can be accurately converted to either periods or durations with as.period() and as.duration()
  • 51. Create an interval with new_interval() or by subtracting two dates. int <- ymd("2010-01-01") - ymd("2009-01-01") Access and set the endpoints with start() and end(). Note that setting preserves length (in seconds). start(int) end(int) <- ymd("2010-03-14") Intervals are always positive
  • 52. converting between time spans Periods can be converted to durations by using the most common lengths (in seconds) of each time unit. These are just estimates. For accuracy, convert a period to a interval ïŹrst and then convert the interval to a duration.
  • 53. arithmetic with date-times time spans are meant to be added and subtracted to both each other and instants e.g, now() + days(1) - minutes(25:30)
  • 54. Challenge Write a function that takes a date and returns the last day of the month the date occurs in.
  • 55. multiplication/division Multiplication and division of time spans works as expected. Dividing periods with durations or other periods can only provide an estimate. Convert to intervals for accuracy. Dividing intervals by durations creates an exact answer. Dividing intervals by periods performs integer division.
  • 56. modulo arithmetic and integer division integer division 128 %/% 5 = 25 modulo 128 %% 5 = 3
  • 57. Your turn Calculate your age in minutes. In hours. In days. How old is someone who was born on June 4th, 1958?
  • 58. lifetime <- now() - ymd("1981-01-22") lifetime / dminutes(1) lifetime / dhours(1) lifetime / ddays(1) (now() - ymd("1981-01-22")) / years(1)
  • 59.
  • 60. This work is licensed under the Creative Commons Attribution-Noncommercial 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/ 3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.