R Projects For Dummies - Joseph Schmuller - E-Book

R Projects For Dummies E-Book

Joseph Schmuller

0,0
20,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Make the most of R's extensive toolset R Projects For Dummies offers a unique learn-by-doing approach. You will increase the depth and breadth of your R skillset by completing a wide variety of projects. By using R's graphics, interactive, and machine learning tools, you'll learn to apply R's extensive capabilities in an array of scenarios. The depth of the project experience is unmatched by any other content online or in print. And you just might increase your statistics knowledge along the way, too! R is a free tool, and it's the basis of a huge amount of work in data science. It's taking the place of costly statistical software that sometimes takes a long time to learn. One reason is that you can use just a few R commands to create sophisticated analyses. Another is that easy-to-learn R graphics enable you make the results of those analyses available to a wide audience. This book will help you sharpen your skills by applying them in the context of projects with R, including dashboards, image processing, data reduction, mapping, and more. * Appropriate for R users at all levels * Helps R programmers plan and complete their own projects * Focuses on R functions and packages * Shows how to carry out complex analyses by just entering a few commands If you're brand new to R or just want to brush up on your skills, R Projects For Dummies will help you complete your projects with ease.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 341

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



R Projects For Dummies®

Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com

Copyright © 2018 by John Wiley & Sons, Inc., Hoboken, New Jersey

Published simultaneously in Canada

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.

For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies.

Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.

Library of Congress Control Number: 2017964027

ISBN: 978-1-119-44618-7; 978-1-119-44617-0 (ebk); 978-1-119-44616-3 (ebk)

R Projects For Dummies®

To view this book's Cheat Sheet, simply go to www.dummies.com and search for “R Projects For Dummies Cheat Sheet” in the Search box.

Table of Contents

Cover

Introduction

About This Book

What You Can Safely Skip

Foolish Assumptions

Icons Used in This Book

Beyond the Book

Where to Go from Here

Part 1: The Tools of the Trade

Chapter 1: R: What It Does and How It Does It

Getting R

Getting RStudio

A Session with R

R Functions

User-Defined Functions

Comments

R Structures

Of for Loops and if Statements

Chapter 2: Working with Packages

Installing Packages

Examining Data

R Formulas

More Packages

Exploring the tidyverse

Chapter 3: Getting Graphic

Touching Base

Graduating to ggplot2

Part 2: Interacting with a User

Chapter 4: Working with a Browser

Getting Your Shine On

Creating Your First shiny Project

Working with ggplot

Another shiny Project

Suggested Project

Chapter 5: Dashboards — How Dashing!

The shinydashboard Package

Exploring Dashboard Layouts

Working with the Sidebar

Interacting with Graphics

Part 3: Machine Learning

Chapter 6: Tools and Data for Machine Learning Projects

The UCI (University of California-Irvine) ML Repository

Introducing the

Rattle

package

Using

Rattle

with

iris

Chapter 7: Decisions, Decisions, Decisions

Decision Tree Components

Decision Trees in R

Decision Trees in

Rattle

Project: A More Complex Decision Tree

Suggested Project: Titanic

Chapter 8: Into the Forest, Randomly

Growing a Random Forest

Random Forests in R

Project: Identifying Glass

Suggested Project: Identifying Mushrooms

Chapter 9: Support Your Local Vector

Some Data to Work With

Separability: It's Usually Nonlinear

Support Vector Machines in R

Project: House Parties

Suggested Project: Titanic Again

Chapter 10: K-Means Clustering

How It Works

K-Means Clustering in R

Project: Glass Clusters

Suggested Project: A Few Quick Ones

Chapter 11: Neural Networks

Networks in the Nervous System

Artificial Neural Networks

Neural Networks in R

Project: Banknotes

Suggested Projects: Rattling Around

Part 4: Large(ish) Data Sets

Chapter 12: Exploring Marketing

Project: Analyzing Retail Data

Enter Machine Learning

Suggested Project: Another Data Set

Chapter 13: From the City That Never Sleeps

Examining the Data Set

Warming Up

Project: Departure Delays

Suggested Project: Delay and Weather

Part 5: Maps and Images

Chapter 14: All Over the Map

Project: The Airports of Wisconsin

Suggested Project 1: Map Your State

Suggested Project 2: Map the Country

Chapter 15: Fun with Pictures

Polishing a Picture: It's magick!

Project: Two Legends in Search of a Legend

Suggested Project: Combine an Animation with a Plot

Part 6: The Part of Tens

Chapter 16: More Than Ten Packages for Your R Projects

Machine Learning

Databases

Maps

Image Processing

Text Analysis

Chapter 17: More than Ten Useful Resources

Interacting with Users

Machine Learning

Databases

Maps and Images

About the Author

Connect with Dummies

End User License Agreement

Guide

Cover

Table of Contents

Begin Reading

Pages

i

ii

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

31

32

33

34

35

36

37

38

39

40

41

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

305

306

307

308

309

310

311

312

313

314

315

316

317

319

320

321

322

323

324

325

327

328

329

345

346

347

348

Introduction

If you’re like me, you think the best way to learn is by doing. Don’t just read about something — practice it! If you want to be a builder, then build. If you want to be a writer, then write. If you want to be a carpenter, then carpenter. (Yes, that noun and verb are the same. Carpent is not a word.)

I based this book on that learning-by-doing philosophy. My objective is for you to expand your R skill set by using R to complete projects in a variety of areas, and to learn something about those areas, too.

Even with those noble intentions, a book like this one can fall into a trap. It can quickly become a cookbook: Use this package, use these functions, create a graphic — and presto, you’ve finished a project and it’s time to move on.

I didn’t want to write that book. Instead, beginning in Part 2 (which is where the projects start), each chapter does more than just walk you through a project. First, I show you some background material about the subject area, and then (in most chapters) you work through a scaled-down project in that area to get your feet wet, and then you complete a larger project.

But a chapter doesn’t end there. At the end of each chapter, you’ll find a Suggested Project that challenges you to apply your newly minted skills. For each of those, I supply just enough information to get you started. (Wherever necessary, I include tips about potential pitfalls.)

Along the way, you’ll also encounter Quick Suggested Projects. These are based on tweaks to projects you’ve already completed, and they present additional challenges to your growing skill set.

One more thing: Every subject area could be the basis for an entire book, so I can only scratch the surface of each one. Chapter 17 directs you toward resources that provide more information.

About This Book

I’ve organized this book into six parts.

Part 1: The Tools of the Trade

Part 1 is all about R and RStudio. I discuss R functions, structures, and packages, and I show you how to create a variety of graphics.

Part 2: Interacting with a User

The projects begin in Part 2, where you learn to create applications that respond to users. I discuss the shiny package for working with web browsers, and the shinydashboard package for creating dashboards.

Part 3: Machine Learning

This is the longest part of the book. I begin by telling you about the University of California–Irvine Machine Learning Repository, which provides the data sets for the projects. I also discuss the rattle package for creating machine learning applications. The projects cover decision trees, random forests, support vector machines, k-means clustering, and neural networks.

Part 4: Large(ish) Data Sets

The two projects in Part 4 deal with larger data sets than you encounter earlier in the book. The first project is a customer segmentation analysis of over 300,000 customers of an online retailer. A follow-up analysis applies machine learning. The second project analyzes a data set of more than 500,000 airline flights.

Part 5: Maps and Images

Two projects are in Part 5. The first is to plot the location (along with other information) of airports in one of the US states. The second shows you how to combine an animated image with a stationary one.

Part 6: The Part of Tens

The first chapter in Part 6 provides information about useful packages that can help you with future projects. The second tells you where to learn more about the subject areas of this book.

What You Can Safely Skip

Any reference book throws a lot of information at you, and this one is no exception. I intended it all to be useful, but I didn't aim it all at the same level. So if you're not deeply into the subject matter, you can avoid paragraphs marked with the Technical Stuff icon, and you can also skip the sidebars.

Foolish Assumptions

I'm assuming that you

Know how to work with Windows or the Mac. I don't spell out the details of pointing, clicking, selecting, and other actions.

Can install R and RStudio (I show you how in

Chapter 1

), and follow along with the examples. I use the Windows version of RStudio, but you should have no problem if you’re working on a Mac.

Icons Used in This Book

You’ll find icons in all For Dummies books, and this one is no exception. Each one is a little picture in the margin that lets you know something special about the paragraph it sits next to.

This icon points out a hint or a shortcut that helps you in your work and makes you an all-around better person.

This one points out timeless wisdom to take with you as you continue on the path to enlightenment.

Pay attention to this icon. It's a reminder to avoid something that might gum up the works for you.

As I mention in the earlier section “What You Can Safely Skip,” this icon indicates material you can blow past if it’s just too technical. (I’ve kept this information to a minimum.)

Beyond the Book

In addition to what you’re reading right now, this product comes with a free access-anywhere Cheat Sheet that presents a selected list of R functions and describes what they do. To get this Cheat Sheet, visit www.dummies.com and type R Projects For Dummies Cheat Sheet in the Search box.

Where to Go from Here

You can start the book anywhere, but here are a couple of hints. Want to introduce yourself to R and packages? You’ll find the info in Chapters 1 and 2. Want to start with graphics? Hit Chapter 3. For anything else, find it in the table of contents or in the index and go for it.

If you’re a cover-to-cover reader, turn the page… .

Part 1

The Tools of the Trade

IN THIS PART …

Learn about R and RStudio

Understand R Functions and Structures

Create your own R Functions

Examine data

Use base R graphics

Graduate to ggplot2 graphics

Chapter 1

R: What It Does and How It Does It

IN THIS CHAPTER

Getting R and RStudio on your computer

Plunging into a session with R

Working with R functions

Working with R structures

So you’re ready to journey into the wonderful world of R! Designed by and for statisticians and data scientists, R has a short but illustrious history.

In the 1990s, Ross Ihaka and Robert Gentleman developed R at the University of Auckland, New Zealand. The Foundation for Statistical Computing supports R, which is growing more popular by the day.

Getting R

If you don’t already have R on your computer, the first thing to do is to download R and install it.

You’ll find the appropriate software on the website of the Comprehensive R Archive Network (CRAN). In your browser, type this web address if you work in Windows:

cran.r-project.org/bin/windows/base

Type this one if you work on the Mac:

cran.r-project.org/bin/macosx

Click the link to download R. This puts a win.exe file in your Windows computer or a pkg file in your Mac. In either case, follow the usual installation procedures. When installation is complete, Windows users see two R icons on their desktop, one for 32-bit processors and one for 64-bit processors (pick the one that's right for you). Mac users see an R icon in their Application folder.

Both addresses provide helpful links to FAQs. The windows-related one also has the link Installation and Other Instructions.

Getting RStudio

Working with R is a lot easier if you do it through an application called RStudio. Computer honchos refer to RStudio as an IDE (Integrated Development Environment). Think of it as a tool that helps you write, edit, run, and keep track of your R code, and as an environment that connects you to a world of helpful hints about R.

Here’s the web address for this terrific tool:

www.rstudio.com/products/rstudio/download

Click the link for the installer for your computer’s operating system — Windows, Mac, or a flavor of Linux — and again follow the usual installation procedures.

In this book, I work with R version 3.4.0 and RStudio version 1.0.143. By the time you read this, later versions of both might be available.

After you finish installing R and RStudio, click on your brand-new RStudio icon to open the window shown in Figure 1-1.

FIGURE 1-1: RStudio, immediately after you install it and click on its icon.

The large Console pane on the left runs R code. One way to run R code is to type it directly into the Console pane. I show you another in a moment.

The other two panes provide helpful information as you work with R. The Environment/History pane is in the upper right. The Environment tab keeps track of the things you create (which R calls objects) as you work with R. The History tab tracks R code that you enter.

Get used to the word object. Everything in R is an object.The Files/Plots/Packages/Help pane is in the lower right. The Files tab shows files you create. The Plots tab holds graphs you create from your data. The Packages tab shows add-ons (called packages) that have downloaded with R. Bear in mind that downloaded doesn’t mean “ready to use.” To use a package’s capabilities, one more step is necessary, and trust me — you’ll want to use packages.

Figure 1-2 shows the Packages tab. I discuss packages later in this chapter.

FIGURE 1-2: The RStudio Packages tab.

The Help tab, shown in Figure 1-3, links you to a wealth of information about R and RStudio.

FIGURE 1-3: The RStudio Help tab.

To tap into the full power of RStudio as an IDE, click the icon in the upper right corner of the Console pane. That changes the appearance of RStudio so that it looks like Figure 1-4.

FIGURE 1-4: RStudio after you click the icon in the upper right corner of the Console pane.

The Console pane relocates to the lower left. The new pane in the upper left is the Scripts pane. You type and edit code in the Scripts pane by pressing Ctrl+R (Command+Enter on the Mac), and then the code executes in the Console pane.

Ctrl+Enter works just like Ctrl+R. You can also highlight lines of code in the Scripts pane and select Code ⇒   Run Selected Line(s) from RStudio’s main menu.

A Session with R

Before you start working, select File ⇒   Save As from the main menu and then save your work file as My First R Session. This relabels the tab in the Scripts pane with the name of the file and adds the .R extension. This also causes the filename (along with the .R extension) to appear on the Files tab.

The working directory

What exactly does R save, and where does R save it? What R saves is called the workspace, which is the environment you're working in. R saves the workspace in the working directory. In Windows, the default working directory is

C:\Users\<User Name>\Documents

On a Mac, it’s

/Users/<User Name>

If you ever forget the path to your working directory, type

> getwd()

in the Console pane, and R returns the path onscreen.

In the Console pane, you don’t type the right-pointing arrowhead at the beginning of the line. That’s a prompt.

My working directory looks like this:

> getwd()

[1] "C:/Users/Joseph Schmuller/Documents

Note the direction the slashes are slanted. They’re opposite to what you typically see in Windows file paths. This is because R uses \ as an escape character — whatever follows the \ means something different from what it usually means. For example, \t in R means Tab key.

You can also write a Windows file path in R as

C:\\Users\\<User Name>\\Documents

If you like, you can change the working directory:

> setwd(<file path>)

Another way to change the working directory is to select Session ⇒   Set Working Directory ⇒   Choose Directory from the main menu.

Getting started

Let's get down to business and start writing R code. In the Scripts pane, type

x <- c(5,10,15,20,25,30,35,40)

and then press Ctrl+R.

That puts this line into the Console pane:

> x <- c(5,10,15,20,25,30,35,40)

As I say in an earlier Tip paragraph, the right-pointing arrowhead (the greater-than sign) is a prompt that R puts in the Console pane. You don’t see it in the Scripts pane.

Here’s what R just did: The arrow-sign says that x gets assigned whatever is to the right of the arrow-sign. Think of the arrow-sign as R’s assignment operator. So the set of numbers 5, 10, 15, 20 … 40 is now assigned to x.

In R-speak, a set of numbers like this is a vector. I tell you more about this concept in the later section “R Structures.”

You can read that line of code as “x gets the vector 5, 10, 15, 20.”

Type x into the Scripts pane and press Ctrl+R, and here’s what you see in the Console pane:

> x

[1] 5 10 15 20 25 30 35 40

The 1 in square brackets is the label for the first line of output. So this signifies that 5 is the first value.

Here you have only one line, of course. What happens when R outputs many values over many lines? Each line gets a bracketed numeric label, and the number corresponds to the first value in the line. For example, if the output consists of 23 values and the eighteenth value is the first one on the second line, the second line begins with [18].

Creating the vector x causes the Environment tab to look like Figure 1-5.

FIGURE 1-5: The RStudio Environment tab after creating the vector x.

Another way to see the objects in the environment is to type ls() into the Scripts pane and then press Ctrl+R. Or you can type > ls() directly into the Console pane and press Enter. Either way, the result in the Console pane is

[1] "x"

Now you can work with x. First, add all numbers in the vector. Typing sum(x) in the Scripts pane (be sure to follow with Ctrl+R) executes the following line in the Console pane:

> sum(x)

[1] 180

How about the average of the numbers in vector x?

That would involve typing mean(x) in the Scripts pane, which (when followed by Ctrl+R) executes

> mean(x)

[1] 22.5

in the Console pane.

As you type in the Scripts pane or in the Console pane, you see that helpful information pops up. As you become experienced with RStudio, you learn how to use that information.

Variance is a measure of how much a set of numbers differ from their mean. Here's how to use R to calculate variance:

> var(x)

[1] 150

What, exactly, is variance and what does it mean? (Shameless plug alert.) For the answers to these and numerous other questions about statistics and analysis, read one of the most classic works in the English language: Statistical Analysis with R For Dummies (written by yours truly and published by Wiley).

After R executes all these commands, the History tab looks like Figure 1-6.

FIGURE 1-6: The History tab, after creating and working with a vector.

To end a session, select File ⇒   Quit Session from the main menu or press Ctrl+Q. As Figure 1-7 shows, a dialog box opens and asks what you want to save from the session. Saving the selections enables you to reopen the session where you left off the next time you open RStudio (although the Console pane doesn’t save your work).

FIGURE 1-7: The Quit R Session dialog box.

Moving forward, most of the time I don’t say “Type this code into the Scripts pane and press Ctrl+Enter” whenever I take you through an example. I just show you the code and its output, as in the var() example.

Also, sometimes I show code with the > prompt, and sometimes without. Generally, I show the prompt when I want you to see R code and its results. I don’t show the prompt when I just want you to see R code that I create in the Scripts pane.

R Functions

The examples in the preceding section use c(), sum(), and var(). These are three functions built into R. Each one consists of a function name immediately followed by parentheses. Inside the parentheses are arguments. In the context of a function, argument doesn't mean “debate” or “disagreement” or anything like that. It’s the math name for whatever a function operates on.

Sometimes a function takes no arguments (as is the case with ls()). You still include the parentheses.

The functions in the examples I showed you are pretty simple: Supply an argument, and each one gives you a result. Some R functions, however, take more than one argument.

R has a couple of ways for you to deal with multi-argument functions. One way is to list the arguments in the order that they appear in the function’s definition. R calls this positional mapping.

Here’s an example. Remember when I created the vector x?

x <- c(5,10,15,20,25,30,35,40)

Another way to create a vector of those numbers is with the function seq():

> y <- seq(5,40,5)

> y

[1] 5 10 15 20 25 30 35 40

Think of seq() as creating a “sequence.” The first argument to seq() is the number to start the sequence from (5). The second argument is the number that ends the sequence — the number the sequence goes to (40). The third argument is the increment of the sequence — the amount the sequence increases by (5).

If you name the arguments, it doesn't matter how you order them:

> z <- seq(to=40,by=5,from=5)

> z

[1] 5 10 15 20 25 30 35 40

So when you use a function, you can place its arguments out of order, if you name them. R calls this keyword matching. This comes in handy when you use an R function that has many arguments. If you can’t remember their order, use their names, and the function works.

For help on a particular function — seq(), for example — type ?seq. When you run that code, helpful information appears on the Help tab and useful information pops up in a little window right next to where you’re typing.

User-Defined Functions

R enables you to create your own functions, and here are the fundamentals on how to do it.

The form of an R function is

myfunction <- function(argument1, argument2, …){

statements

return(object)

}

Here’s a function for dealing with right triangles. Remember them? A right triangle has two sides that form a right angle, and a third side called a hypotenuse. You might also remember that a guy named Pythagoras showed that if one side has length a and the other side has length b, the length of the hypotenuse, c, is

So here’s a simple function called hypotenuse() that takes two numbers a and b, (the lengths of the two sides of a right triangle) and returns c, the length of the hypotenuse:

hypotenuse <- function(a,b){

hyp <- sqrt(a^2+b^2)

return(hyp)

}

Type that code snippet into the Scripts pane and highlight it. Then press Ctrl+Enter. Here's what appears in the Console pane:

> hypotenuse <- function(a,b){

+ hyp <- sqrt(a^2+b^2)

+ return(hyp)

+ }

Each plus sign is a continuation prompt. It just indicates that a line continues from the preceding line.

And here’s how to use the function:

> hypotenuse(3,4)

[1] 5

Writing R functions can encompass way more than I’ve shown you here. To learn more, take a look at R For Dummies, by Andrie de Vries and Joris Meys (Wiley).

Comments

A comment is a way of annotating code. Begin a comment with the # symbol, which, as everyone knows, is called an octothorpe. (Wait. What? “Hashtag?” Get atta here!) This symbol tells R to ignore everything to the right of it.

Comments help someone who has to read the code you’ve written. For example:

hypotenuse <- function(a,b){ # list the arguments

hyp <- sqrt(a^2+b^2) # perform the computation

return(hyp) # return the value

}

Here’s a heads-up: I don’t typically add comments to lines of code in this book. Instead, I provide detailed descriptions. In a book like this, I feel it’s the best way to get the message across.

Of for Loops and if Statements

Like many programming languages, R provides a way to iterate through its structures to get things done. R’s way is called the for loop. And, like many languages, R gives you a way to test against a criterion: the if statement.

The general format of a for loop is

for counter in start:end{

statement 1

statement n

}

As you might imagine, counter tracks the iterations.

The simplest general format of an if statement is

if(test){statement to execute if test is TRUE}