Hands-O Programmin wit R Garrett

Forewordvii

Prefaceix

Part I.

Project 1: Weighted Dice

1The Very Basics3

The R User Interface

Objects

Functions

Sample with Replacement

Writing Your Own Functions

The Function Constructor

Arguments

Scripts

Summary

2. Packages and Help Pages

Packages

install.packages

library

Getting Help with Help Pages

Parts of a Help Page

Getting More Help

Summary

Project 1 Wrap-up

iiiPart II.

Project 2: Playing Cards

3R Objects37

Atomic Vectors

Doubles

Integers

Characters

Logicals

Complex and Raw

Attributes

Names

Dim

Matrices

Arrays

Class

Dates and Times

Factors

Coercion

Lists

Data Frames

Loading Data

Saving Data

Summary

4R Notation65

Selecting Values

Positive Integers

Negative Integers

Zero

Blank Spaces

Logical Values

Names

Deal a Card

Shuffle the Deck

Dollar Signs and Double Brackets

Summary

5 Modifying Values

Changing Values in Place

Logical Subsetting

Logical Tests

Boolean Operators

85Missing Information

na.rm

is.na

Summary

6 Environments93

Environments

Working with Environments

The Active Environment

Scoping Rules

Assignment

Evaluation

Closures

Summary

Project 2 Wrap-up

Part III.

Project 3: Slot Machine

7 Programs

Strategy

Sequential Steps

Parallel Cases

if Statements

else Statements

Lookup Tables

Code Comments

Summary

The S3 System

Attributes

Generic Functions

Methods

Method Dispatch

Classes

S3 and Debugging

S4 and R5

Summary

9 Loops

Expected Values

vexpand.grid

for Loops

while Loops

repeat Loops

Summary

10 Speed

Vectorized Code

How to Write Vectorized Code

How to Write Fast for Loops in R

Vectorized Code in Practice

Loops Versus Vectorized Code

Summary

Project 3 Wrap-up

AInstalling R and RStudio187

BR Packages191

CUpdating R and Its Packages195

DLoading and Saving Data in R197

EDebugging R Code211

Index221