
Program
In this part of the book, you’ll improve your programming skills. Programming is a cross-cutting skill needed for all data science work: you must use a computer to do data science; you cannot do it in your head, or with pencil and paper.
在本书的这一部分,你将提升你的编程技能。编程是所有数据科学工作所需的一项贯穿性技能:你必须使用计算机来做数据科学;你无法在头脑中,或用纸笔完成它。

Programming produces code, and code is a tool of communication. Obviously code tells the computer what you want it to do. But it also communicates meaning to other humans. Thinking about code as a vehicle for communication is important because every project you do is fundamentally collaborative. Even if you’re not working with other people, you’ll definitely be working with future-you! Writing clear code is important so that others (like future-you) can understand why you tackled an analysis in the way you did. That means getting better at programming also involves getting better at communicating. Over time, you want your code to become not just easier to write, but easier for others to read.
编程产生代码,而代码是一种沟通工具。显然,代码告诉计算机你希望它做什么。但它也向其他人类传达意义。将代码视为一种沟通媒介非常重要,因为你做的每个项目本质上都是协作性的。即使你没有和别人一起工作,你也肯定会和未来的你一起工作!编写清晰的代码很重要,这样其他人(比如未来的你)才能理解你为什么用那种方式处理分析。这意味着,提高编程能力也包括提高沟通能力。随着时间的推移,你希望你的代码不仅更容易编写,也更容易为他人阅读。
In the following three chapters, you’ll learn skills to improve your programming skills:
在接下来的三章中,你将学到提升编程技能的技巧:
Copy-and-paste is a powerful tool, but you should avoid doing it more than twice. Repeating yourself in code is dangerous because it can easily lead to errors and inconsistencies. Instead, in 25 Functions, you’ll learn how to write functions which let you extract out repeated tidyverse code so that it can be easily reused.
复制粘贴是一个强大的工具,但你应该避免重复使用它超过两次。在代码中重复自己是危险的,因为它很容易导致错误和不一致。因此,在 25 Functions 中,你将学习如何编写函数 (functions),这能让你提取出重复的 tidyverse 代码,以便于轻松重用。Functions extract out repeated code, but you often need to repeat the same actions on different inputs. You need tools for iteration that let you do similar things again and again. These tools include for loops and functional programming, which you’ll learn about in 26 Iteration.
函数提取了重复的代码,但你经常需要对不同的输入重复相同的操作。你需要迭代 (iteration) 工具,让你能一遍又一遍地做类似的事情。这些工具包括 for 循环和函数式编程,你将在 26 Iteration 中学习它们。As you read more code written by others, you’ll see more code that doesn’t use the tidyverse. In 27 A field guide to base R, you’ll learn some of the most important base R functions that you’ll see in the wild.
当你阅读更多他人编写的代码时,你会看到更多不使用 tidyverse 的代码。在 27 A field guide to base R 中,你将学习一些你在实际中会看到的最重要的基础 R 函数。
The goal of these chapters is to teach you the minimum about programming that you need for data science. Once you have mastered the material here, we strongly recommend that you continue to invest in your programming skills. We’ve written two books that you might find helpful. Hands on Programming with R, by Garrett Grolemund, is an introduction to R as a programming language and is a great place to start if R is your first programming language. Advanced R by Hadley Wickham dives into the details of R the programming language; it’s a great place to start if you have existing programming experience and a great next step once you’ve internalized the ideas in these chapters.
这些章节的目标是教会你数据科学所需的最低限度的编程知识。一旦你掌握了这里的内容,我们强烈建议你继续投资于你的编程技能。我们写了两本书,你可能会觉得有帮助。Garrett Grolemund 的 Hands on Programming with R 是 R 作为一种编程语言的入门介绍,如果 R 是你的第一门编程语言,这是一个很好的起点。Hadley Wickham 的 Advanced R 深入探讨了 R 编程语言的细节;如果你有现成的编程经验,这是一个很好的起点,也是你在内化了这些章节的思想后的一个很好的下一步。