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版

第 21 章 数据库 数据库

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

导言

数据库中保存着大量数据,因此您必须知道如何访问这些数据。有时,你可以请别人帮你把快照下载到.csv 文件中,但这样做很快就会很麻烦:每次你需要进行更改时,都必须与另一个人沟通。您希望能够在需要的时候直接进入数据库获取所需的数据。

在本章中,您将首先学习 DBI 软件包的基础知识:如何使用它连接数据库,然后使用 SQL1查询检索数据。SQL 是结构化查询语言的简称,是数据库的通用语言,也是所有数据科学家都要学习的重要语言。尽管如此,我们并不打算从 SQL 开始,而是要教你 dbplyr,它可以将你的 dplyr 代码翻译成 SQL。我们将以此为切入点,教你一些 SQL 最重要的功能。你不会在本章结束时成为 SQL 大师,但你将能够识别最重要的组件并了解它们的作用。

先决条件

在本章中,我们将介绍 DBI 和 dbplyr。DBI 是一个低级接口,用于连接数据库和执行 SQL;dbplyr 是一个高级接口,用于将 dplyr 代码转换为 SQL 查询,然后用 DBI 执行。

library(DBI)
library(dbplyr)
library(tidyverse)

数据库基础知识

在最简单的层面上,您可以将数据库视为数据框架的集合,在数据库术语中称为。与data.frame 一样,数据库表是命名列的集合,列中的每个值都是相同的类型。数据帧和数据库表之间有三个高层次的区别:

  • 数据库表存储在磁盘上,可以任意增大。数据帧存储在内存中,从根本上受到限制(尽管对于许多问题来说,这个限制仍然很大)。

  • 数据库表几乎都有索引。数据库索引就像一本书的索引,可以快速找到感兴趣的行,而不必查看每一行。数据帧和 tibbles 没有索引,但数据表有,这也是它们如此快速的原因之一。

  • 大多数传统数据库都是为快速收集数据而优化的,而不是分析现有数据。这些数据库被称为面向行的数据库,因为数据是逐行存储的,而不是像 R 那样逐列存储。最近,面向列的数据库有了长足的发展,可以更快地分析现有数据。

数据库由数据库管理系统简称DBMS)运行,它有三种基本形式:

  • 客户服务器数据库管理系统运行在一个功能强大的中央服务器上,通过计算机(客户端)连接服务器。它们非常适合与组织中的多人共享数据。流行的客户端-服务器数据库管理系统包括 PostgreSQL、MariaDB、SQL Server 和 Oracle。
  • CloudDBMS,如 Snowflake、亚马逊的 RedShift 和谷歌的 BigQuery,与客户端-服务器 DBMS 类似,但它们在云中运行。这意味着它们可以轻松处理超大数据集,并能根据需要自动提供更多计算资源。
  • 进程内数据库管理系统(如 SQLite 或 duckdb)完全在计算机上运行。它们非常适合处理您是主要用户的大型数据集。

连接数据库

要从 R 连接到数据库,需要使用一对软件包:

  • 您会一直使用 DBI数据库 接口),因为它提供了一组通用函数,可以连接到数据库、上传数据、运行 SQL 查询等。

  • 您还将使用为所连接的 DBMS 量身定制的软件包。该软件包将通用的 DBI 命令转化为特定 DBMS 所需的特定命令。通常每个 DBMS 都有一个软件包,例如 PostgreSQL 使用 RPostgres,MySQL 使用 RMariaDB。

如果找不到适用于 DBMS 的特定软件包,通常可以使用 ...

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