diff --git a/02_RProgramming/Dates/index.Rmd b/02_RProgramming/Dates/index.Rmd index 52462db80..d435714c4 100644 --- a/02_RProgramming/Dates/index.Rmd +++ b/02_RProgramming/Dates/index.Rmd @@ -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} @@ -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. diff --git a/02_RProgramming/Dates/index.html b/02_RProgramming/Dates/index.html index b0fe8124f..c3d93923d 100644 --- a/02_RProgramming/Dates/index.html +++ b/02_RProgramming/Dates/index.html @@ -8,51 +8,46 @@ - - + - - - - + + - - - - - - - + - + + +
+

Dates and Times in R

+

+

Roger D. Peng, Associate Professor of Biostatistics
Johns Hopkins Bloomberg School of Public Health

+
+
+
- - - - -
-

Dates and Times in R

-

-

Roger D. Peng, Associate Professor of Biostatistics
Johns Hopkins Bloomberg School of Public Health

-
-
- - +

Dates and Times in R

-
+

R has developed a special representation of dates and times

    @@ -66,11 +61,11 @@

    Dates and Times in R

    - +

    Dates in R

    -
    +

    Dates are represented by the Date class and can be coerced from a character string using the as.Date() function.

    x <- as.Date("1970-01-01")
    @@ -86,11 +81,11 @@ 

    Dates in R

    - +

    Times in R

    -
    +

    Times are represented using the POSIXct or the POSIXlt class

      @@ -110,11 +105,11 @@

      Times in R

      - +

      Times in R

      -
      +

      Times can be coerced from a character string using the as.POSIXlt or as.POSIXct function.

      x <- Sys.time()
      @@ -132,11 +127,11 @@ 

      Times in R

      - +

      Times in R

      -
      +

      You can also use the POSIXct format.

      x <- Sys.time()
      @@ -155,19 +150,26 @@ 

      Times in R

      - +

      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

      -
      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"
      -class(x)
      -## [1] "POSIXlt" "POSIXt"
      +
      + +
      ## [1] "2012-01-10 10:40:00 EST" "2011-12-09 09:10:00 EST"
      +
      + +
      class(x)
      +
      + +
      ## [1] "POSIXlt" "POSIXt"
       

      I can never remember the formatting strings. Check ?strptime for details.

      @@ -176,11 +178,11 @@

      Times in R

      - +

      Operations on Dates and Times

      -
      +

      You can use mathematical operations on dates and times. Well, really just + and -. You can do comparisons too (i.e. ==, <=)

      x <- as.Date("2012-01-01")
      @@ -198,11 +200,11 @@ 

      Operations on Dates and Times

      - +

      Operations on Dates and Times

      -
      +

      Even keeps track of leap years, leap seconds, daylight savings, and time zones.

      x <- as.Date("2012-03-01") y <- as.Date("2012-02-28") 
      @@ -218,11 +220,11 @@ 

      Operations on Dates and Times

      - +

      Summary

      -
      +
      • Dates and times have special classes in R that allow for numerical and statistical calculations
      • Dates use the Date class
      • @@ -236,34 +238,89 @@

        Summary

        - - - - - - - - - - - + + + - - - - \ No newline at end of file + + + + + \ No newline at end of file diff --git a/02_RProgramming/Dates/index.md b/02_RProgramming/Dates/index.md index fff705469..5e6c6789c 100644 --- a/02_RProgramming/Dates/index.md +++ b/02_RProgramming/Dates/index.md @@ -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} @@ -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.