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版

第 2 章 工作流程 工作流程:基础知识

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

现在你已经有了一些运行 R 代码的经验。我们没有给你提供太多细节,但你显然已经掌握了基础知识,否则你早就沮丧地扔掉这本书了!当你开始使用 R 编程时,沮丧是很自然的,因为它对标点符号的要求非常严格,哪怕一个字符错位都会引起它的抱怨。不过,虽然你应该预料到会有一点挫败感,但这种经历是典型的、暂时的:它发生在每个人身上,克服它的唯一方法就是不断尝试。

在进一步了解之前,让我们先确保你已经在运行 R 代码方面打下了坚实的基础,并了解一些最有用的 RStudio 功能。

编码基础

让我们回顾一下迄今为止我们省略的一些基础知识,以便让您尽快绘制地图。您可以使用 R 进行基本的数学计算:

1 / 200 * 30
#> [1] 0.15
(59 + 73 + 2) / 3
#> [1] 44.66667
sin(pi / 2)
#> [1] 1

您可以使用赋值运算符<- 创建新对象:

x <- 3 * 4

请注意,x 的值不会打印出来,只是存储起来。如果要查看值,请在控制台中键入x

您可以使用 c():

primes <- c(2, 3, 5, 7, 11, 13)

矢量的基本运算适用于矢量的每个元素:

primes * 2
#> [1]  4  6 10 14 22 26
primes - 1
#> [1]  1  2  4  6 10 12

所有创建对象的 R 语句、赋值语句都有相同的形式:

object_name <- value

阅读这段代码时,请在脑海中默念 "对象名称获取值"。

您会做很多作业,而<- 输入起来很麻烦。您可以使用 RStudio 的快捷键来节省时间:Alt+-(减号)。请注意,RStudio 会自动在<- 周围加上空格,这是很好的代码格式化做法。在天气好的时候,代码读起来会很费劲,所以请使用空格,让你的眼睛休息一下。

评论

R 将忽略该行# 之后的任何文本。这样,您就可以编写注释,即 R 忽略但人类可以阅读的文本。我们有时会在示例中加入注释,以解释代码中发生了什么。

注释有助于简要说明代码的作用:

# create vector of primes
primes <- c(2, 3, 5, 7, 11, 13)

# multiply primes by 2
primes * 2
#> [1]  4  6 10 14 22 26

对于像这样的短代码,也许没有必要为每一行代码都留下注释。但是当你编写的代码变得越来越复杂时,注释就能为你(和你的合作者)节省大量的时间来弄清代码中做了什么。

使用注释来解释代码的原因,而不是如何做什么。代码中的"是什么 ""如何做"总是可以通过仔细阅读弄清楚的,即使这可能会很乏味。如果您在注释中描述了每一个步骤,然后又修改了代码,那么您就必须记得更新注释,否则将来再看您的代码时就会很混乱。

要弄清为什么要这样做,即使不是不可能,也要困难得多。例如,geom_smooth() 有一个名为span 的参数,用于控制曲线的平滑度,数值越大曲线越平滑。假设您决定将span 的值从默认的 0.75 改为 0.9:未来的读者很容易理解发生了什么,但除非您在注释中注明您的想法,否则没有人会理解您为什么要更改默认值。

对于数据分析代码,使用注释来解释您的整体攻击计划,并记录您遇到的重要见解。从代码本身是无法重新获取这些知识的。

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