Skip to Content
R在数据科学中的应用,第2版
book

R在数据科学中的应用,第2版

by Hadley Wickham, Mine Cetinkaya-Rundel, Garrett Grolemund
May 2025
Intermediate to advanced
578 pages
8h 9m
Chinese
O'Reilly Media, Inc.
Content preview from R在数据科学中的应用,第2版

第 17 章 日期和时间 日期和时间

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

导言

乍一看,日期和时间似乎很简单。你在平时的生活中经常使用它们,而且它们似乎不会引起太多困惑。然而,对日期和时间了解得越多,它们似乎就越复杂!

热身时,想想一年有多少天,一天有多少小时。你可能记得大多数年份都是 365 天,但闰年是 366 天。你知道判断一年是否是闰年的完整规则吗?1一天中的小时数就不那么明显了:大多数日子都是 24 小时,但在使用夏令时(DST)的地方,每年有一天是 23 小时,另一天是 25 小时。

日期和时间很难处理,因为它们必须协调两种物理现象(地球自转和地球绕太阳公转)和一系列地缘政治现象,包括月份、时区和夏令时。本章不会教你关于日期和时间的每一个细节,但会为你打下坚实的实用技能基础,帮助你应对常见的数据分析难题。

首先,我们将向你展示如何从各种输入中创建日期时间,然后在获得日期时间后,你将学习如何提取年、月和日等组件。然后,我们将深入探讨如何处理时间跨度这个棘手的问题。最后,我们将简要讨论时区带来的额外挑战。

先决条件

本章将重点介绍 lubridate 软件包,它使在 R 中处理日期和时间变得更容易。从最新的 tidyverse 版本开始,lubridate 已成为核心 tidyverse 的一部分。我们还需要 nycflights13 来获取实践数据。

library(tidyverse)
library(nycflights13)

创建日期/时间

有三种类型的日期/时间数据指的是瞬间时间:

  • 一个日期。Tibbles 将其打印为<date>

  • 一天之内的时间Tibbles 将此打印为<time>

  • 日期-时间是一个日期加上一个时间:它唯一标识时间中的一瞬间(通常精确到秒)。Tibbles 将其打印为<dttm> 。Base R 将其称为 POSIXct,但这并不十分顺口。

在本章中,我们将重点讨论日期和日期-时间,因为 R 没有存储时间的本地类。如果需要,可以使用 hms 软件包。

您应该始终使用最简单的数据类型来满足您的需求。也就是说,如果可以使用日期而不是日期-时间,就应该使用。由于需要处理时区,日期时间要复杂得多,这一点我们将在本章结尾再讨论。

要获取当前日期或日期时间,可以使用 today()now():

today()
#> [1] "2023-03-12"
now()
#> [1] "2023-03-12 13:07:31 CDT"

否则,下面将介绍创建日期/时间的四种方法:

  • 使用 readr 读取文件时
  • 从字符串
  • 从单个日期时间组件
  • 来自现有日期/时间对象

导入期间

如果您的 CSV 包含 ISO8601 日期或日期时间,您无需做任何操作,readr 会自动识别:

csv <- "
  date,datetime
  2022-01-02,2022-01-02 05:12
"
read_csv(csv)
#> # A tibble: 1 × 2
#>   date       datetime           
#>   <date>     <dttm>             
#> 1 2022-01-02 2022-01-02 05:12:00

如果您以前没有听说过ISO8601,它是一种书写日期的国际标准,其中日期的组成部分从大到小用- 分隔。例如,在 ISO8601 中,2022 年 5 月 3 日是2022-05-03 。ISO8601 日期还可以包括时间,其中小时、分钟和秒用 ...

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

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

R深度学习权威指南

R深度学习权威指南

Posts & Telecom Press, Joshua F. Wiley
AI工程

AI工程

Chip Huyen
Raku学习手册

Raku学习手册

brian d foy

Publisher Resources

ISBN: 9798341657304