R
Notes for ProfessionalsRNotes for Professionals
400+ pages
of professional hints and tricks
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial R group(s) or company(s).
All trademarks and registered trademarks are
the property of their respective owners
Contents
About
...................................................................................................................................................................................
1
Chapter 1: Getting started with R Language
..................................................................................................
2
Section 1.1: Installing R
Section 1.2: Hello World!
Section 1.3: Getting Help
Section 1.4: Interactive mode and R scripts
...................................................................................................................................................
................................................................................................................................................
...............................................................................................................................................
................................................................................................................
3
Chapter 2: Variables
....................................................................................................................................................
7
Section 2.1: Variables, data structures and basic Operations
..................................................................................
7
Chapter 3: Arithmetic Operators
Section 3.1: Range and addition
Section 3.2: Addition and subtraction
........................................................................................................................
.................................................................................................................................
.......................................................................................................................
10
Chapter 4: Matrices
...................................................................................................................................................
13
Section 4.1: Creating matrices
....................................................................................................................................
13
Chapter 5: Formula
....................................................................................................................................................
15
Section 5.1: The basics of formula
.............................................................................................................................
15
Chapter 6: Reading and writing strings
Section 6.1: Printing and displaying strings
Section 6.2: Capture output of operating system command
Section 6.3: Reading from or writing to a file connection
Chapter 7: String manipulation with stringi package
..........................................................................................................
...............................................................................................................
.................................................................................
.......................................................................................
..............................................................................
21
Section 7.1: Count pattern inside string
Section 7.2: Duplicating strings
Section 7.3: Paste vectors
Section 7.4: Splitting text by some fixed pattern
.....................................................................................................................
..................................................................................................................................
...........................................................................................................................................
......................................................................................................
22
Chapter 8: Classes
......................................................................................................................................................
23
Section 8.1: Inspect classes
Section 8.2: Vectors and lists
Section 8.3: Vectors
.........................................................................................................................................
23
.....................................................................................................................................
23
.....................................................................................................................................................
24
Chapter 9: Lists
............................................................................................................................................................
25
Section 9.1: Introduction to lists
Section 9.2: Quick Introduction to Lists
Section 9.3: Serialization: using lists to pass information
..................................................................................................................................
.....................................................................................................................
........................................................................................
27
Chapter 10: Hashmaps
.............................................................................................................................................
29
Section 10.1: Environments as hash maps
Section 10.2: package:hash
Section 10.3: package:listenv
Chapter 11: Creating vectors
........................................................................................................................................
......................................................................................................................................
.................................................................................................................................
................................................................................................................
29
Section 11.1: Vectors from build in constants: Sequences of letters & month names
Section 11.2: Creating named vectors
Section 11.3: Sequence of numbers
Section 11.4: seq()
Section 11.5: Vectors
Section 11.6: Expanding a vector with the rep() function
........................................................................................................................
............................................................................................................................
.........................................................................................................................................................
....................................................................................................................................................
.........................................................................................
...........................................
35
Chapter 12: Date and Time
....................................................................................................................................
41
Section 12.1: Current Date and Time
..........................................................................................................................
41
2
3
3
10
10
17
17
18
Advertisement
19
21
21
22
25
25
32
33
35
35
37
37
38
39
Section 12.2: Go to the End of the Month
Section 12.3: Go to First Day of the Month
Section 12.4: Move a date a number of months consistently by months
..................................................................................................................
................................................................................................................
.............................................................
42
Chapter 13: The Date class
Section 13.1: Formatting Dates
Section 13.2: Parsing Strings into Date Objects
Section 13.3: Dates
.....................................................................................................................................
...................................................................................................................................
........................................................................................................
44
.......................................................................................................................................................
45
Chapter 14: Date-time classes (POSIXct and POSIXlt)
............................................................................
47
Section 14.1: Formatting and printing date-time objects
Section 14.2: Date-time arithmetic
Section 14.3: Parsing strings into date-time objects
.............................................................................................................................
................................................................................................
.........................................................................................
47
Chapter 15: The character class
..........................................................................................................................
50
Section 15.1: Coercion
..................................................................................................................................................
50
Chapter 16: Numeric classes and storage modes
......................................................................................
51
Section 16.1: Numeric
...................................................................................................................................................
51
Chapter 17: The logical class
.................................................................................................................................
53
Section 17.1: Logical operators
Section 17.2: Coercion
Section 17.3: Interpretation of NAs
...................................................................................................................................
.................................................................................................................................................
.............................................................................................................................
53
Chapter 18: Data frames
.........................................................................................................................................
55
Section 18.1: Create an empty data.frame
Section 18.2: Subsetting rows and columns from a data frame
Section 18.3: Convenience functions to manipulate data.frames
Section 18.4: Introduction
Section 18.5: Convert all columns of a data.frame to character class
................................................................................................................
............................................................................
..........................................................................
............................................................................................................................................
..................................................................
61
Chapter 19: Split function
.......................................................................................................................................
63
Section 19.1: Using split in the split-apply-combine paradigm
Section 19.2: Basic usage of split
...............................................................................................................................
64
...............................................................................
63
Chapter 20: Reading and writing tabular data in plain-text files (CSV, TSV, etc.)
...................
67
Section 20.1: Importing .csv files
Section 20.2: Importing with data.table
Section 20.3: Exporting .csv files
Section 20.4: Import multiple csv files
Section 20.5: Importing fixed-width files
................................................................................................................................
....................................................................................................................
................................................................................................................................
.......................................................................................................................
...................................................................................................................
69
Chapter 21: Pipe operators (%>% and others)
.............................................................................................
71
Section 21.1: Basic use and chaining
Section 21.2: Functional sequences
Section 21.3: Assignment with %<>%
Section 21.4: Exposing contents with %$%
Section 21.5: Creating side eects with %T>%
Section 21.6: Using the pipe with dplyr and ggplot2
..........................................................................................................................
...........................................................................................................................
..........................................................................................................................
................................................................................................................
..........................................................................................................
................................................................................................
75
Chapter 22: Linear Models (Regression)
.........................................................................................................
76
Section 22.1: Linear regression on the mtcars dataset
Section 22.2: Using the 'predict' function
Section 22.3: Weighting
Section 22.4: Checking for nonlinearity with polynomial regression
Section 22.5: Plotting The Regression (base)
Section 22.6: Quality assessment
...........................................................................................................
..............................................................................................................................
.....................................................................
81
...........................................................................................
..................................................................................................................
..............................................................................................................................................
Chapter 23: data.table
Advertisement
.............................................................................................................................................
87
41
42
44
44
47
48
53
53
55
56
59
60
67
68
69
69
71
72
73
73
74
76
78
79
83
85
Section 23.1: Creating a data.table
Section 23.2: Special symbols in data.table
Section 23.3: Adding and modifying columns
Section 23.4: Writing code compatible with both data.frame and data.table
Section 23.5: Setting keys in data.table
............................................................................................................................
.............................................................................................................
..........................................................................................................
....................................................................................................................
93
......................................................
91
Chapter 24: Pivot and unpivot with data.table
..........................................................................................
95
Section 24.1: Pivot and unpivot tabular data with data.table - I
Section 24.2: Pivot and unpivot tabular data with data.table - II
.............................................................................
95
...........................................................................
96
Chapter 25: Bar Chart
..............................................................................................................................................
98
Section 25.1: barplot() function
..................................................................................................................................
98
Chapter 26: Base Plotting
Section 26.1: Density plot
Section 26.2: Combining Plots
Section 26.3: Getting Started with R_Plots
Section 26.4: Basic Plot
Section 26.5: Histograms
Section 26.6: Matplot
Section 26.7: Empirical Cumulative Distribution Function
....................................................................................................................................
..........................................................................................................................................
..................................................................................................................................
.............................................................................................................
.............................................................................................................................................
..........................................................................................................................................
................................................................................................................................................
.....................................................................................
119
Chapter 27: boxplot
.................................................................................................................................................
121
Section 27.1: Create a box-and-whisker plot with boxplot() {graphics}
Section 27.2: Additional boxplot style parameters
................................................................................................
125
..............................................................
121
Chapter 28: ggplot2
................................................................................................................................................
128
....................................................................................................................
.........................................................................................................
..........................................................................................
133
Section 28.1: Displaying multiple plots
Section 28.2: Prepare your data for plotting
Section 28.3: Add horizontal and vertical lines to plot
Section 28.4: Scatter Plots
Section 28.5: Produce basic plots with qplot
Section 28.6: Vertical and Horizontal Bar Chart
Section 28.7: Violin plot
........................................................................................................................................
..........................................................................................................
....................................................................................................
.............................................................................................................................................
Chapter 29: Factors
.................................................................................................................................................
143
Section 29.1: Consolidating Factor Levels with a List
Section 29.2: Basic creation of factors
Section 29.3: Changing and reordering factors
Section 29.4: Rebuilding factors from zero
...................................................................................................................
.....................................................................................................
............................................................................................................
............................................................................................
143
Chapter 30: Pattern Matching and Replacement
....................................................................................
152
Section 30.1: Finding Matches
Section 30.2: Single and Global match
Section 30.3: Making substitutions
Section 30.4: Find matches in big data sets
..................................................................................................................................
...................................................................................................................
..........................................................................................................................
...........................................................................................................
154
Chapter 31: Run-length encoding
.....................................................................................................................
156
............................................................................................................
.....................................................................................
Section 31.1: Run-length Encoding with rle
Section 31.2: Identifying and grouping by runs in base R
Section 31.3: Run-length encoding to compress and decompress vectors
Section 31.4: Identifying and grouping by runs in data.table
Chapter 32: Speeding up tough-to-vectorize code
Advertisement
Section 32.1: Speeding tough-to-vectorize for loops with Rcpp
Section 32.2: Speeding tough-to-vectorize for loops by byte compiling
........................................................
157
...............................................................................
.................................................................................
...........................................................................
............................................................
159
Chapter 33: Introduction to Geographical Maps
......................................................................................
161
Section 33.1: Basic map-making with map() from the package maps
...............................................................
161
87
88
89
104
104
105
107
108
111
113
128
131
136
136
138
140
144
145
150
152
153
154
156
156
158
159
159
Section 33.2: 50 State Maps and Advanced Choropleths with Google Viz
Section 33.3: Interactive plotly maps
Section 33.4: Making Dynamic HTML Maps with Leaflet
Section 33.5: Dynamic Leaflet maps in Shiny applications
......................................................................................
..................................................................................
......................................................................................................................
.........................................................
164
Chapter 34: Set operations
.................................................................................................................................
171
Section 34.1: Set operators for pairs of vectors
Section 34.2: Cartesian or "cross" products of vectors
Section 34.3: Set membership for vectors
Section 34.4: Make unique / drop duplicates / select distinct elements from a vector
Section 34.5: Measuring set overlaps / Venn diagrams for vectors
.....................................................................................................
.........................................................................................
..............................................................................................................
...................................................................
173
....................................
172
Chapter 35: tidyverse
.............................................................................................................................................
174
Section 35.1: tidyverse: an overview
Section 35.2: Creating tbl_df’s
........................................................................................................................
174
.................................................................................................................................
175
Chapter 36: Rcpp
......................................................................................................................................................
176
Section 36.1: Extending Rcpp with Plugins
Section 36.2: Inline Code Compile
Section 36.3: Rcpp Attributes
Section 36.4: Specifying Additional Build Dependencies
..............................................................................................................
............................................................................................................................
...................................................................................................................................
......................................................................................
178
Chapter 37: Random Numbers Generator
..................................................................................................
179
Section 37.1: Random permutations
Section 37.2: Generating random numbers using various density functions
Section 37.3: Random number generator's reproducibility
.....................................................
179
..................................................................................
181
........................................................................................................................
179
Chapter 38: Parallel processing
........................................................................................................................
182
........................................................................................
182
......................................................................................
183
Section 38.1: Parallel processing with parallel package
Section 38.2: Parallel processing with foreach package
Section 38.3: Random Number Generation
Section 38.4: mcparallelDo
Chapter 39: Subsetting
Section 39.1: Data frames
Section 39.2: Atomic vectors
Section 39.3: Matrices
Section 39.4: Lists
Section 39.5: Vector indexing
Section 39.6: Other objects
Section 39.7: Elementwise Matrix Operations
............................................................................................................
.......................................................................................................................................
..........................................................................................................................................
.........................................................................................................................................
....................................................................................................................................
...............................................................................................................................................
......................................................................................................................................................
...................................................................................................................................
.......................................................................................................................................
........................................................................................................
192
Chapter 40: Debugging
Section 40.1: Using debug
Section 40.2: Using browser
.........................................................................................................................................
Advertisement
........................................................................................................................................
.....................................................................................................................................
194
Chapter 41: Installing packages
.......................................................................................................................
196
Section 41.1: Install packages from GitHub
Section 41.2: Download and install packages from repositories
Section 41.3: Install package from local source
Section 41.4: Install local development version of a package
Section 41.5: Using a CLI package manager -- basic pacman usage
.............................................................................................................
.........................................................................
.....................................................................................................
..............................................................................
.................................................................
199
Chapter 42: Inspecting packages
Section 42.1: View Package Version
Section 42.2: View Loaded packages in Current Session
Section 42.3: View package information
Section 42.4: View package's built-in data sets
....................................................................................................................
........................................................................................................................
.....................................................................................
200
................................................................................................................
.....................................................................................................
165
167
168
171
171
172
176
176
177
184
184
186
186
187
188
190
191
192
194
194
196
197
198
198
200
200
200
200
Section 42.5: List a package's exported functions
Chapter 43: Creating packages with devtools
Section 43.1: Creating and distributing packages
Section 43.2: Creating vignettes
................................................................................................
200
.........................................................................................
201
..................................................................................................
..............................................................................................................................
Chapter 44: Using pipe assignment in your own package %<>%: How to ?
..............................
204
Section 44.1: Putting the pipe in a utility-functions file
..........................................................................................
204
Chapter 45: Arima Models
...................................................................................................................................
205
Section 45.1: Modeling an AR1 Process with Arima
................................................................................................
205
Chapter 46: Distribution Functions
Section 46.1: Normal distribution
Section 46.2: Binomial Distribution
.................................................................................................................
210
.............................................................................................................................
..........................................................................................................................
Chapter 47: Shiny
.....................................................................................................................................................
214
Section 47.1: Create an app
Section 47.2: Checkbox Group
Section 47.3: Radio Button
Section 47.4: Debugging
Section 47.5: Select box
Section 47.6: Launch a Shiny app
Section 47.7: Control widgets
Chapter 48: spatial analysis
......................................................................................................................................
.................................................................................................................................
.......................................................................................................................................
...........................................................................................................................................
............................................................................................................................................
............................................................................................................................
217
...................................................................................................................................
...............................................................................................................................
Section 48.1: Create spatial points from XY data set
Section 48.2: Importing a shape file (.shp)
.............................................................................................
220
.............................................................................................................
221
Chapter 49: sqldf
......................................................................................................................................................
222
Section 49.1: Basic Usage Examples
.......................................................................................................................
222
Chapter 50: Code profiling
..................................................................................................................................
224
Section 50.1: Benchmarking using microbenchmark
Section 50.2: proc.time()
Section 50.3: Microbenchmark
Section 50.4: System.time
Section 50.5: Line Profiling
...........................................................................................................................................
................................................................................................................................
........................................................................................................................................
.......................................................................................................................................
............................................................................................
224
Chapter 51: Control flow structures
...............................................