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版

第 23 章 分层数据 分层数据

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

导言

在本章中,你将学习数据矩形化的艺术,即从根本上将分层数据或树状数据转换为由行和列组成的矩形数据帧。这一点非常重要,因为分层数据非常常见,尤其是在处理来自网络的数据时。

要学习矩形化,首先要了解列表,这是一种使分层数据成为可能的数据结构。然后,你将学习两个重要的 tidyr 函数: tidyr::unnest_longer()tidyr::unnest_wider().然后,我们将向你展示一些案例研究,反复应用这些简单的函数来解决实际问题。最后,我们将讨论 JSON,它是分层数据集最常见的来源,也是网络数据交换的常用格式。

先决条件

在本章中,我们将使用 tidyr 的许多函数,它是 tidyverse 的核心成员。我们还将使用repurrrsive提供一些有趣的数据集进行矩形化练习,最后使用jsonlite将 JSON 文件读入 R 列表。

library(tidyverse)
library(repurrrsive)
library(jsonlite)

列表

到目前为止,您已经使用过包含简单向量(如整数、数字、字符、日期时间和因数)的数据帧。这些向量之所以简单,是因为它们是同质的:每个元素都是相同的数据类型。如果要在同一向量中存储不同类型的元素,则需要一个列表,使用 list():

x1 <- list(1:4, "a", TRUE)
x1
#> [[1]]
#> [1] 1 2 3 4
#> 
#> [[2]]
#> [1] "a"
#> 
#> [[3]]
#> [1] TRUE

命名列表的组件或子元素通常很方便,命名方法与命名 tibble 列的方法相同:

x2 <- list(a = 1:2, b = 1:3, c = 1:4)
x2
#> $a
#> [1] 1 2
#> 
#> $b
#> [1] 1 2 3
#> 
#> $c
#> [1] 1 2 3 4

即使是这些简单的列表,打印也会占用大量空间。一个有用的替代方法是 str(),它可以紧凑地显示结构,而不强调内容:

str(x1)
#> List of 3
#>  $ : int [1:4] 1 2 3 4
#>  $ : chr "a"
#>  $ : logi TRUE
str(x2)
#> List of 3
#>  $ a: int [1:2] 1 2
#>  $ b: int [1:3] 1 2 3
#>  $ c: int [1:4] 1 2 3 4

正如您所看到的、 str()将列表中的每个子项显示在自己的一行上。如果存在,它会显示名称;然后是类型缩写;然后是前几个值。

层次结构

列表可以包含任何类型的对象,包括其他列表。因此,列表适合表示分层(树状)结构:

x3 <- list(list(1, 2), list(3, 4))
str(x3)
#> List of 2
#>  $ :List of 2
#>   ..$ : num 1
#>   ..$ : num 2
#>  $ :List of 2
#>   ..$ : num 3
#>   ..$ : num 4

这与 c()产生一个平面向量:

c(c(1, 2), c(3, 4))
#> [1] 1 2 3 4

x4 <- c(list(1, 2), list(3, 4))
str(x4)
#> List of 4
#> $ : num ...
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