Chapter 7. Strings and Dates
Strings? Dates? In a statistical programming package?
As soon as you read files or print reports, you need strings. When you work with real-world problems, you need dates.
R has facilities for both strings and dates. They are clumsy compared to string-oriented languages such as Perl, but then it’s a matter of the right tool for the job. We wouldn’t want to perform logistic regression in Perl.
Some of this clunkiness with strings and dates has been improved through
the tidyverse packages stringr and lubridate. As with other chapters
in this book, the examples here will pull from Base R as well as
add-on packages that make life easier, faster, and more convenient.
Classes for Dates and Times
R has a variety of classes for working with dates and times, which is nice if you prefer having a choice but annoying if you prefer living simply. There is a critical distinction among the classes: some are date-only classes, some are datetime classes. All classes can handle calendar dates (e.g., March 15, 2019), but not all can represent a datetime (11:45 AM on March 1, 2019).
The following classes are included in the base distribution of R:
Date-
The
Dateclass can represent a calendar date but not a clock time. It is a solid, general-purpose class for working with dates, including conversions, formatting, basic date arithmetic, and time-zone handling. Most of the date-related recipes in this book are built on theDateclass. POSIXct-
This is a datetime class, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access