An Introduction to R
Total Page:16
File Type:pdf, Size:1020Kb
An introduction to R Longhow Lam Under Construction Feb-2010 some sections are unfinished! longhowlam at gmail dot com Contents 1 Introduction 7 1.1 What is R? . .7 1.2 The R environment . .8 1.3 Obtaining and installing R . .8 1.4 Your first R session . .9 1.5 The available help . 11 1.5.1 The on line help . 11 1.5.2 The R mailing lists and the R Journal . 12 1.6 The R workspace, managing objects . 12 1.7 R Packages . 13 1.8 Conflicting objects . 15 1.9 Editors for R scripts . 16 1.9.1 The editor in RGui . 16 1.9.2 Other editors . 16 2 Data Objects 19 2.1 Data types . 19 2.1.1 Double . 19 2.1.2 Integer . 20 2.1.3 Complex . 21 2.1.4 Logical . 21 2.1.5 Character . 22 2.1.6 Factor . 23 2.1.7 Dates and Times . 25 2.1.8 Missing data and Infinite values . 27 2.2 Data structures . 28 2.2.1 Vectors . 28 2.2.2 Matrices . 32 2.2.3 Arrays . 34 2.2.4 Data frames . 35 2.2.5 Time-series objects . 37 2.2.6 Lists . 38 2.2.7 The str function . 41 3 Importing data 42 3.1 Text files . 42 1 3.1.1 The scan function . 44 3.2 Excel files . 44 3.3 Databases . 45 3.4 The Foreign package . 46 4 Data Manipulation 47 4.1 Vector subscripts . 47 4.2 Matrix subscripts . 51 4.3 Manipulating Data frames . 54 4.3.1 Extracting data from data frames . 54 4.3.2 Adding columns to a data frame . 57 4.3.3 Combining data frames . 57 4.3.4 Merging data frames . 59 4.3.5 Aggregating data frames . 60 4.3.6 Stacking columns of data frames . 61 4.3.7 Reshaping data . 61 4.4 Attributes . 62 4.5 Character manipulation . 63 4.5.1 The functions nchar, substring and paste ............ 64 4.5.2 Finding patterns in character objects . 65 4.5.3 Replacing characters . 67 4.5.4 Splitting characters . 68 4.6 Creating factors from continuous data . 68 5 Writing functions 70 5.1 Introduction . 70 5.2 Arguments and variables . 72 5.2.1 Required and optional arguments . 72 5.2.2 The `...' argument . 73 5.2.3 Local variables . 73 5.2.4 Returning an object . 74 5.2.5 The Scoping rules . 75 5.2.6 Lazy evaluation . 76 5.3 Control flow . 77 5.3.1 Tests with if and switch ...................... 77 5.3.2 Looping with for, while and repeat ................ 79 5.4 Debugging your R functions . 80 5.4.1 The traceback function . 80 5.4.2 The warning and stop functions . 81 5.4.3 Stepping through a function . 82 5.4.4 The browser function . 83 6 Efficient calculations 84 6.1 Vectorized computations . 84 2 6.2 The apply and outer functions . 86 6.2.1 the apply function . 86 6.2.2 the lapply and sapply functions . 87 6.2.3 The tapply function . 89 6.2.4 The by function . 90 6.2.5 The outer function . 91 6.3 Using Compiled code . 92 6.3.1 The .C and .Fortran interfaces . 93 6.3.2 The .Call and .External interfaces . 94 6.4 Some Compiled Code examples . 94 6.4.1 The arsim example . 94 6.4.2 Using #include <R.h> ........................ 96 6.4.3 Evaluating R expressions in C . 98 7 Graphics 103 7.1 Introduction . 103 7.2 More plot functions . 104 7.2.1 The plot function . 105 7.2.2 Distribution plots . 106 7.2.3 Two or more variables . 109 7.2.4 Graphical Devices . 111 7.3 Modifying a graph . 112 7.3.1 Graphical parameters . 112 7.3.2 Some handy low-level functions . 119 7.3.3 Controlling the axes . 121 7.4 Trellis Graphics . 123 7.4.1 Introduction . 123 7.4.2 Multi panel graphs . 125 7.4.3 Trellis panel functions . 128 7.4.4 Conditioning plots . 130 7.5 The ggplot2 package . 131 7.5.1 The qplot function . 131 7.5.2 Facetting . 133 7.5.3 Plots with several layers . 134 8 Statistics 135 8.1 Basic statistical functions . 135 8.1.1 Statistical summaries and tests . 135 8.1.2 Probability distributions and random numbers . 139 8.2 Regression models . 141 8.2.1 Formula objects . 141 8.3 Linear regression models . 142 8.3.1 Formula objects . 142 8.3.2 Modeling functions . 144 3 8.3.3 Multicollinearity . 149 8.3.4 Factor (categorical) variables as regression variables . 152 8.4 Logistic regression . 154 8.4.1 The modeling function glm ...................... 155 8.4.2 Performance measures . 157 8.4.3 Predictive ability of a logistic regression . 158 8.5 Tree models . 160 8.5.1 An example of a tree model . ..