Skip to content

Commit

Permalink
Evaluate date/time code
Browse files Browse the repository at this point in the history
  • Loading branch information
rdpeng committed Jul 22, 2014
1 parent 030b91e commit 44c42cc
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 82 deletions.
6 changes: 2 additions & 4 deletions 02_RProgramming/Dates/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ framework : io2012 # {io2012, html5slides, shower, dzslides, ...}
highlighter : highlight.js # {highlight.js, prettify, highlight}
hitheme : tomorrow #
url:
lib: ../../libraries
lib: ../../librariesNew
assets: ../../assets
widgets : [mathjax] # {mathjax, quiz, bootstrap}
mode : selfcontained # {standalone, draft}
Expand Down Expand Up @@ -95,13 +95,11 @@ p$sec
Finally, there is the `strptime` function in case your dates are
written in a different format

```r
```{r}
datestring <- c("January 10, 2012 10:40", "December 9, 2011 9:10")
x <- strptime(datestring, "%B %d, %Y %H:%M")
x
## [1] "2012-01-10 10:40:00" "2011-12-09 09:10:00"
class(x)
## [1] "POSIXlt" "POSIXt"
```
I can _never_ remember the formatting strings. Check `?strptime` for details.

Expand Down
205 changes: 131 additions & 74 deletions 02_RProgramming/Dates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,46 @@
<meta name="generator" content="slidify" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<link rel="stylesheet" href="../../libraries/frameworks/io2012/css/default.css" media="all" >
<link rel="stylesheet" href="../../libraries/frameworks/io2012/phone.css"
<link rel="stylesheet" href="../../librariesNew/frameworks/io2012/css/default.css" media="all" >
<link rel="stylesheet" href="../../librariesNew/frameworks/io2012/css/phone.css"
media="only screen and (max-device-width: 480px)" >
<link rel="stylesheet" href="../../libraries/frameworks/io2012/css/slidify.css" >
<link rel="stylesheet" href="../../libraries/highlighters/highlight.js/css/tomorrow.css" />
<base target="_blank"> <!-- This amazingness opens all links in a new tab. -->
<script data-main="../../libraries/frameworks/io2012/js/slides"
src="../../libraries/frameworks/io2012/js/require-1.0.8.min.js">
<link rel="stylesheet" href="../../librariesNew/frameworks/io2012/css/slidify.css" >
<link rel="stylesheet" href="../../librariesNew/highlighters/highlight.js/css/tomorrow.css" />
<base target="_blank"> <!-- This amazingness opens all links in a new tab. -->

<!-- Grab CDN jQuery, fall back to local if offline -->
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.min.js"></script>
<script>window.jQuery || document.write('<script src="../../librariesNew/widgets/quiz/js/jquery.js"><\/script>')</script>
<script data-main="../../librariesNew/frameworks/io2012/js/slides"
src="../../librariesNew/frameworks/io2012/js/require-1.0.8.min.js">
</script>

<link rel="stylesheet" href = "../../assets/css/custom.css">
<link rel="stylesheet" href = "../../assets/css/custom.css.BACKUP.546.css">
<link rel="stylesheet" href = "../../assets/css/custom.css.BASE.546.css">
<link rel="stylesheet" href = "../../assets/css/custom.css.LOCAL.546.css">
<link rel="stylesheet" href = "../../assets/css/custom.css.orig">
<link rel="stylesheet" href = "../../assets/css/custom.css.REMOTE.546.css">
<link rel="stylesheet" href = "../../assets/css/ribbons.css">


</head>
<body style="opacity: 0">
<slides class="layout-widescreen">

<!-- LOGO SLIDE -->
<!-- END LOGO SLIDE -->
<slide class="title-slide segue nobackground">
<aside class="gdbar">
<img src="../../assets/img/bloomberg_shield.png">
</aside>
<hgroup class="auto-fadein">
<h1>Dates and Times in R</h1>
<h2></h2>
<p>Roger D. Peng, Associate Professor of Biostatistics<br/>Johns Hopkins Bloomberg School of Public Health</p>
</hgroup>
<article></article>
</slide>


<!-- TITLE SLIDE -->
<!-- Should I move this to a Local Layout File? -->
<slide class="title-slide segue nobackground">
<aside class="gdbar">
<img src="../../assets/img/bloomberg_shield.png">
</aside>
<hgroup class="auto-fadein">
<h1>Dates and Times in R</h1>
<h2></h2>
<p>Roger D. Peng, Associate Professor of Biostatistics<br/>Johns Hopkins Bloomberg School of Public Health</p>
</hgroup>
</slide>

<!-- SLIDES -->
<slide class="" id="slide-1" style="background:;">
<slide class="" id="slide-1" style="background:;">
<hgroup>
<h2>Dates and Times in R</h2>
</hgroup>
<article>
<article data-timings="">
<p>R has developed a special representation of dates and times</p>

<ul>
Expand All @@ -66,11 +61,11 @@ <h2>Dates and Times in R</h2>
<!-- Presenter Notes -->
</slide>

<slide class="" id="slide-2" style="background:;">
<slide class="" id="slide-2" style="background:;">
<hgroup>
<h2>Dates in R</h2>
</hgroup>
<article>
<article data-timings="">
<p>Dates are represented by the Date class and can be coerced from a character string using the <code>as.Date()</code> function.</p>

<pre><code class="r">x &lt;- as.Date(&quot;1970-01-01&quot;)
Expand All @@ -86,11 +81,11 @@ <h2>Dates in R</h2>
<!-- Presenter Notes -->
</slide>

<slide class="" id="slide-3" style="background:;">
<slide class="" id="slide-3" style="background:;">
<hgroup>
<h2>Times in R</h2>
</hgroup>
<article>
<article data-timings="">
<p>Times are represented using the <code>POSIXct</code> or the <code>POSIXlt</code> class</p>

<ul>
Expand All @@ -110,11 +105,11 @@ <h2>Times in R</h2>
<!-- Presenter Notes -->
</slide>

<slide class="" id="slide-4" style="background:;">
<slide class="" id="slide-4" style="background:;">
<hgroup>
<h2>Times in R</h2>
</hgroup>
<article>
<article data-timings="">
<p>Times can be coerced from a character string using the <code>as.POSIXlt</code> or <code>as.POSIXct</code> function.</p>

<pre><code class="r">x &lt;- Sys.time()
Expand All @@ -132,11 +127,11 @@ <h2>Times in R</h2>
<!-- Presenter Notes -->
</slide>

<slide class="" id="slide-5" style="background:;">
<slide class="" id="slide-5" style="background:;">
<hgroup>
<h2>Times in R</h2>
</hgroup>
<article>
<article data-timings="">
<p>You can also use the <code>POSIXct</code> format.</p>

<pre><code class="r">x &lt;- Sys.time()
Expand All @@ -155,19 +150,26 @@ <h2>Times in R</h2>
<!-- Presenter Notes -->
</slide>

<slide class="" id="slide-6" style="background:;">
<slide class="" id="slide-6" style="background:;">
<hgroup>
<h2>Times in R</h2>
</hgroup>
<article>
<p>Finally, there is the <code>strptime</code> function in case your dates are written in a different format</p>
<article data-timings="">
<p>Finally, there is the <code>strptime</code> function in case your dates are
written in a different format</p>

<pre><code class="r">datestring &lt;- c(&quot;January 10, 2012 10:40&quot;, &quot;December 9, 2011
<pre><code class="r">datestring &lt;- c(&quot;January 10, 2012 10:40&quot;, &quot;December 9, 2011 9:10&quot;)
x &lt;- strptime(datestring, &quot;%B %d, %Y %H:%M&quot;)
x
## [1] &quot;2012-01-10 10:40:00&quot; &quot;2011-12-09 09:10:00&quot;
class(x)
## [1] &quot;POSIXlt&quot; &quot;POSIXt&quot;
</code></pre>

<pre><code>## [1] &quot;2012-01-10 10:40:00 EST&quot; &quot;2011-12-09 09:10:00 EST&quot;
</code></pre>

<pre><code class="r">class(x)
</code></pre>

<pre><code>## [1] &quot;POSIXlt&quot; &quot;POSIXt&quot;
</code></pre>

<p>I can <em>never</em> remember the formatting strings. Check <code>?strptime</code> for details.</p>
Expand All @@ -176,11 +178,11 @@ <h2>Times in R</h2>
<!-- Presenter Notes -->
</slide>

<slide class="" id="slide-7" style="background:;">
<slide class="" id="slide-7" style="background:;">
<hgroup>
<h2>Operations on Dates and Times</h2>
</hgroup>
<article>
<article data-timings="">
<p>You can use mathematical operations on dates and times. Well, really just + and -. You can do comparisons too (i.e. ==, &lt;=)</p>

<pre><code class="r">x &lt;- as.Date(&quot;2012-01-01&quot;)
Expand All @@ -198,11 +200,11 @@ <h2>Operations on Dates and Times</h2>
<!-- Presenter Notes -->
</slide>

<slide class="" id="slide-8" style="background:;">
<slide class="" id="slide-8" style="background:;">
<hgroup>
<h2>Operations on Dates and Times</h2>
</hgroup>
<article>
<article data-timings="">
<p>Even keeps track of leap years, leap seconds, daylight savings, and time zones.</p>

<pre><code class="r">x &lt;- as.Date(&quot;2012-03-01&quot;) y &lt;- as.Date(&quot;2012-02-28&quot;)
Expand All @@ -218,11 +220,11 @@ <h2>Operations on Dates and Times</h2>
<!-- Presenter Notes -->
</slide>

<slide class="" id="slide-9" style="background:;">
<slide class="" id="slide-9" style="background:;">
<hgroup>
<h2>Summary</h2>
</hgroup>
<article>
<article data-timings="">
<ul>
<li>Dates and times have special classes in R that allow for numerical and statistical calculations</li>
<li>Dates use the <code>Date</code> class</li>
Expand All @@ -236,34 +238,89 @@ <h2>Summary</h2>

<slide class="backdrop"></slide>
</slides>

<!--[if IE]>
<div class="pagination pagination-small" id='io2012-ptoc' style="display:none;">
<ul>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=1 title='Dates and Times in R'>
1
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=2 title='Dates in R'>
2
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=3 title='Times in R'>
3
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=4 title='Times in R'>
4
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=5 title='Times in R'>
5
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=6 title='Times in R'>
6
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=7 title='Operations on Dates and Times'>
7
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=8 title='Operations on Dates and Times'>
8
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=9 title='Summary'>
9
</a>
</li>
</ul>
</div> <!--[if IE]>
<script
src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js">
</script>
<script>CFInstall.check({mode: 'overlay'});</script>
<![endif]-->
</body>
<!-- Grab CDN jQuery, fall back to local if offline -->
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.min.js"></script>
<script>window.jQuery || document.write('<script src="../../libraries/widgets/quiz/js/jquery-1.7.min.js"><\/script>')</script>
<!-- Load Javascripts for Widgets -->
<!-- MathJax: Fall back to local if CDN offline but local image fonts are not supported (saves >100MB) -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'], ['\\(','\\)']],
processEscapes: true
}
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<!-- <script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script> -->
<script>window.MathJax || document.write('<script type="text/x-mathjax-config">MathJax.Hub.Config({"HTML-CSS":{imageFont:null}});<\/script><script src="../../libraries/widgets/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"><\/script>')
<!-- Load Javascripts for Widgets -->

<!-- MathJax: Fall back to local if CDN offline but local image fonts are not supported (saves >100MB) -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'], ['\\(','\\)']],
processEscapes: true
}
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<!-- <script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script> -->
<script>window.MathJax || document.write('<script type="text/x-mathjax-config">MathJax.Hub.Config({"HTML-CSS":{imageFont:null}});<\/script><script src="../../librariesNew/widgets/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"><\/script>')
</script>
<!-- LOAD HIGHLIGHTER JS FILES -->
<script src="../../libraries/highlighters/highlight.js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<!-- DONE LOADING HIGHLIGHTER JS FILES -->
</html>
<script src="../../librariesNew/highlighters/highlight.js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<!-- DONE LOADING HIGHLIGHTER JS FILES -->

</html>
19 changes: 15 additions & 4 deletions 02_RProgramming/Dates/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ framework : io2012 # {io2012, html5slides, shower, dzslides, ...}
highlighter : highlight.js # {highlight.js, prettify, highlight}
hitheme : tomorrow #
url:
lib: ../../libraries
lib: ../../librariesNew
assets: ../../assets
widgets : [mathjax] # {mathjax, quiz, bootstrap}
mode : selfcontained # {standalone, draft}
Expand Down Expand Up @@ -92,14 +92,25 @@ p$sec

## Times in R

Finally, there is the `strptime` function in case your dates are written in a different format
Finally, there is the `strptime` function in case your dates are
written in a different format


```r
datestring <- c("January 10, 2012 10:40", "December 9, 2011
datestring <- c("January 10, 2012 10:40", "December 9, 2011 9:10")
x <- strptime(datestring, "%B %d, %Y %H:%M")
x
## [1] "2012-01-10 10:40:00" "2011-12-09 09:10:00"
```

```
## [1] "2012-01-10 10:40:00 EST" "2011-12-09 09:10:00 EST"
```

```r
class(x)
```

```
## [1] "POSIXlt" "POSIXt"
```
I can _never_ remember the formatting strings. Check `?strptime` for details.
Expand Down

0 comments on commit 44c42cc

Please sign in to comment.