Introduction |
|
1 | (8) |
|
|
1 | (1) |
|
Changes in the Second Edition |
|
|
2 | (1) |
|
Conventions Used in This Book |
|
|
3 | (1) |
|
|
4 | (1) |
|
|
4 | (1) |
|
How This Book Is Organized |
|
|
5 | (1) |
|
Part I: Getting Started with R Programming |
|
|
5 | (1) |
|
Part II: Getting Down to Work in R |
|
|
5 | (1) |
|
|
5 | (1) |
|
Part IV: Making the Data Talk |
|
|
5 | (1) |
|
Part V: Working with Graphics |
|
|
6 | (1) |
|
Part VI: The Part of Tens |
|
|
6 | (1) |
|
|
6 | (1) |
|
|
7 | (1) |
|
|
7 | (2) |
Part 1: Getting Started with R Programming |
|
9 | (40) |
|
Chapter 1 Introducing R: The Big Picture |
|
|
11 | (8) |
|
Recognizing the Benefits of Using R |
|
|
12 | (3) |
|
It comes as free, open-source code |
|
|
12 | (1) |
|
|
13 | (1) |
|
|
13 | (1) |
|
It provides an engaged community |
|
|
13 | (1) |
|
It connects with other languages |
|
|
14 | (1) |
|
Looking At Some of the Unique Features of R |
|
|
15 | (4) |
|
Performing multiple calculations with vectors |
|
|
15 | (1) |
|
Processing more than just statistics |
|
|
16 | (1) |
|
Running code without a compiler |
|
|
16 | (3) |
|
|
19 | (16) |
|
Working with a Code Editor |
|
|
20 | (5) |
|
|
21 | (2) |
|
|
23 | (2) |
|
Starting Your First R Session |
|
|
25 | (4) |
|
Saying hello to the world |
|
|
25 | (1) |
|
|
26 | (1) |
|
|
26 | (1) |
|
Storing and calculating values |
|
|
27 | (1) |
|
|
28 | (1) |
|
|
29 | (3) |
|
|
30 | (2) |
|
Navigating the Environment |
|
|
32 | (3) |
|
Manipulating the content of the environment |
|
|
32 | (1) |
|
|
33 | (1) |
|
|
34 | (1) |
|
Chapter 3 The Fundamentals of R |
|
|
35 | (14) |
|
Using the Full Power of Functions |
|
|
35 | (5) |
|
Vectorizing your functions |
|
|
36 | (1) |
|
Putting the argument in a function |
|
|
37 | (2) |
|
|
39 | (1) |
|
Keeping Your Code Readable |
|
|
40 | (5) |
|
Following naming conventions |
|
|
40 | (3) |
|
|
43 | (2) |
|
|
45 | (1) |
|
Getting from Base R to More |
|
|
45 | (6) |
|
|
45 | (1) |
|
|
46 | (1) |
|
Loading and unloading packages |
|
|
46 | (3) |
Part II: Getting Down to Work in R |
|
49 | (100) |
|
Chapter 4 Getting Started with Arithmetic |
|
|
51 | (28) |
|
Working with Numbers, Infinity, and Missing Values |
|
|
51 | (9) |
|
|
52 | (2) |
|
Using mathematical functions |
|
|
54 | (3) |
|
Calculating whole vectors |
|
|
57 | (1) |
|
|
58 | (2) |
|
Organizing Data in Vectors |
|
|
60 | (5) |
|
Discovering the properties of vectors |
|
|
61 | (2) |
|
|
63 | (1) |
|
|
64 | (1) |
|
|
64 | (1) |
|
Getting Values in and out of Vectors |
|
|
65 | (3) |
|
Understanding indexing in R |
|
|
65 | (1) |
|
Extracting values from a vector |
|
|
66 | (1) |
|
Changing values in a vector |
|
|
67 | (1) |
|
Working with Logical Vectors |
|
|
68 | (5) |
|
|
69 | (1) |
|
Using logical vectors as indices |
|
|
70 | (1) |
|
Combining logical statements |
|
|
71 | (1) |
|
Summarizing logical vectors |
|
|
72 | (1) |
|
|
73 | (6) |
|
Using arithmetic vector operations |
|
|
73 | (3) |
|
|
76 | (3) |
|
Chapter 5 Getting Started with Reading and Writing |
|
|
79 | (24) |
|
Using Character Vectors for Text Data |
|
|
79 | (5) |
|
Assigning a value to a character vector |
|
|
80 | (1) |
|
Creating a character vector with more than one element |
|
|
80 | (1) |
|
Extracting a subset of a vector |
|
|
81 | (1) |
|
Naming the values in your vectors |
|
|
82 | (2) |
|
|
84 | (10) |
|
String theory: Combining and splitting strings |
|
|
84 | (4) |
|
|
88 | (1) |
|
|
89 | (2) |
|
|
91 | (1) |
|
Revving up with regular expressions |
|
|
92 | (2) |
|
|
94 | (9) |
|
|
95 | (1) |
|
|
96 | (2) |
|
|
98 | (1) |
|
Distinguishing data types |
|
|
99 | (1) |
|
Working with ordered factors |
|
|
100 | (3) |
|
Chapter 6 Going on a Date with R |
|
|
103 | (10) |
|
|
104 | (2) |
|
Presenting Dates in Different Formats |
|
|
106 | (1) |
|
Adding Time Information to Dates |
|
|
107 | (2) |
|
Formatting Dates and Times |
|
|
109 | (1) |
|
Performing Operations on Dates and Times |
|
|
109 | (4) |
|
|
109 | (1) |
|
|
110 | (1) |
|
|
111 | (2) |
|
Chapter 7 Working in More Dimensions |
|
|
113 | (36) |
|
Adding a Second Dimension |
|
|
113 | (5) |
|
Discovering a new dimension |
|
|
114 | (3) |
|
Combining vectors into a matrix |
|
|
117 | (1) |
|
|
118 | (3) |
|
Extracting values from a matrix |
|
|
118 | (2) |
|
Replacing values in a matrix |
|
|
120 | (1) |
|
Naming Matrix Rows and Columns |
|
|
121 | (2) |
|
Changing the row and column names |
|
|
122 | (1) |
|
|
123 | (1) |
|
Calculating with Matrices |
|
|
123 | (4) |
|
Using standard operations with matrices |
|
|
124 | (1) |
|
Calculating row and column summaries |
|
|
125 | (1) |
|
|
126 | (1) |
|
|
127 | (3) |
|
|
128 | (1) |
|
Using dimensions to extract values |
|
|
129 | (1) |
|
Combining Different Types of Values in a Data Frame |
|
|
130 | (4) |
|
Creating a data frame from a matrix |
|
|
130 | (2) |
|
Creating a data frame from scratch |
|
|
132 | (1) |
|
Naming variables and observations |
|
|
133 | (1) |
|
Manipulating Values in a Data Frame |
|
|
134 | (6) |
|
Extracting variables, observations, and values |
|
|
135 | (1) |
|
Adding observations to a data frame |
|
|
136 | (3) |
|
Adding variables to a data frame |
|
|
139 | (1) |
|
Combining Different Objects in a List |
|
|
140 | (11) |
|
|
141 | (1) |
|
Extracting components from lists |
|
|
142 | (2) |
|
Changing the components in lists |
|
|
144 | (2) |
|
Reading the output of str() for lists |
|
|
146 | (2) |
|
Seeing the forest through the trees |
|
|
148 | (1) |
Part III: Coding in R |
|
149 | (70) |
|
Chapter 8 Putting the Fun in Functions |
|
|
151 | (20) |
|
Moving from Scripts to Functions |
|
|
151 | (6) |
|
|
152 | (1) |
|
|
153 | (1) |
|
|
154 | (1) |
|
Reducing the number of lines |
|
|
155 | (2) |
|
Using Arguments the Smart Way |
|
|
157 | (6) |
|
|
157 | (2) |
|
Conjuring tricks with dots |
|
|
159 | (2) |
|
Using functions as arguments |
|
|
161 | (2) |
|
|
163 | (2) |
|
|
164 | (1) |
|
|
165 | (6) |
|
Finding the methods behind the function |
|
|
166 | (2) |
|
|
168 | (3) |
|
Chapter 9 Controlling the Logical Flow |
|
|
171 | (22) |
|
Making Choices with if Statements |
|
|
172 | (2) |
|
Doing Something Else with an if...else Statement |
|
|
174 | (2) |
|
|
176 | (2) |
|
|
176 | (1) |
|
Choosing based on a logical vector |
|
|
176 | (2) |
|
|
178 | (3) |
|
Chaining if...else statements |
|
|
178 | (2) |
|
Switching between possibilities |
|
|
180 | (1) |
|
|
181 | (3) |
|
|
181 | (1) |
|
Calculating values in a for loop |
|
|
182 | (2) |
|
Looping without Loops: Meeting the Apply Family |
|
|
184 | (9) |
|
Looking at the family features |
|
|
185 | (1) |
|
Meeting three of the, members |
|
|
185 | (1) |
|
Applying functions on rows and columns |
|
|
186 | (2) |
|
Applying functions to listlike objects |
|
|
188 | (5) |
|
Chapter 10 Debugging Your Code |
|
|
193 | (16) |
|
|
193 | (1) |
|
Reading Errors and Warnings |
|
|
194 | (3) |
|
|
194 | (1) |
|
Caring about warnings (or not) |
|
|
195 | (2) |
|
|
197 | (5) |
|
|
197 | (1) |
|
Knowing where an error comes from |
|
|
197 | (1) |
|
Looking inside a function |
|
|
198 | (4) |
|
Generating Your Own Messages |
|
|
202 | (2) |
|
|
203 | (1) |
|
|
203 | (1) |
|
Recognizing the Mistakes You're Sure to Make |
|
|
204 | (5) |
|
Starting with the wrong data |
|
|
204 | (1) |
|
Having your data in the wrong format |
|
|
205 | (4) |
|
|
209 | (10) |
|
Finding Information in the R Help Files |
|
|
209 | (3) |
|
When you know exactly what you're looking for |
|
|
210 | (1) |
|
When you don't know exactly what you're looking for |
|
|
211 | (1) |
|
Searching the Web for Help with R |
|
|
212 | (1) |
|
Getting Involved in the R Community |
|
|
213 | (2) |
|
Discussing R on Stack Overflow and Stack Exchange |
|
|
213 | (1) |
|
Using the R mailing lists |
|
|
214 | (1) |
|
|
215 | (1) |
|
Making a Minimal Reproducible Example |
|
|
215 | (6) |
|
Creating sample data with random values |
|
|
215 | (2) |
|
|
217 | (1) |
|
Providing the necessary information |
|
|
217 | (2) |
Part Ilk Making the Data Talk |
|
219 | (106) |
|
Chapter 12 Getting Data into and out of R |
|
|
221 | (18) |
|
|
221 | (11) |
|
Entering data in the R text editor |
|
|
222 | (1) |
|
Using the Clipboard to copy and paste |
|
|
223 | (2) |
|
Reading data in CSV files |
|
|
225 | (4) |
|
|
229 | (1) |
|
Working with other data types |
|
|
230 | (2) |
|
Getting Your Data out of R |
|
|
232 | (1) |
|
Working with Files and Folders |
|
|
233 | (6) |
|
Understanding the working directory |
|
|
233 | (1) |
|
|
234 | (5) |
|
Chapter 13 Manipulating and Processing Data |
|
|
239 | (36) |
|
Deciding on the Most Appropriate Data Structure |
|
|
239 | (2) |
|
Creating Subsets of Your Data |
|
|
241 | (6) |
|
Understanding the three subset operators |
|
|
241 | (1) |
|
Understanding the five ways of specifying the subset |
|
|
242 | (1) |
|
|
242 | (5) |
|
Adding Calculated Fields to Data |
|
|
247 | (4) |
|
Doing arithmetic on columns of a data frame |
|
|
247 | (1) |
|
Using with and transform to improve code readability |
|
|
248 | (1) |
|
Creating subgroups or bins of data |
|
|
249 | (2) |
|
Combining and Merging Data Sets |
|
|
251 | (6) |
|
Creating sample data to illustrate merging |
|
|
252 | (1) |
|
Using the merge() function |
|
|
253 | (2) |
|
Working with lookup tables |
|
|
255 | (2) |
|
Sorting and Ordering Data |
|
|
257 | (3) |
|
|
257 | (1) |
|
|
258 | (2) |
|
Traversing Your Data with the Apply Functions |
|
|
260 | (6) |
|
Using the apply() function to summarize arrays |
|
|
261 | (2) |
|
Using lapply() and sapply() to traverse a list or data frame |
|
|
263 | (1) |
|
Using tapply() to create tabular summaries |
|
|
264 | (2) |
|
Getting to Know the Formula Interface |
|
|
266 | (2) |
|
Whipping Your Data into Shape |
|
|
268 | (7) |
|
Understanding data in long and wide formats |
|
|
269 | (1) |
|
Getting started with the reshape2 package |
|
|
270 | (1) |
|
Melting data to long format |
|
|
270 | (1) |
|
Casting data to wide format |
|
|
271 | (4) |
|
Chapter 14 Summarizing Data |
|
|
275 | (24) |
|
Starting with the Right Data |
|
|
275 | (3) |
|
Using factors or numeric data |
|
|
276 | (1) |
|
|
277 | (1) |
|
|
277 | (1) |
|
Describing Continuous Variables |
|
|
278 | (3) |
|
Talking about the center of your data |
|
|
278 | (1) |
|
|
279 | (1) |
|
|
279 | (2) |
|
|
281 | (2) |
|
|
281 | (1) |
|
|
282 | (1) |
|
|
282 | (1) |
|
|
283 | (4) |
|
|
283 | (2) |
|
Using frequencies or densities |
|
|
285 | (2) |
|
Describing Multiple Variables |
|
|
287 | (6) |
|
Summarizing a complete dataset |
|
|
287 | (1) |
|
Plotting quantiles for subgroups |
|
|
288 | (2) |
|
|
290 | (3) |
|
|
293 | (6) |
|
|
294 | (1) |
|
Converting tables to a data frame |
|
|
295 | (1) |
|
Looking at margins and proportions |
|
|
296 | (3) |
|
Chapter 15 Testing Differences and Relations |
|
|
299 | (26) |
|
Taking a Closer Look at Distributions |
|
|
300 | (5) |
|
|
300 | (1) |
|
Testing normality graphically |
|
|
301 | (1) |
|
|
302 | (2) |
|
Testing normality in a formal way |
|
|
304 | (1) |
|
|
305 | (4) |
|
|
305 | (3) |
|
|
308 | (1) |
|
Testing Counts and Proportions |
|
|
309 | (4) |
|
|
309 | (1) |
|
|
310 | (2) |
|
|
312 | (1) |
|
|
313 | (14) |
|
|
313 | (2) |
|
Evaluating the differences |
|
|
315 | (3) |
|
Modeling linear relations |
|
|
318 | (2) |
|
|
320 | (3) |
|
|
323 | (2) |
Part V: Working With Graphics |
|
325 | (50) |
|
Chapter 16 Using Base Graphics |
|
|
327 | (16) |
|
Creating Different Types of Plots |
|
|
327 | (7) |
|
Getting an overview of plot |
|
|
328 | (1) |
|
Adding points and lines to a plot |
|
|
329 | (3) |
|
|
332 | (2) |
|
Controlling Plot Options and Arguments |
|
|
334 | (6) |
|
Adding titles and axis labels |
|
|
335 | (1) |
|
|
335 | (4) |
|
Putting multiple plots on a single page |
|
|
339 | (1) |
|
Saving Graphics to Image Files |
|
|
340 | (3) |
|
Chapter 17 Creating Faceted Graphics with Lattice |
|
|
343 | (18) |
|
|
344 | (4) |
|
Loading the lattice package |
|
|
345 | (1) |
|
Making a lattice scatterplot |
|
|
345 | (1) |
|
|
346 | (2) |
|
|
348 | (3) |
|
|
348 | (1) |
|
Changing the font size of titles and labels |
|
|
349 | (1) |
|
Using themes to modify plot options |
|
|
350 | (1) |
|
|
351 | (3) |
|
|
352 | (1) |
|
Making a box-and-whisker plot |
|
|
353 | (1) |
|
|
354 | (3) |
|
Using data in tall format |
|
|
354 | (2) |
|
Creating a chart with groups |
|
|
356 | (1) |
|
|
356 | (1) |
|
Printing and Saving a Lattice Plot |
|
|
357 | (4) |
|
Assigning a lattice plot to an object |
|
|
358 | (1) |
|
Printing a lattice plot in a script |
|
|
358 | (1) |
|
Saving a lattice plot to file |
|
|
358 | (3) |
|
Chapter 18 Looking At ggplot2 Graphics |
|
|
361 | (14) |
|
Installing and Loading ggplot2 |
|
|
361 | (1) |
|
|
362 | (1) |
|
|
363 | (6) |
|
Defining what data to use |
|
|
364 | (1) |
|
Mapping data to plot aesthetics |
|
|
364 | (1) |
|
|
365 | (4) |
|
|
369 | (2) |
|
Adding Facets, Scales, and Options |
|
|
371 | (3) |
|
|
371 | (1) |
|
|
372 | (2) |
|
|
374 | (1) |
Part VI: The Part of Tens |
|
375 | (20) |
|
Chapter 19 Ten Things You Can Do in R That You Would've Done in Microsoft Excel |
|
|
377 | (10) |
|
Adding Row and Column Totals |
|
|
377 | (1) |
|
|
378 | (2) |
|
|
380 | (1) |
|
|
380 | (1) |
|
Calculating Conditional Totals |
|
|
381 | (1) |
|
Transposing Columns or Rows |
|
|
382 | (1) |
|
Finding Unique or Duplicated Values |
|
|
383 | (1) |
|
Working with Lookup Tables |
|
|
383 | (1) |
|
Working with Pivot Tables |
|
|
384 | (1) |
|
Using the Goal Seek and Solver |
|
|
385 | (2) |
|
Chapter 20 Ten Tips on Working with Packages |
|
|
387 | (8) |
|
Poking Around the Nooks and Crannies of CRAN |
|
|
387 | (1) |
|
Finding Interesting Packages |
|
|
388 | (1) |
|
|
389 | (1) |
|
|
389 | (1) |
|
Reading the Package Manual and Vignette |
|
|
390 | (1) |
|
|
390 | (1) |
|
Forging Ahead with R-Forge |
|
|
391 | (1) |
|
Getting packages from github |
|
|
392 | (1) |
|
Conducting Installations from BioConductor |
|
|
392 | (1) |
|
|
393 | (2) |
Appendix A: Installing R and RStudio |
|
395 | (6) |
|
Installing and Configuring R |
|
|
395 | (3) |
|
|
395 | (1) |
|
|
396 | (2) |
|
Installing and Configuring RStudio |
|
|
398 | (3) |
|
|
398 | (1) |
|
|
398 | (3) |
Appendix B: The rfordummies Package |
|
401 | (2) |
|
|
401 | (2) |
Index |
|
403 | |