Hands-On Programming withR

Hands-On Programming with R

WRITE YOUR OWN FUNCTIONS AND SIMULATIONS

Garrett Grolemund

Foreword by Hadley Wickham

Hands-On Programming with R

Learn how to program by diving into the R language, and then use your newfound skills to solve practical data science problems. With this book, you'll learn how to load data, assemble and disassemble data objects, navigate R's environment system, write your own functions, and use all of R's programming tools.

RStudio Master Instructor Garrett Grolemund not only teaches you how to program, but also shows you how to get more from R than just visualizing and modeling data. You'll gain valuable programming skills and support your work as a data scientist at the same time.

Work hands-on with three practical data analysis projects based on casino games

Store, retrieve, and change data values in your computer's memory

Write programs and simulations that outperform those written by typical R users

Use R programming tools such as if else statements, for loops, and S3 classes

Learn how to write lightning-fast vectorized R code

Take advantage of R's package system and debugging tools Practice and apply R programming concepts as you learn them

"Hands-On Programming with R is friendly, conversational, and active. It's the next-best thing to learning R programming from me or Garrett in person. I hope you enjoy reading " it as much as I have. --Hadley Wickham Chief Scientist at RStudio

Garrett Grolemund is a statistician, teacher, and R developer who works as a data scientist and Master Instructor at RStudio. Garrett received his PhD at Rice University, where his research traced the origins of data analysis as a cognitive process and identified how attentional and epistemological concerns guide every data analysis.

DATA ANALYSIS/STATISTICAL SOFT WARE

US $39.99

CAN $41.99

ISBN: 978-1-449-35901-0

Twitter: @oreillymedia oreilly

Hands-On Programming with R

Garrett Grolemund

Hands-On Programming with R

by Garrett Grolemund

Copyright ? 2014 Garrett Grolemund. All rights reserved.

Printed in the United States of America.

Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

O'Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@.

Editors: Julie Steele and Courtney Nash Production Editor: Matthew Hacker Copyeditor: Eliahu Sussman Proofreader: Amanda Kersey

Indexer: Judith McConville Cover Designer: Randy Comer Interior Designer: David Futato Illustrator: Rebecca Demarest

July 2014:

First Edition

Revision History for the First Edition: 2014-07-08: First release

See for release details.

Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc. Hands-On Programming with R, the picture of an orange-winged Amazon parrot, and related trade dress are trademarks of O'Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.

ISBN: 978-1-449-35901-0 [LSI]

Table of Contents

Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix

Part I. Project 1: Weighted Dice

1. The Very Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

The R User Interface

3

Objects

7

Functions

12

Sample with Replacement

14

Writing Your Own Functions

16

The Function Constructor

17

Arguments

18

Scripts

20

Summary

22

2. Packages and Help Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Packages

23

install.packages

24

library

24

Getting Help with Help Pages

29

Parts of a Help Page

30

Getting More Help

33

Summary

33

Project 1 Wrap-up

34

iii

Part II. Project 2: Playing Cards

3. R Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

Atomic Vectors

38

Doubles

39

Integers

40

Characters

41

Logicals

42

Complex and Raw

42

Attributes

43

Names

44

Dim

45

Matrices

46

Arrays

46

Class

47

Dates and Times

48

Factors

49

Coercion

51

Lists

53

Data Frames

55

Loading Data

57

Saving Data

61

Summary

61

4. R Notation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

Selecting Values

65

Positive Integers

66

Negative Integers

68

Zero

69

Blank Spaces

69

Logical Values

69

Names

70

Deal a Card

70

Shuffle the Deck

71

Dollar Signs and Double Brackets

73

Summary

76

5. Modifying Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

Changing Values in Place

77

Logical Subsetting

80

Logical Tests

80

Boolean Operators

85

iv | Table of Contents

Missing Information

89

na.rm

90

is.na

90

Summary

91

6. Environments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

Environments

93

Working with Environments

95

The Active Environment

97

Scoping Rules

98

Assignment

99

Evaluation

99

Closures

107

Summary

112

Project 2 Wrap-up

112

Part III. Project 3: Slot Machine

7. Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

Strategy

118

Sequential Steps

118

Parallel Cases

119

if Statements

120

else Statements

123

Lookup Tables

130

Code Comments

136

Summary

137

8. S3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

The S3 System

139

Attributes

140

Generic Functions

145

Methods

146

Method Dispatch

148

Classes

151

S3 and Debugging

152

S4 and R5

152

Summary

152

9. Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

Expected Values

155

Table of Contents | v

expand.grid

157

for Loops

163

while Loops

168

repeat Loops

169

Summary

169

10. Speed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

Vectorized Code

171

How to Write Vectorized Code

173

How to Write Fast for Loops in R

178

Vectorized Code in Practice

179

Loops Versus Vectorized Code

183

Summary

183

Project 3 Wrap-up

184

A. Installing R and RStudio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

B. R Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

C. Updating R and Its Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

D. Loading and Saving Data in R. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

E. Debugging R Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

vi | Table of Contents

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download