R for Programmers
TABLE OF CONTENTS Preface xxi Chapter 1 Introduction to R Programming ......................................................... 1 Introduction ............................................................................................. 1 The R Environment ................................................................................. 2 Why the R Language? ............................................................................ 3 Why R for Data Analysis? ......................................................................4 How R Programming is Applied to Real World ........................................5 Why Should You Adopt R Programming? ................................................6 Why R? The pros and cons of the R Language .......................................8 History .................................................................................................... 9 Statistical Features ................................................................................ 10 Comparison of R with Other Technologies ............................................ 12 Programming Features .......................................................................... 12 Packages .............................................................................................. 13 Milestones ............................................................................................. 18 Interfaces ............................................................................................. 19 Implementations .................................................................................... 19 R Environment Setup ............................................................................ 20 Local Environment Setup ...................................................................... 20 R Journal .............................................................................................. 21 Comparison with SAS, SPSS, and STATA ............................................ 21 Commercial Support for R .................................................................... 21 Examples .............................................................................................. 22 Basic Syntax ......................................................................................... 22 vi What are Comments in R Programming ................................................ 22 Another Example .................................................................................. 23 Structure of a Function.......................................................................... 23 Working with R Scripts ......................................................................... 23 R Graphical User Interface (R Gui) ...................................................... 24 R Studio ................................................................................................ 24 R Scripting ............................................................................................ 25 Sourcing a Script in R ........................................................................... 25 R Applications ....................................................................................... 26 Basic Data Types ................................................................................. 26 Numeric ................................................................................................ 27 Integer .................................................................................................. 27 Complex ............................................................................................... 28 Logical .................................................................................................. 28 Character .............................................................................................. 29 R Reserved Words ................................................................................ 29 Variables & Constants .......................................................................... 30 Variables in R ....................................................................................... 30 Constants in R ...................................................................................... 31 R Operators .......................................................................................... 32 R Arithmetic Operators ......................................................................... 32 R Relational Operators .......................................................................... 33 Operation on Vectors ............................................................................ 33 R Logical Operators .............................................................................. 34 R Assignment Operators ....................................................................... 35 Precedence & Associativity .................................................................. 35 Operator Precedence ............................................................................ 35 Operator Associativity........................................................................... 36 Precedence and Associativity of Different Operators in R from Highest to Lowest .............................................................................................. 36 Chapter 2 R Flow Control .................................................................................. 37 Decision Making ................................................................................... 37 R if Statement ....................................................................................... 38 if…else Statement ................................................................................ 38 if…else Ladder ..................................................................................... 39 Switch Statement .................................................................................. 40 R ifelse() Function ................................................................................ 41 Syntax of ifelse() Function .................................................................... 42 Example: ifelse() Function ..................................................................... 42 Loop ..................................................................................................... 42 Loop Control Statements ....................................................................... 43 R - Repeat Loop ................................................................................... 43 vii R – While Loop..................................................................................... 44 R – For Loop ........................................................................................ 46 R - Break and Next Statement .............................................................. 47 Chapter 3 R Functions ........................................................................................ 50 R – Functions ....................................................................................... 50 Function Definition ................................................................................ 50 Function Components ............................................................................ 50 Built-in Function .................................................................................... 51 User-defined Function........................................................................... 51 Calling a Function ................................................................................. 52 Calling a Function without an Argument ................................................ 52 Calling a Function with Argument Values (by position and by name) ...... 52 Calling a Function with Default Argument ............................................. 53 Lazy Evaluation of Function .................................................................. 53 Function Return Value........................................................................... 54 Syntax of Return() ................................................................................ 54 Example: Return() ................................................................................. 54 Functions without Return() .................................................................... 54 Multiple Returns .................................................................................... 55 R Environment & Scope ....................................................................... 55 R Programming Environment ................................................................ 56 Example: Cascading of Environments .................................................... 56 R Programming Scope .......................................................................... 57 Global Variables .................................................................................... 57 Local Variables ..................................................................................... 57 Accessing Global Variables ................................................................... 58 R Recursive Function ............................................................................ 59 Example: Recursive Function in R ......................................................... 59 R Infix Operator ................................................................................... 60 Example: How Infix Operators Work in R? ........................................... 60 Example: User Defined Infix Operator .................................................. 60 Predefined Infix Operators in R ............................................................ 61 R Switch Function................................................................................. 61 Syntax of switch() Function................................................................... 61 Example: switch() Function with as String Expression ........................... 62 Chapter 4 R Data Structure ................................................................................ 63 Vector ................................................................................................... 63 Reading Vectors .................................................................................... 63 How to Create Vector in R?.................................................................. 64 How to access Elements of a Vector? .................................................. 66 How to Modify a Vector in R? .............................................................. 67 viii Vector Manipulation .............................................................................. 67 How to Delete a Vector? ...................................................................... 68 Matrix ................................................................................................... 69 How to Create a Matrix in R programming? ......................................... 69 How to access Elements of a Matrix? .................................................. 71 How to Modify a Matrix in R? .............................................................. 74 Matrix Computations ............................................................................. 75 Array .................................................................................................... 77 Example ................................................................................................ 77 Naming Columns and Rows .................................................................. 77 Accessing Array Elements .................................................................... 78 Manipulating Array Elements ................................................................ 79 Calculations across Array Elements ...................................................... 79 Lists ...................................................................................................... 80 How to Create a List in R programming? .............................................. 80 How to Access Components of a list? ................................................... 81 How to modify a list in R? ..................................................................... 82 How to add Components to a list? ......................................................... 82 How to Delete Components from a list?................................................ 83 Merging Lists ........................................................................................ 83 Converting List to Vector ...................................................................... 83 Data Frames ......................................................................................... 84 Build-in Data Frame.............................................................................. 84 Check if a Variable is a Data Frame or not ........................................... 85 Functions of Data Frame ...................................................................... 85 How to Create a Data Frame in R? ...................................................... 86 How to Access Components of a Data Frame? .................................... 86 How to Modify a Data Frame in R? ...................................................... 88 Adding Components .............................................................................. 88 Deleting Component.............................................................................. 88 Data Frame Column Vector .................................................................. 89 Data Frame Column Slice ..................................................................... 89 Data Frame Row Slice.......................................................................... 90 Data Import .......................................................................................... 91 Factors .................................................................................................. 93 How to Create a Factor in R? ............................................................... 93 How to Access Components of a Factor? ............................................. 94 How to Modify a Factor? ...................................................................... 94 Changing the Order of Levels ............................................................... 95 Generating Factor Levels ...................................................................... 95 Chapter 5 Doing Math and Simulations in R .................................................... 96 Math Functions ..................................................................................... 96 ix Extended Example: Calculating a Probability ......................................... 97 Cumulative Sums and Products ............................................................. 97 Minima and Maxima ............................................................................. 98 Calculus ................................................................................................ 99 Functions for Statistical Distributions ..................................................... 99 Sorting ................................................................................................ 100 Linear Algebra Operations on Vectors and Matrices ........................... 101 Extended Example: Vector Cross Product ........................................... 104 Extended Example: Finding Stationary Distributions of Markov Chains 105 Set Operations .................................................................................... 107 Simulation Programming in R .............................................................. 108 Built-In Random Variate Generators .................................................... 108 Obtaining the Same Random Stream in Repeated Runs ....................... 109 Extended Example: A Combinatorial Simulation ................................... 110 Chapter 6 R Classes and Objects .................................................................... 112 Base Types ......................................................................................... 113 S3 Class .............................................................................................. 114 The Basic Idea ................................................................................... 114 Memory Management ......................................................................... 116 Creating an S3 Class ........................................................................... 116 Creating Methods ............................................................................... 119 Inheritance .......................................................................................... 124 S4 Class .............................................................................................. 127 The Basic Idea ................................................................................... 127 Creating an S4 Class ........................................................................... 129 Creating Methods ............................................................................... 132 Inheritance .......................................................................................... 136 Reference Class ................................................................................. 141 How to define a reference class? ........................................................ 141 How to Create a Reference Objects? ................................................. 141 How to Access and Modify Fields? ..................................................... 142 Reference Methods ............................................................................ 144 Inheritance in Reference Class ........................................................... 145 Chapter 7 Input/Output .................................................................................... 146 Objective ............................................................................................ 146 Accessing the Keyboard and Monitor ................................................. 146 Reading from the Keyboard ................................................................ 146 Reading and Writing Files .................................................................... 147 Reading a Data or Matrix from a File ................................................. 147 Reading a Single File One Line at a Time ............................................ 148 Writing a Table to a File ...................................................................... 148 x Introduction to Connections ................................................................. 148 Why Use PUMS? ............................................................................... 148 How Can I access PUMS? ................................................................. 149 Writing to a File................................................................................... 149 File and Directory Information ............................................................ 149 Set the Directory ................................................................................ 149 Getting a List of Files in a Directory.................................................... 149 Merging the Files into a Single Data Frame ......................................... 149 What is TCP/IP in R? ......................................................................... 150 What is the Protocol? .......................................................................... 150 What does TCP/IP Work? .................................................................. 151 TCP/IP Applications, Services, and Protocols ...................................... 151 TCP/IP Variable SMC-R Storage Allocations ...................................... 151 What are Sockets in R ........................................................................ 151 Chapter 8 String Manipulation ......................................................................... 153 Objective ............................................................................................ 153 What is String Manipulation in R? ....................................................... 153 R String Manipulation Functions .......................................................... 154 Regular Expressions ............................................................................ 157 Regular Expression Syntax .................................................................. 157 Use of String Utilities in the edtdbg Debugging Tool ............................ 157 Chapter 9 Debugging ........................................................................................ 159 What is Debugging? ............................................................................ 159 Fundamental Principles of Debugging ................................................. 159 The Essence of Debugging: The Principle of Confirmation .................. 159 Start Small .......................................................................................... 160 Debug in a Modular, Top-Down Manner ............................................. 160 Antibugging ......................................................................................... 160 Functions for Debugging in R Programming ........................................ 160 Traceback() ........................................................................................ 160 Debug() .............................................................................................. 161 Browser() ........................................................................................... 162 Trace()................................................................................................ 163 Recover() ........................................................................................... 164 Chapter 10 R Data Interfaces ............................................................................ 166 R - CSV Files ..................................................................................... 166 Getting and Setting the Working Directory .......................................... 166 Input as CSV File ............................................................................... 167 Reading a CSV File ............................................................................ 167 Analyzing the CSV File ....................................................................... 167 xi Writing into a CSV File ....................................................................... 169 R - Excel File ...................................................................................... 170 Install xlsx Package ............................................................................ 170 Verify and Load the “xlsx” Package.................................................... 170 Input as xlsx File ................................................................................. 170 Reading the Excel File ........................................................................ 171 R - Binary Files................................................................................... 171 Syntax ................................................................................................. 172 Example .............................................................................................. 172 Writing the Binary File ........................................................................ 172 Reading the Binary File ....................................................................... 172 R - XML Files ..................................................................................... 173 Input Data ........................................................................................... 174 Reading XML File............................................................................... 175 Details of the First Node ..................................................................... 177 XML to Data Frame ........................................................................... 178 R - JSON Files ................................................................................... 178 Install rjson Package ........................................................................... 178 Input Data ........................................................................................... 178 Read the JSON File ............................................................................ 179 Convert JSON to a Data Frame .......................................................... 179 R - Web Data ..................................................................................... 179 Install R Packages .............................................................................. 180 Input Data ........................................................................................... 180 Example .............................................................................................. 180 Verify the File Download .................................................................... 181 R – Databases .................................................................................... 181 RMySQL Package.............................................................................. 181 Connecting R to MySql ....................................................................... 181 Querying the Tables ............................................................................ 182 Query with Filter Clause ..................................................................... 182 Updating Rows in the Tables ............................................................... 182 Inserting Data into the Tables .............................................................. 183 Creating Tables in MySql .................................................................... 183 Dropping Tables in MySql ................................................................... 183 Chapter 11 R Charts and Graphs ....................................................................... 184 R - Pie Charts ..................................................................................... 184 Syntax ................................................................................................. 184 Example .............................................................................................. 185 Pie Chart Title and Colours ................................................................. 185 Example .............................................................................................. 185 Slice Percentages and Chart Legend ................................................... 186 3D Pie Chart ....................................................................................... 187 R - Bar Charts .................................................................................... 187 Syntax ................................................................................................. 187 Example .............................................................................................. 188 Bar Chart Labels, Title and Colours .................................................... 188 Example .............................................................................................. 188 Group Bar Chart and Stacked Bar Chart ............................................. 189 R – Boxplots ....................................................................................... 190 Syntax ................................................................................................. 190 Example .............................................................................................. 190 Creating the Boxplot ........................................................................... 191 Boxplot with Notch ............................................................................. 191 R – Histograms ................................................................................... 192 Syntax ................................................................................................. 192 Example .............................................................................................. 193 Range of X and Y Values .................................................................... 193 R - Line Graphs .................................................................................. 194 Syntax ................................................................................................. 194 Example .............................................................................................. 195 Line Chart Title, Colour and Labels ..................................................... 195 Multiple Lines in a Line Chart ............................................................. 196 R – Scatterplots .................................................................................. 197 Syntax ................................................................................................. 197 Example .............................................................................................. 197 Creating the Scatterplot ....................................................................... 198 Scatterplot Matrices ............................................................................ 199 Chapter 12 R Statistics Examples ...................................................................... 200 R - Mean, Median & Mode................................................................. 200 Mean .................................................................................................. 200 Median................................................................................................ 201 Mode .................................................................................................. 202 R - Linear Regression ......................................................................... 202 Types of Linear Regressions ............................................................... 203 Least Square Estimation...................................................................... 204 Checking Model Adequacy ................................................................. 204 Regression Assumptions ...................................................................... 205 Multicollinearity................................................................................... 205 Steps to Establish a Regression ........................................................... 206 Working with R Linear Regression ...................................................... 206 Simple Linear Regression in R ............................................................ 210 Linear Model Results Objects ............................................................. 211 xii Model Building .................................................................................... 212 Comparing Models .............................................................................. 213 Curvilinear Regression ........................................................................ 213 R - Multiple Regression....................................................................... 213 lm() Function ...................................................................................... 214 Example .............................................................................................. 214 Apply Equation for predicting New Values .......................................... 215 R - Logistic Regression ....................................................................... 216 Syntax ................................................................................................. 216 Example .............................................................................................. 216 Create Regression Model .................................................................... 217 Derivation of logistic regression with R ............................................... 217 Performance of Logistic Regression Model......................................... 218 Applications of Logistic Regression with R ......................................... 218 R - Normal Distribution ....................................................................... 218 dnorm() ............................................................................................... 219 pnorm() .