What Time Is It On Mars?
R Programming in Data Science: Dates and Times

What Time Is It On Mars?

That question can't be answered without asking more questions: Do you want the answer in relation to Mars time or Earth time? Since Mars has two moons, does it have twice as many months? Is a year 686 days or 365 days?

Calculating dates and times on earth are only slightly less complex. Data collected over a month's time might have 30 observations. Or 31. Or 28. Or 29. Even a consistent data series will yield inconsistent results if your calculations don't account for the variation in time. Programming Dates and Times in R is tricky business and a a complex programming challenge.

R Programming for Data Science: Dates and Times in R

LinkedIn Learning just released R Programming for Data Science: Dates and Times. It's part of a series of courses on R programming available by subscription or individual purchase. This course shows how to store, retrieve, format, compare, add, subtract, and extract dates and times using built-in R functions. It also covers specialized R packages such as lubridate, busdater, zoo, timelineR, anytime, datetime, and more.

An Example:

Base R has three data structures: Date, POSIXct, and POSIXlt. In the Dates and Times course, this is illustrated with the following code.

HAL_online <- as.Date("1992-01-12")


HAL_online now contains a Date structure, which is the number of days since January 1st, 1970. The Date structure doesn't account for times.

as.numeric(HAL_online) # which produces 8046
# 8046 is the number of days between 1970-01-01 and 1992-01-12

But...what if you define HAL_online as POSIXct?

HAL_online <- as.POSIXct("1992-01-12 11:23:34", tz = "America/Chicago")
as.integer(HAL_online)

# result is 695237014

POSIXct defines a date & time by the number of seconds since January 1, 1970. So now it's important to declare a time zone.

But wait - Base R has a third way to declare dates: POSIXlt.

HAL_online <- as.POSIXlt("1992-01-12 11:23:34", tz = "America/Chicago")
as.integer(HAL_online) # no longer an integer

# which produces ....
 [1] 34 23 11 12  0 92  0 11  0 NA NA
Warning message:
NAs introduced by coercion 

...which produces an odd set of numbers and a warning message. What is this?

That's because POSIXlt is a list of values. Unclass will reveal the structure...

> unclass(HAL_online) # this shows the components of the list
$sec
[1] 34


$min
[1] 23


$hour
[1] 11


$mday
[1] 12


$mon
[1] 0


$year
[1] 92


$wday
[1] 0


$yday
[1] 11


$isdst
[1] 0


$zone
[1] "CST"


$gmtoff
[1] NA


attr(,"tzone")
[1] "America/Chicago"

At this point, someone will bring up lubridate, a part of the tidyverse. Before you jump to that solution, you might want to know more about how it stores dates and times. You'll be pleased to know it's covered in the Date and Time course as well.

R Programming for Data Science: Dates and Times can be watched with your subscription, or purchased individually for $39.99

#linkedinlearning #rstats #r


要查看或添加评论,请登录

Mark Niemann-Ross的更多文章

  • Documenting My Code ... For Me

    Documenting My Code ... For Me

    There are two signs of old age: old age, and ..

  • R Meets Hardware

    R Meets Hardware

    R is a programming language for statistical computing and data visualization. It has been adopted in the fields of data…

    2 条评论
  • Party Buzz Kill: modifying data

    Party Buzz Kill: modifying data

    So Steve (SQL), Marsha (C), Bob (Python), and I (R) are at this party. We have TOTALLY cleared the room, especially now…

    2 条评论
  • Rain - Evapotranspiration = mm Water

    Rain - Evapotranspiration = mm Water

    "Eeee-VAP-oooo-TRANS-PURR-ation," I savor the word as I release it into our conversation. I'm still at the party with…

  • Party Buzz Kill: Data Storage

    Party Buzz Kill: Data Storage

    I'm at this party where Bob and Marsha and I are discussing the best languages for programming a Raspberry Pi. Bob…

    5 条评论
  • R Waters My Garden

    R Waters My Garden

    I'm at a party, and the topic of programming languages comes up. A quarter of the room politely leaves, another half…

    10 条评论
  • Caning and Naming

    Caning and Naming

    We've been back from Port Townsend for a week. Progress on the boat isn't as dramatic as it is when we're spending the…

    1 条评论
  • Irrigate with R and Raspberry Pi

    Irrigate with R and Raspberry Pi

    I’m working on my irrigation system. This requires a controller to turn it on and off.

    3 条评论
  • 5 Reasons to Learn Natural Language Processing with R

    5 Reasons to Learn Natural Language Processing with R

    Why learn R? Why learn Natural Language Processing? Here's five reasons..

    1 条评论
  • Performing Natural Language Processing with R

    Performing Natural Language Processing with R

    I recently released a course on Educative covering topics in Natural Language Processing. Different Learners -…

    1 条评论

社区洞察

其他会员也浏览了