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版

第 22 章 箭 箭头

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

导言

CSV 文件旨在方便人类读取。它们是一种很好的交换格式,因为它们很简单,可以被所有工具读取。但 CSV 文件并不高效:要将数据读入 R,你必须做大量的工作。在本章中,你将了解到一种强大的替代格式:parquet 格式,这是一种基于开放标准的格式,被大数据系统广泛使用。

我们将把 parquet 文件与Apache Arrow 搭配使用,后者是一个多语言工具箱,专为高效分析和传输大型数据集而设计。我们将通过arrow 软件包使用 Apache Arrow,它提供了一个 dplyr 后端,允许你使用熟悉的 dplyr 语法分析比内存更大的数据集。作为额外的好处,arrow 的运行速度极快;你将在本章稍后部分看到一些示例。

arrow 和 dbplyr 都提供了 dplyr 后端,所以你可能会想知道什么时候使用这两种后端。在很多情况下,你已经做出了选择,因为数据已经存在于数据库或 parquet 文件中,你会希望原封不动地使用这些数据。但是,如果您开始使用自己的数据(可能是 CSV 文件),您可以将其加载到数据库或转换为 parquet 文件。一般来说,很难知道哪种方法最有效,因此在分析的早期阶段,我们鼓励您尝试两种方法,然后选择最适合您的一种。

(衷心感谢丹妮尔-纳瓦罗(Danielle Navarro)为本章提供的最初版本)。

先决条件

在本章中,我们将继续使用 tidyverse,尤其是 dplyr,但我们将把它与专门为处理大型数据而设计的 arrow 软件包搭配使用:

library(tidyverse)
library(arrow)

在本章后面,我们还将看到 arrow 和 duckdb 之间的一些联系,因此我们还需要 dbplyr 和 duckdb:

library(dbplyr, warn.conflicts = FALSE)
library(duckdb)
#> Loading required package: DBI

获取数据

我们首先要获得一个与这些工具相匹配的数据集:西雅图开放数据(Seattle Open Data)网站上的西雅图公共图书馆物品借阅数据集。这个数据集包含 41,389,465 行,告诉你从 2005 年 4 月到 2022 年 10 月,每本书每月被借出的次数。

以下代码将为您提供数据的缓存副本。数据是一个 9 GB 的 CSV 文件,因此下载需要一些时间。我强烈建议使用curl::multidownload() 来获取超大文件,因为它正是为此目的而设计的:它会给你一个进度条,如果下载中断,它还能继续下载。

dir.create("data", showWarnings = FALSE)

curl::multi_download(
  "https://r4ds.s3.us-west-2.amazonaws.com/seattle-library-checkouts.csv",
  "data/seattle-library-checkouts.csv",
  resume = TRUE
)

打开数据集

我们先来看看数据。这个文件有 9 GB 之大,我们可能不想把整个文件都加载到内存中。一个好的经验法则是,你通常需要的内存至少是数据大小的两倍,而许多笔记本电脑的内存上限是 16 GB。这意味着我们要避免 read_csv()而使用 arrow::open_dataset() ...

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