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版

第 26 章 迭代 迭代

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

导言

在本章中,你将学习迭代工具,即在不同对象上重复执行相同的操作。R 语言中的迭代通常与其他编程语言不同,因为很多迭代都是隐式的,而且是免费的。例如,如果要在 R 中将一个数字向量x 加倍,只需写入2 * x 即可。而在大多数其他语言中,您需要使用某种 for 循环显式地将x 中的每个元素加倍。

本书已经为你提供了少量但功能强大的工具,可以对多个 "事物 "执行相同的操作:

现在是时候学习一些更通用的工具了,这些工具通常被称为函数式编程工具,因为它们是围绕将其他函数作为输入的函数而构建的。学习函数式编程很容易陷入抽象的境地,但在本章中,我们将把重点放在三个常见任务上,使其具体化:修改多个列、读取多个文件和保存多个对象。

先决条件

在本章中,我们将重点介绍由 dplyr 和 purrr 提供的工具,它们都是 tidyverse 的核心成员。你以前见过 dplyr,但purrr是新工具。在本章中,我们将只使用几个 purrr 函数,但在提高编程技能的过程中,它是一个非常值得探索的软件包:

library(tidyverse)

修改多列

想象一下,你有一个简单的 tibble,你想计算观测值的数量,并计算每一列的中位数:

df <- tibble(
  a = rnorm(10),
  b = rnorm(10),
  c = rnorm(10),
  d = rnorm(10)
)

你可以通过复制和粘贴来实现:

df |> summarize(
  n = n(),
  a = median(a),
  b = median(b),
  c = median(c),
  d = median(d),
)
#> # A tibble: 1 × 5
#>       n      a      b       c     d
#>   <int>  <dbl>  <dbl>   <dbl> <dbl>
#> 1    10 -0.246 -0.287 -0.0567 0.144

这违反了我们的经验法则,即复制和粘贴绝不超过两次,可以想象,如果你有几十甚至上百个列,这样做会很乏味。您可以使用 across():

df |> summarize(
  n = n(),
  across(a:d, median),
)
#> # A tibble: 1 × 5
#>       n      a      b       c     d
#>   <int>  <dbl>  <dbl>   <dbl> <dbl>
#> 1    10 -0.246 -0.287 -0.0567 0.144

across()有三个特别重要的参数,我们将在下面的章节中详细讨论。每次使用 across()第一个参数.cols 指定要遍历的列,第二个参数.fns 指定对每一列的操作。当需要对输出列的名称进行额外控制时,可以使用.names 参数,这在使用 across()mutate().我们还将讨论两个重要的变体、 if_any()if_all()filter().

使用 .cols 选择列

的第一个参数 across()的第一个参数,.cols ,选择要转换的列。它使用与 select(),"select() "的规格相同,因此您可以使用诸如 ...

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