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版

第 25 章 函数 职能

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

导言

编写函数是提高数据科学家能力的最佳方法之一。与复制和粘贴相比,函数允许您以更强大、更通用的方式自动执行常见任务。与复制和粘贴相比,编写函数有三大优势:

  • 您可以为函数取一个令人回味的名字,使代码更容易理解。

  • 随着需求的变化,您只需要在一个地方更新代码,而不是在许多地方。

  • 这样就不会在复制和粘贴时偶然出错(例如,在一处更新了变量名,但在另一处却没有更新)。

  • 它使您更容易在各个项目之间重复使用工作,从而长期提高您的工作效率。

一个好的经验法则是,当你复制和粘贴一个代码块超过两次时(即你现在有三份相同的代码),就考虑编写一个函数。本章将介绍三种有用的函数:

  • 向量函数将一个或多个向量作为输入,并将一个向量作为输出返回。
  • 数据帧函数将数据帧作为输入,并将数据帧作为输出返回。
  • 绘图函数将数据帧作为输入,并将绘图作为输出返回。

每个部分都包含许多示例,可以帮助你归纳出你所看到的模式。如果没有 Twitter 朋友们的帮助,这些示例是不可能实现的,我们鼓励你跟随评论中的链接查看原始灵感。您可能还想阅读一般函数绘图函数的原始激励推文,以了解更多函数。

先决条件

我们将总结 tidyverse 中的各种函数。我们还将使用 NYCFlights13 作为熟悉的数据源,以使用我们的函数:

library(tidyverse)
library(nycflights13)

矢量函数

我们将从向量函数开始:这些函数接收一个或多个向量,并返回向量结果。例如,请看这段代码。它是做什么的?

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

df |> mutate(
  a = (a - min(a, na.rm = TRUE)) / 
    (max(a, na.rm = TRUE) - min(a, na.rm = TRUE)),
  b = (b - min(b, na.rm = TRUE)) / 
    (max(b, na.rm = TRUE) - min(a, na.rm = TRUE)),
  c = (c - min(c, na.rm = TRUE)) / 
    (max(c, na.rm = TRUE) - min(c, na.rm = TRUE)),
  d = (d - min(d, na.rm = TRUE)) / 
    (max(d, na.rm = TRUE) - min(d, na.rm = TRUE)),
)
#> # A tibble: 5 × 4
#>       a     b     c     d
#>   <dbl> <dbl> <dbl> <dbl>
#> 1 0.339  2.59 0.291 0    
#> 2 0.880  0    0.611 0.557
#> 3 0      1.37 1     0.752
#> 4 0.795  1.37 0     1    
#> 5 1      1.34 0.580 0.394

你也许能猜出这是将每一列的范围从 0 改为 1,但你发现错误了吗?当 Hadley 写这段代码时,他在复制和粘贴时犯了一个错误,忘记将a 改为b 。防止这种错误是学习如何编写函数的一个很好的理由。

编写函数

要编写函数,首先需要分析重复的代码,找出哪些部分是不变的,哪些部分是变化的。如果我们把前面的代码拉到 mutate(),就更容易看出其中的规律了,因为现在每段重复代码只有一行:

(
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