Introduction To R
Introduction To R
A FIRST COURSE IN R
Introduction to R 2
Contents
Goals 4
What You Will Need 4
What is R? 5
Getting Started 6
Installation 6
Layout 7
Console 7
Script Editor & Related Programming Functionality 7
Output 8
Menus 8
Visualization of Information 23
Examples 23
Enhancing Standard Graphs 23
Beyond base R plots 25
Getting Interactive 26
Getting Help 26
Basics 26
A Plan of Attack for Diving In 27
Some Issues 28
Summary 28
Why R? 29
Who is R for? 29
R as a solution 29
Resources 30
General Web Assistance 30
Manuals 30
Graphics 30
Development Sites 30
Commercial Versions 30
Miscellaneous 30
Introduction to R 4
Goals
The goal of this tutorial and associated short course is fairly straight-
forward - I wish to introduce the program well enough for one to un-
derstand some basics and feel comfortable trying things on their own.
In this light the focus will be on some typical applications (colored by
a social science perspective) with simple examples, and furthermore
to identify the ways people can obtain additional assistance on their
own in order to eventually become self-sufficient. It is hoped that those
taking it will obtain a good sense of how R is organized, what it has to
offer above and beyond the package they might already be using, and
have a good idea how to use it once they leave the course.
You will need R and it helps to have something like Rstudio to do your
programming. Some exposure to statistical science is assumed but
only the minimum that would warrant your interest in R in the first
place. Programming experience is useful but no specific experience is
assumed.
5 A First Course
What is R?
R is a dialect of the S language1 (’Gnu-S’) and in that sense it has a 1
S was developed primarily by John
long past but a shorter history. R’s specific development is now pushing Chambers at Bell Labs in the 70s.
This will be the first thing to get used to- R is not used in a manner
like many such as SPSS, Stata, etc. It is a programming environment
within which statistical analysis and visualization is conducted, not
merely a place to run canned routines that come with whatever you
have purchased. This is not intended as a knock on other statistical
software, many of which accomplish their goals quite well. Rather, the
point is that it may require a different mindset in order to get used to it
depending on the kind of exposure you’ve had to other programs.
The initial install of R and accompanying packages provides a fully
functioning statistical environment in which one may conduct any
number of typical and advanced analyses. You could already do most
of what you would find in the other programs, but also have the flex-
ibility to do much more. However, there are over 73933 user con- 3
As of the date of this document, which
tributed packages that provide a vast amount of enhanced functioning. includes CRAN, GitHub, and Bioconduc-
tor.
If you come across a particular analysis, it is unlikely there wouldn’t al-
ready be an R package devoted to it. Unfortunately, there is not much
sense to be made of a list of all those packages just by looking at them,
and I don’t recommend that you install all of them. The CRAN Task
views provides lists of primary packages in an organized fashion by
subject matter, and is a good place to start exploring.
R is a true object-oriented programming language, much like oth-
ers such as C++, Python etc. Objects are manipulated by functions,
creating new objects which may then have more functions that can be
applied to them. Objects can be just about anything: a single value,
variable, datasets, lists of several types of objects etc. The object’s
class (e.g. numeric, factor, data frame, matrix etc.) determines how a
generic function (like summary and plot) will treat the object. It may For example the summary function
be a little confusing at first, and it will take time to get used to, but in can work on single variables, whole
data sets, or specific models. The plot
the end R can be much more efficient than other statistical packages function can be used for e.g. a simple
for many applications. scatterplot, or have specific capabilities
depending on what type of object is
Typically there are often several ways to do the same thing depend- supplied to it.
ing on the objects and functions being used and the same function may
do different things for different classes of objects. One can see below
Introduction to R 6
how to create an object and three different ways to produce the same
variable. This code creates an object called UScapt
that is just the string "D.C.". After that
UScapt = 'D.C.' is demonstrated three different ways in
UScapt which to produce an object myvar∗ that
is the series of numbers from 1 to 3.
## [1] "D.C."
myvar1 = c(1, 2, 3)
myvar2 = 1:3
myvar3 = seq(from=1, to=3, by=1)
myvar1
## [1] 1 2 3
myvar2
## [1] 1 2 3
myvar3
## [1] 1 2 3
Getting Started
Installation
Getting R up and running is very easy. Just head to the website4 and 4
One can just do a web search R and it
once done basking in the 1997 nostalgia of the front page, click the may be the first return.
Layout
I’ll spend a little time here talking about the basic R installation, but
there is typically little need on your own to not work with an IDE
of your choice to make programming easier. The layout for R when
first started shows only the console but in fact you’ll often have three
windows open9 . The console is where everything takes place, the script 9
You have the choice of whether you
file for extended programming, and a graphics device. As I will note want a single document interface (SDI),
which has separate windows to deal
shortly though, an IDE such as Rstudio is a far more efficient means with, or multi-document interface
of using R. Even there though, you will have the basic components of (MDI), where all windows open within
one single window (think of the Stata
console, script editor, and graphics device just as you do in base R. layout).
Console
The console is not too friendly to the R beginner (or anyone for that
matter) and best reserved for examining the output of code that has
been submitted to it.
One can use it as a command
line interface (similar to Stata’s
with single line input), but it’s
far too unforgiving and inflexible
to spend much time there. I still
use it for easy one-liners like the
hist() or summary() functions,
but otherwise do not do your programming there.
Output
Unlike some programs (e.g. SPSS), there is no designated output
file/system with R. One can look at it such that everything created
within the environment is "output". All created objects are able to be
examined, manipulated, exported etc., graphics can be saved as one of
many typical file extensions e.g. jpeg, bmp etc. Results of any analysis
may be stored as an object, which may then be saved within the .Rdata
file mentioned previously, or perhaps as a text file to be imported into
another program (see e.g. sink). There are even ways to have the re-
sults put into a document and updated as the code changes11 . Thus 11
For example, this handout was created
rather than have a separate output file full of tables, graphics etc., without ever leaving Rstudio.
which often get notably large and much of which is disregarded after
a time, one has a single file that provides the ability to call up any- load("myworkspace.Rdata")
thing one has ever done in a session very easily, to go along with other
possibilities for capturing the output from analysis.
Menus
Let’s just get something out of the way now. The R development team
has no interest in providing a menu system of the sort you see with
SPSS, Stata and others. They’ve had plenty of opportunity and if they
wanted to they would have by now. For those that like menus, you’d be
out of luck, except that the power of open source comes to the rescue.
There are a few packages here and there that come with a menu
system specifically designed for the package, but for a general all-
purpose one akin to what you see in those other packages, you might
want to consider R-commander from John Fox (at right). This is a true
menu system, has a notable variety of analyses, and has additional
functionality via Rcmdr-specific add-ons and other customization. I
think many coming to R from other statistical packages will find it
useful for quickly importing data, obtaining descriptive statistics and
plots, and familiarizing oneself with the language, but you’ll know you
are getting a very good feel for R as you rely on it less and less12 . 12
After installing the package you load
Note also that there are proprietary ways to provide a menu system the library just like any other and it will
bring up the gui.
along with R. S-plus is just like the other programs with menus and
spreadsheet, and most of the R language works in that environment.
9 A First Course
SPSS, SAS and others have add-ons that allow one to perform R from
within those programs. In short, if you like menus for at least some
things, you have options.
Example. You import a dataset using the read.csv function, and this returns a value which is a data frame. However, unless
you assign the value to an object, you’d have to repeat the read.csv process everytime you wanted to use the dataset.
Assigning it to an object, one can now supply it to a function, which can then use elements of that data as values to other
arguments.
Coding Practices
Functions
## [1] 3
mymean("thisisnotanumber")
In general, conditionals (if... then), flow control (for, while), and oper-
ators (!=, >=) work much the same as they do in other programming
languages, though some specific functions are available to aid in these
matters, and some that may not be found in other stat packages.
11 A First Course
The above works but is only shown for demonstration purposes, and
mostly to demonstrate that you are typically better served using other
approaches, as when it comes to iterative processes there are usually
more efficient means of coding so in R than using explicit loops. For
example it would have been more easy to simply type:
Note these are not (usually) simply using a particular function that
internally does the same loop we just demonstrated, but performing a
vectorized operation, i.e. operating on whole vectors rather than indi-
vidual parts of a sequence, and do not have to iterate some processes
(e.g. particular function calls) as one does with an explicit loop. Some
functions may even be calling compiled C code which can result in
even greater speed gains. One can start by looking up the apply and
related functions tapply, lapply, sapply etc.15 Using such functions 15
I should probably point out that these
can often make for clearer code and may be faster in many situations. apply functions do not always work
in a way many would think logical.
There is the plyr package available for
an approach that is oftentimes more
Conditional Statements straightforward. The next iteration, dplyr
is in the works.
As an example, the ifelse function compares the elements of two
objects according to some Boolean statement. It can return scalar or
vector values for a true condition, and a different set of values for a
false condition. In the following code, x is compared to y and if less,
one gets cake, and for any other result, death.
Introduction to R 12
x <- c(1, 3, 5)
y <- c(6, 4, 2)
z <- ifelse(x < y, "Cake", "Death")
z
You also have the usual flow control functions available such as the
standard if, while, for etc.
## [,1] [,2]
## [1,] -0.3896 0.1247
## [2,] -0.1636 -1.1703
## [3,] -0.9783 -2.5762
## [4,] -0.1991 0.8355
## [5,] -0.4534 0.7477
## [6,] 1.8228 1.6047
cor(xydata)
## [,1] [,2]
## [1,] 1.0 0.5
## [2,] 0.5 1.0
13 A First Course
# Create data set with factor variables of time, subject and gender 70
60
# In the next line, R will recycle the result to match the
# length of the data.frame upon attachment.
mydata$gender <- factor(rep(0:1, each=4), labels=c('Male', 'Female'))
DV
50
30
library(lattice) 1 2 3 4 1 2 3 4
Importing Data
as \\.
# library(foreign)
Introduction to R 14
To save the data you can typically use ’write’ instead of ’read’ while
specifying a file location and other options (e.g. write.csv(objectname,
filelocation) ) .
Indexing
R19 has a lot of data sets at the ready and more come with almost 19
R manual
every package for demonstration purposes. As an example we’ll use
the state data set which is initially a matrix class object20 . To provide 20
Matrix and data.frame are functions
some more flexibility we will want to convert it to a data frame. The for creating matrices and data frames
as well the name of their respective
state level data regards population, income, illiteracy, life expectancy, object classes. One should check out the
murder rate, high school graduation rate, the number of days in which data.table package for additional data set
functionality.
the temperature goes below freezing, and area21 . Note also there are 21
Type ?state.x77 at the console for
other variable objects as separate vectors which we might also make additional detail.
use of, e.g. state.region.
To begin:
Next we want to examine specific parts of the data such as the first
10 rows or 3rd column, as well as examine its overall structure to de-
termine what kinds of variables we are dealing with. To get at different
parts of typical data frame, type brackets after the data set name and
specify the row number left of the comma and column number to its
right.
head(state2, 10) #first 10 rows
## [1] 2.1 1.5 1.8 1.9 1.1 0.7 1.1 0.9 1.3 2.0 1.9 0.6 0.9 0.7 0.5 0.6 1.6
## [18] 2.8 0.7 0.9 1.1 0.9 0.6 2.4 0.8 0.6 0.6 0.5 0.7 1.1 2.2 1.4 1.8 0.8
## [35] 0.8 1.1 0.6 1.0 1.3 2.3 0.5 1.7 2.2 0.6 0.6 1.4 0.6 1.4 0.7 0.6
## [1] 58.1
## [1] 20 152 15 65 20 166 139 103 11 60 0 126 127 122 140 114 95
## [18] 12 161 101 103 125 160 50 108 155 139 188 174 115 120 82 80 186
## [35] 124 82 44 126 127 65 172 70 35 137 168 85 32 100 149 173
Note that one can also examine and edit the data with the fix func-
tion (or clicking the data object in Rstudio), though if you want to do
this sort of thing a lot I suggest popping over to a statistics package
that takes the spreadsheet approach seriously. However using code for
data manipulation is far more efficient.
# get any States starting with 'I' and ending with 'a' using regular
# expressions
mysubset = state2[grep("^I.*a$", rownames(state2)), ]
# add columns
mydat$grade = y #add y via extract operator
df <- data.frame(id = mydat$id, y)
mydat2 <- merge(mydat, df, by = "id", sort = F) #using merge
mydat3 <- cbind(mydat, x) #using cbind
# add rows
df <- data.frame(id = factor(13:24), group = factor(rep(1:2, e = 3)), grade = sample(y))
17 A First Course
Sometimes you will have to reshape data (e.g. with repeated mea-
surements) from ’long’ with multiple observations per unit to wide,
where each row would represent a case, and vice versa. This is typi-
cally very nuanced depending on the data set, so you’ll probably have
to play with things a bit to get your data just the way you want. Along
with the reshape function one should look into the melt and related
functions from the reshape2 package. The following uses y from the
merge example, but first creates the original data in long format,
makes it wide, then reverts to long form.
Miscellaneous
FA C T O R S
I want to mention specifically how to deal with factors (categori-
cal variables) as it may not be straightforward to the uninitiated. In
general they are similar to what you deal with in other packages, and
while we typically would prefer numeric values with associated labels,
they don’t have to be labeled. The following shows two ways in which
to create a factor representing gender.
At times you may only have the variable as a numeric class, but the
R function you want to use will require a factor. To create a factor on
the fly use the as.factor function on the variable name22 . 22
As an example, the lrm function in
AT TA C H I N G the rms package will realize that the
numeric variable for the outcome for the
Attaching data via the attach function can be useful at times, es- ordinal regression is to be treated as a
pecially to the new user, as it allows one to call variables by name as categorical, but the polr function in the
MASS library would need something like
though they were an object in the current working environment. How- as.factor(y) ~ x1 + x2.
ever this should be used sparingly as one tends to use several versions
of a data set, and attaching a recent dataset will mask variables in the
previous. As it is very common to use multiple data sets to accomplish
one’s goal, in the long run it’s generally just easier to type e.g. my-
data$myvar than attaching the data and calling myvar. Look also at the
with function.
Introduction to R 18
## [1] 70.88
sd(state2$Life.Exp)
## [1] 1.342
summary(state2$Population)
## state.region
## Northeast South North Central West
## 9 16 12 13
library(psych)
describe(state2)
Often we’ll want information across groups. The following all ac-
complish the same thing, but note the describe function is specific to
the psych library. It is worth noting how R is dealing
with region as illustrative of the power
# tapply(state2$Frost, state.region, describe) #not shown of object-oriented programming. The
Region variable and the state2 data
# by(state2$Frost, state.region,describe) #not shown set are the same length, and for the
describeBy(state2$Frost, state.region) purposes of carrying out the function,
R is able to index the data by means of
## group: Northeast the region vector. An error will result
## vars n mean sd median trimmed mad min max range skew kurtosis se if you substitute state.region with
## 1 1 9 132.8 30.89 127 132.8 35.58 82 174 92 -0.1 -1.46 10.3 state.region[-1], which will drop the first
## -------------------------------------------------------- entry. One can instantly create objects
## group: South and use them in conjunction with other
## vars n mean sd median trimmed mad min max range skew kurtosis objects quite seamlessly. They don’t have
## 1 1 16 64.62 31.31 67.5 65.71 33.36 11 103 92 -0.46 -1.22 to be in the data.frame object.
## se
## 1 7.83
## --------------------------------------------------------
## group: North Central
## vars n mean sd median trimmed mad min max range skew kurtosis se
## 1 1 12 138.8 23.89 133 137.2 20.02 108 186 78 0.58 -1 6.9
## --------------------------------------------------------
## group: West
## vars n mean sd median trimmed mad min max range skew kurtosis
## 1 1 13 102.2 68.88 126 103.6 69.68 0 188 188 -0.29 -1.77
## se
## 1 19.1
cor(state2)
25
hist(state2$Illiteracy)
20
barplot(table(state.region), col = c("lightblue", "mistyrose", "papayawhip",
15
Frequency
"lavender"))
10
plot(state2$Illiteracy ~ state2$Population)
stripchart(state2$Illiteracy ~ state.region, data = state2, col = rainbow(4),
5
method = "jitter")
0
0.5 1.0 1.5 2.0 2.5 3.0
state2$Illiteracy
15
The Basic Modeling Approach
10
5
R’s format for analysis of models may have a bit of a different feel than
0
other statistical packages, but it does not require much to get used to.
Northeast South North Central West
2.5
●
●
● ●
●
Typical statistics packages fit a model and output the results in some
2.0
●
state2$Illiteracy
● ●
● ●
●
●
fashion after the syntax is run. R produces ’model objects’ that store
1.5
●
● ● ●
● ●
●
● ● ● ●
● ● ●
●● ● ● ●
●
● ●
makes this very easy and is far more efficient. Some commonly used
model functions include lm24 for linear models, glm for generalized ●
West
●
● ● ● ● ● ●
● ● ●
● ● ●
● ● ●
● ●
● ● ● ●
● ● ● ● ●
● ● ● ● ●
● ●
● ● ● ● ● ●
●
●
here you haven’t seen in other package model summaries, but those 0.5 1.0 1.5
state2$Illiteracy
2.0 2.5
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.76e+01 1.33e+00 50.91 <2e-16 ***
## Income 7.43e-04 2.97e-04 2.51 0.016 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.28 on 48 degrees of freedom
## Multiple R-squared: 0.116,Adjusted R-squared: 0.0974
## F-statistic: 6.28 on 1 and 48 DF, p-value: 0.0156
##
## Call:
## lm(formula = Life.Exp ~ Income + Frost + HS.Grad, data = state2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.0878 -0.6604 -0.0043 0.6791 2.1029
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.59e+01 1.25e+00 52.82 < 2e-16 ***
## Income -7.32e-05 3.33e-04 -0.22 0.82703
## Frost 1.45e-03 3.32e-03 0.44 0.66495
## HS.Grad 9.68e-02 2.65e-02 3.65 0.00067 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.12 on 46 degrees of freedom
## Multiple R-squared: 0.342,Adjusted R-squared: 0.299
## F-statistic: 7.98 on 3 and 46 DF, p-value: 0.000217
##
## Call:
## lm(formula = Life.Exp ~ Income + HS.Grad, data = state2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.0082 -0.6866 -0.0644 0.6186 2.2306
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.59e+01 1.24e+00 53.34 < 2e-16 ***
## Income -7.34e-05 3.30e-04 -0.22 0.82501
## HS.Grad 1.00e-01 2.51e-02 3.99 0.00023 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.11 on 47 degrees of freedom
## Multiple R-squared: 0.34,Adjusted R-squared: 0.312
## F-statistic: 12.1 on 2 and 47 DF, p-value: 5.81e-05
Introduction to R 22
anova(mod1, mod2)
anova(mod3, mod2)
## (Intercept) Income
## 6.758e+01 7.433e-04
coef(mod1)
## (Intercept) Income
## 6.758e+01 7.433e-04
## 2.5 % 97.5 %
## (Intercept) 6.491e+01 70.25058
## Income 1.472e-04 0.00134
As your needs vary you will likely move to packages beyond the
base offerings, and this will require you to get used to the specifics
of each. For example, some may not require a summary function to
produce the basic output, others require matrix rather than formula
input, plots will produce different graphs or perhaps no default plot is
available etc. Depending on the analysis of choice there may be quite a
few options, and as you use a new package you should investigate the
help file thoroughly before getting too carried away. 1
gear ●
0.8
am ● ●
# Example poisson, additive and mixed model. 0.6
drat ● ●
●
lmer(Y ~ X + (1 | group), data = d) qsec 0
wt −0.2
disp
−0.4
cyl
−0.6
hp
−0.8
carb
−1
Visualization of Information
Examples
To start with visualization in R, you might peruse some of the graphs in 10000
the margin to see what’s possible, sometimes remarkably easily even. 8000
Creating graphs in R allows you fine detail control over all aspects 6000
cut
Fair
of the graph and you have the ability to create extremely impressive
Good
count
Very Good
Premium
4000 Ideal
The typical statistical plots are available, but most have enhanced
0
versions in other packages. One can use generic functions such as plot 55 60
depth
65 70
10%
Earlier we created some standard graphs, but they typically aren’t 20%
40%
60%
we’ll do here is start with the usual and tweak as we go along. To 70%
80%
begin with we will take some random data and plot the histogram and 90%
100%
Fair D I1
x <- rnorm(100)
hist(x)
boxplot(x)
Such graphs are as basic as they come and not too helpful except
as an initial pass. For the following we’ll use the Cars93 data from the
MASS package. Load up the MASS library if you haven’t already.
Political Trust Media Trust
hist(Cars93$MPG.highway, col = "lightblue1", main = "Distance per Gallon 1993",
Andorra
xlab = "Highway MPG", breaks = "FD") Argentina
Australia
Brazil
Bulgaria
Burkina Faso
Canada
Chile
Romania
Rwanda
Serbia
Slovenia
S Africa
S Korea
Spain
Sweden
Switzerland
Frequency
Taiwan
10
Thailand
Trinidad and Tobago
Ukraine
UK
Uruguay
USA
Vietnam
Zambia
5
Education
Life Satisfaction
Right Affiliation
Postmaterialism
Interpersonal Trust
Political Interest
Newspapers
TV Radio
Internet
Education
Life Satisfaction
Right Affiliation
Postmaterialism
Interpersonal Trust
Political Interest
Newspapers
TV Radio
Internet
0
20 25 30 35 40 45 50
●●
●
Highway MPG ● ●
●
●●
150
●● ●●
● ●
●
● ●●
●
●● ●
● ●● ●
● ●●
● ●
●
●● ●●●●
● ●● ● ● ● ●
● ● ●●
100
● ● ● ● ●● ●
plot to the overall graph. The par function is used here to set the num-
●
● ●
● ● ● ●●
● ●● ● ● ●●
● ●
●● ●
50
● ●
● ● ● ●● ●
●●
●●●
●
● ● ●●●● ●
● ●
● ● ●●● ● ●
● ●● ● ●
● ● ●●
ber of rows and columns within the actual graphics device so that more
● ●● ● ●● ●
●●●
● ● ●● ● ● ● ● ● ●
● ● ●● ● ●
● ● ●●● ● ●●
●● ● ● ●
● ● ● ● ● ● ●●
●●●
●
0
than one plot at a time may be displayed, at which point it is reset for Censored Metastasis
boxplots
● ●
● ●
● ●
squares
● ●
●
●
● ●
● ● circles
● ●
● ●
● ●
● ●
● ●
● ●
● ● ● ●
Which will produce the following graphic: 5 Left Text ● ● ●● 5 Right Text
−3 −1.5 0 1.5 3
● A ● B ● C ● D ● E
228
223 2381 611
233 162126
218
213 31
208
203 36
41
198
193 46
51
188
183
56
61
178 66
173 71
168 76
1 6 11 16 21 26 163 81
223 228 233 238 158 86
213 218 31 36
203 208 41 46 153 91
193 198 51 56 148 96
188
178
183 61 66 143 101
173 71 138 106
168 76 133 111
163 81 128 116
158 86
153 91 123 121
148 96 118 126
143 101
138 106 113 131
133 111 108 136
128 116
103 141
123 121
118 126 98 146
113 131 93 151
108 136 88 156
103 141
83 161
98 146
93 151 78 166
88 156 73 171
83 161 68 176
78 166 63 181
73 171
58 186
68 176
53 191
63 181
58 186
48 196
53 191 43 201
48 196 38 206
43 201 33 211
38 206 28 216
33
28
211
216
23
Openness
Neuroticism 221
23
18
Openness Neuroticism 221
226
18
13
8
226
231
236
13 231
8 236 3 2
3 2 239 7
239 7 234 12
234 12 229 17
229 17 224 22
224 22 219 27
219 27
214 32
214 32
209 37
209 37
204 42
204 42
199 47
199 47
194 52 194 52
189 57 189 57
184 62 184 62
179 67 179 67
174 72 174 72
169 77 169 77
164 82
164 82
159 87
154 Agreeableness Extraversion 92
159
Agreeableness
87
149 97
154
149
Extraversion 92
97
144 102
139 107 144 102
79 167
64 182
74 172
59 187
54 192 69 177
49 197 64 182
44 202 59 187
39 207 54 192
34 212
49 197
29 217
24 222 44 202
19 227 39 207
14 232 34 212
9 237 29 217
4 5
240 10 24 222
235 15 19 227
230 20 14 232
225 25 9 237
220 30
215 35 4 5
210 40 240 10
205 45
200 50 235 15
195 55 230 20
190 60
70 65
185 225 25
180
175 170
80 75 220 30
165 160
90 85 215 35
145 140 135 130 125 120 115 110 105 100 95
155 150 210 40
205 45
200 50
195 55
190 60
185
180 7065
175
170 8075
165
160 9085
155
150
145 100
105 95
140
135
130 110
115
120
125
25 A First Course
Histogram of Cars93$MPG.highway
50
0.10
45
0.08
40
0.06
Density
35
0.04
30
0.02
25
0.00
20
20 25 30 35 40 45 50 USA non−USA
Cars93$MPG.highway
It is worth noting that some typically used plots come with extras
by default. For example, the scatterplot function in the car library
automatically provides a smoothed loess line and marginal boxplots,
providing very useful information with ease.
library(car)
scatterplot(prestige ~ income | type, data = Prestige)
scatterplot(vocabulary ~ education, jitter = list(x = 1, y = 1), data = Vocab,
col = c("green", "red", "gray80"), pch = 19)
type
● bc ● ● ●
● ● ●
●●●● ●
●
●
●
●
●●
●
●
●
●
●
●
●●
●●●
●●
●
●
●
●● ●●
●
●●
●●
● ●●●
●
●
●
● ●
●●
●●
●
●●
●
●●
●
●
●●
● ●
●
● ●●●●●
●
●●
● ●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●●
●
●●
●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●●
●
●
●
●
●●●
●
●
●
● ●●●
●●
● ●●
●● ●
●
●
●
●
●
●
●●●●
●●
●
●
●
●
●
●
●
10
● ●● ●
●● ● ●●●●● ●
● ● ● ●● ●
●●●
●●●● ●
●● ●
● ●
●●
●● ●● ●
●●
●●●
● ●● ●
●
● ●●
●
prof ● ● ● ●
● ● ●●
●●
●
●●
●
● ●
●
●● ● ●●
●
● ●
●
●● ●●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●●
●●
●●●
●
●●
●●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●●
●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●
●●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●●
●
●
●●
●
●
●
●
●
●●●
●
●
●●
●
●●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●●
●
●
●●
●●
●●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
wc
● ●● ●●● ●
●● ●●● ●
● ●
●●
●
●●
●
●●
●●
●●
●
●
● ●● ●
●●●
●●● ●
●●
●
●●
●●
●
●●
● ● ●●
● ●● ●
●●
●
●●
●●
●
●
●●
●●
●
●
● ● ● ●
●●●●
●●
●
● ●●●
●●●●●
●● ●●●●
●●● ●●
● ●●●
● ●●
●●● ●● ●
●● ● ●
●
●
●
●
●
●●
●
●
●●
●
●●
●
●●
●
●
●
●●
●
●
● ●
●
●●
●●●●
●●● ●
●
●●
●
●
●●
●
●
●
●
●
●
●●
●
●●
●
●
● ●●
●
●●●●
●
●●● ●
●
●
●
●●●
●
●●
●
●
●●
●
●●
●
●
● ●
●
●
●●●
●●●
●●
●
● ●●
●●
●
●●
● ●
●●●
● ●
●●●● ●●●
● ●
●
●● ●
●
●●
● ●
●● ●● ● ●●●● ●● ●
●●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●●
●
● ●
●
●
●
●
●
●●
●●
●●
●●●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●●
● ●
●● ●
●
●●
●
●●
●
●
●
●
●●
●
●●
●
●●
●
● ●●
● ●
●
●
●●
●
●
●
●
●
●●
● ●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
● ●● ●●
●
●●
●
●
● ●
● ●
●●
●●
●●●
●●● ●●●●●
● ●●
● ● ● ●
●
●●●●●
●●
●
● ●●● ● ● ● ●●●
●●● ● ●
● ●● ●
●●●
●
● ●●● ●
●●●
●●●● ●
●
●● ●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
● ●
●
●●
●
●●●
● ●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●●
●
●
●●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●●
●
●
●
●
●
●●
●
●● ●
●
●
●
●●
●
●
●●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●●
●●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●●
●
●
●
● ● ●● ●●●
●●●
● ●
● ●
●● ●
●●
●
●●●
●
●●
●
●
●●
● ●●●●
● ●
● ●
●●
●
●●
●●●●
● ●
●●●
●●
●●● ●
●
●
●●
●
●●
●
●●
●
●
●●
●●
●
●
● ● ●
●●
●●
●●
● ●
●
●●●
●●●●
● ●
●
●●● ● ● ●
●●
●●
●●
●
●
●●
●●
●●● ●● ● ●
● ●●●● ●● ●●●●
●
●●
●● ●●
● ●
● ● ●●●●●●● ●● ● ●●● ● ●
●
● ●● ● ●● ● ●● ●
●●
● ●
●
●●
●●
● ●●●
●●●●
● ●
●●●
●●
●●
●
●● ● ●
●●
●●● ●
●●●
●
●●
●●
● ● ● ●●
●●● ● ● ●
● ● ●●●
●
● ● ●
●●● ●●●●●
●
●
●●
●
●●●●●
● ●●
●●●
●●●
●●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●● ●●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●● ●
●
●
●
●
●
●●
●
●
●
●
●
●
●●
●
●
●●
●
●
●
●●
●
● ●●
●●
●●
●
●
●
●
●●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●●
●
●
●●
●
●
●
●
●
●● ●
●●
●
●
●●
●
●●●
●
●● ●
●●
●
●
●
●●
●●●
●●
●
●
● ●●
●●●●●●
●
●
●
●
●
●●●●●
●
●
●
● ● ●● ● ●● ●●
●●
●●●●
● ●
●●●●●● ●
●
●
●●
●
●●
●●
●
●
●
●●
●
●●
●
●
● ●
●
●●
●●
●●
●
●
●●●
●● ●
●
●●
●
●●
●
●●
●
●●●
●
●● ●
●●
●
●●●
●●
● ●
●
●
●
●●
●●
●●
●
●●
●
●●
●
●
●
● ●
●
● ●●●
●● ●
●●●●● ●
● ●●●
●
●●
●● ●●
● ●
●● ● ●●●● ● ● ●●
● ● ●
● ● ● ●● ●● ●● ●
●● ● ● ●●●
●
80
● ●● ● ● ●● ● ●
●
● ● ● ● ●
● ●● ●
8
●● ● ●
●●
●● ●
●
●●
●●
●●●● ●
● ● ●
●●● ● ●●●
●●
●
● ●●●● ● ●
●●
●●
●
●●●
●● ● ●●
● ●
●●●
●● ● ●
●● ● ●
●
● ●● ●● ●●● ●
● ●●
●
●●
●● ● ●
●●
● ●
●
●● ●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●
●
●
●
●
●
●●●
●
●
●
●
●
●
●●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●●
●
● ●
●
●
●
●
●
●●
●
●●
●
●
●●
●
●
●●● ●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●●
●
●
●
●
● ●
●
●
●
●
●
●
●●
●
●
●
●
●
●●●
●●
●●
●● ●
●
●●●
●
●●●
● ●
●
●●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●●● ● ● ● ●● ● ● ●
● ●
●
● ● ● ● ● ●● ● ● ●
● ●
●●
●
●● ● ● ●●●●●
● ●
●
● ●●●
● ●●●● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●● ●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●
●●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●●
●●●
●
●
● ●
●●●
●●
●
●●
●
●● ●●
●
●●●
●
● ●●
●
●
● ● ●● ●●
● ●●●
● ●
●
●●●● ●
●
●●●
● ●●
● ●● ●
●
●
●
●●
●
●
●●
●
●
●
●●
●
●
●
●●
●
●
●
●●
●
●
●
● ●
●
●●
●●
●
●●
●●
●
●
●●
●
● ●
●
●
●●
●
●●
●
●●
●●
●
●●
●
● ●
●●●
●
●●
●
●●
●●
●
● ●
●
●●
●●
●
●●
●
●
●
●●
●
●
●●
●
● ●
●●
● ●●
●● ●
●●●●
● ●● ●● ● ●● ●●
● ● ● ● ●● ●
●●●●● ●●
●●●
●● ●
●
●●●
●●
●●● ●
●● ●●
●● ●
●
●
●
●
●●
●
●●
●
●
●
●●
●
●●
●
●●
●
● ●
●●
●
●●
●●●
●●
●● ●
●
●
●
●
●●
●●
●
●
●●
●
●●
●
●
●●
●
●
● ●
●
●●
●
●●
● ●●●●
●
● ●
●
●●
●
●●
●
●●●
●
●
●●
●
●●
●
●
●●
●
●
● ●
● ●
●●●●
●● ● ●
●
●●●●
●
● ●●● ● ●● ●●
● ●●
● ● ●●● ●●
●●
●●
●
●
●
●●
● ● ●●
●●
●●
● ●●
●
●
●●
●
●
●●●
●
●●●
● ●●
●●
●●
●
●●
●
●●●
● ●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●● ●
●
●●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
● ●
●
●
●
●
●●
●
●
●●
●
●●
●
●
●●
●
●
●
● ●●
●
●
●●●
●
●●●
●
●
●● ●
●
●
●
●●
●
●
●●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
● ●
●●●
●
●●
●
●●●
●
●
● ●
●
●●
●●
●
●●
●●
●
●●
●
● ●●●●●●●● ● ●
●●●●●
●●
●●
●
● ● ● ●●●● ●●
●●●
● ●
●●
●● ●●●●●● ●
●
●
●●●
●
● ●
●
●●●
● ●
●
●
●●
●
●●
●●
●
●●
●● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●●
●
●
●● ●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●
●
●
●
●
●● ●
●
●●
●
●●
●●
●
●
●●
●
●●
● ●
●
●
●
●
●●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●● ●
●●●
●
●
●●
●
●
●
●●
●
●
●●
●●
● ●
●●
●
●●
●
●●
●
●
●
●
●
● ●●
● ●● ●● ●
●
● ●●● ●●●
●●●
●
●
●●● ●
●●
●
● ● ● ●
●●
●
●
●●●
●
●●●●
●●
●
●
●●
●
●
●●
●
●●
●
● ●
●●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●●
●
●●
●
●
●
●
●
●
●
●
● ●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●●
●●
●
●●
●●
●
● ●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●● ●
●
●●●
●
●
●
●
●●●
●
●
●
●
●● ●
●
●
● ●
●●
●
●
●●
●
●
●●
● ●● ●●
● ●
● ●●
●●●●●●
● ●●
● ●●●● ●
●●
●
●
●●●●●●
● ● ●●●● ●
●●●
●
●
●●
●
●●●
● ●
●
●●
●
●
●
●
●●
●●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●●
●
●
●
● ●
●
●●
●
●
●
●●
●
●
●●
●●
●
●●
●
●
●
●●
●
●
●
●
●●
●
●
● ●
●
●
●●
●●
●
●
●●
●
●
● ●
●●
●●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
● ●●●●
●
●
●
●●
●●
● ●
●
●●
●●
●
●●
●●
●●
●
● ●
●●● ●●
●● ●●
● ●
● ●●●
70
●
● ● ●●●● ●
●●
●
●●
●
●●
●●
●●
●
●● ●
●●●●●●● ●
●●
●●
●●●
●●●
● ●
●
●●●
●●
●
●
●●
●
●●● ●
●
●
●
●●
●
●
●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●
●
●
●●
●
●
●
●
● ●
●
●
●●
●
●
●●
●●
●
●●
●●
● ●
●●
●●
●●
●●
●
●●
●● ●
●●●
●
● ●
●
●
●●
●
●●
●
●
● ●
●
●
●●
●
●●
●
●
●●
●
●
●●
●
●
●●
●
●
●
● ●
● ●
●
●●
●●
● ●
●●●●●
●●
● ●●●● ●
●
●●●●●● ●●● ● ●●
●
● ●
●
●●
●●
●●
● ●●
●
●●
●● ●●
● ●
●
●●
●●
●
●
●●
●
●●
●●
●●
● ●
●
●
●●
●
●●
●●
●●
●
●●
●●
● ●
●
●
●
●●
●
●
●
●
●●
●
●
●
●
●●
●
●
●●
●
●
●
●●
●
●
●
●
●
●● ●
●●
●
●
●
●●
●●
●
●
●
●
●●
●
●
●
●●
●
●
●● ●
●
●
●
●
●●
●
●●
●
●
●●
●
●
●●
●
●
●
●●
● ●
●●
●
●●●●
●
●●
●● ●
●
●●
●
●●
●
●
●●
●
●
●●
●
●●
●● ●
●●
●●● ●● ●●
●●●
●
●●
●●
●
●
●● ●
●
● ● ● ●●
●
●● ● ● ●●
●
●●●●● ●●
●
●●
●
●●
●●
●
●●
●
●
●
●
●
●
● ●
●
●●
●
●●
●●
●●●
● ●
●
●
●
●●
●
●●
●
●●●
●●
●
●
● ●
●
●●
●
●●●
●
●●
●
●
●
●●●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
● ●
●
●
●
●
●●
●●
●
●
●●
●
●
●●
●
●
●
●
●
●
●
●
● ●
●●
●
●
●
●●
●
●
●●
●
●
●
●
●
●
●●
●
●
●
●
●
●
● ●
●●
●
●●
●
●
●
●
●
●
●●
●
●
●●
● ●
●
●
●●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
● ●
●●
● ●●
●
●●
●● ●●
●●●●
●
● ●
●●
●●● ●● ●
● ●●
●
6
●●●● ●
● ● ●
● ●
● ●● ● ●●●● ● ●●●
●● ● ●
●
●●
●
●●●
●
●●●
●●
● ●
●●●●●●
● ●●●
●●
●●
● ● ● ●
● ● ●
● ●● ● ●
●●
●●● ● ● ●● ●
● ●● ●● ●
● ●
●
●
● ●
●●
●●
●
●
● ●
●●
●
●
●●●●●
●
● ●
●
●●
●
●●
●●
●
●●●● ●
●
●
●●
●
●
●●
●●
●●
●
●●
●●
● ●
●
●
●
●
●●
●
●
●●
●
●●
●
●
●
●
●●
●
●
●●
●●
●
●
●●
● ●
●
●
●●
●
●●
●
●●
●●
●
●
●
●●
●
●
● ●
●●
●
●
●
●
●●
●●
●
●●
●
●
●
●●
●
●
● ●
●
●●
●
●●
●●
●●
●●
●● ●
●
●
●
●●
●
●
●
●
●●
●●
●
●
●●
●
●●
●
●● ●●●●●
●● ●● ● ● ●● ● ●● ●
●● ● ● ●●●
●● ●
●
●●●●
●● ●
●●
●●
●
●
●
●●
●●
●
●
●
●
●
●
●●
●
● ●
●
●●●
●
●●
●
●
●
●
●
●
●
●●
●
●
●●
●
●
●
●
●
●
●●
●
●●●
●
●
●
●
●
●
●●
● ●
●
●
●●
●
●●
●
●
●
●●
●
●
●
●●
●●
●●
● ●
●
●
●
●
●●
●
●
●
●
●
●●
●
●
●
●●
●●
●
●
●
●●
●
●
●
●
●
●
●●
●
●
●
●
●● ●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●●
●
●
●
●●
●
●
●
●
● ●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●●
●
●
●
●
●●
●● ●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●●
●
●
●
● ● ●●
●●
●●
● ●●●
●●
●
●
●●●
●
● ●●● ● ● ● ●
● ● ● ● ●●
●●
●
●
●● ●
●
●●●
●
●●
●●●
●
●
●● ●
●●
●●●
●
●●
●● ●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●●
●●
●●
●
●
● ●
●
●●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●
●● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●●
●
●
●●
●
●
●
●
●
●
● ●
●●
●●●
●
●●
●
●●
●
●
●
● ●●
● ● ●
●●● ● ●
● ●
●● ●●● ●●
vocabulary
60
●● ● ● ● ● ● ●
● ●
●
●●
●● ●
●● ●●
●●●
●●
● ● ●●●
● ● ●●● ●● ●
●●●● ●● ●●
●
●● ● ● ●● ●
● ●●
●●●
● ●
●●
●
●
●●
●
●
●
●●●
●●
●● ●●
●
● ●
●
●
●
●
●
●● ●
●
●●
●
●●
●
●●
●
●
●
●●●
●●
● ●
●
●
●
●●
●
●●
●
●●
●
●●
●●
●
●
● ●
●
●
●
●
●
●
●
●●
●
●
●
●
●●
●
●
●
●
●
●●
●
●
●
●
●●
●
●
●
●
●
● ●
●●
●
●●
●
●●
●
●●
●
●
●
●
● ●
●
●
●
●
●
●
●
●●
●
●
●
●●
●●
●
●●
●
●● ●
●
●●
●
●
●●
●
●
●
●●
●
●
●
●●●
●
● ●
●
●●
●
●
●
●●
●●
●●
●
●
●● ●
●●
● ●●
●●●
● ● ●
● ● ● ●●
● ● ●●
●
●
● ●●
●●
● ● ●●
●●●●●●
● ●
●
●
●
●
●
●●
●
●●
●●
●
●●
●
●
●● ●
●●
●
●●
●
●
●
●●
●
●●●
●● ●
●
●
●●
●
●
●
●●
●●
●
●
●●
●
●●
●
●
● ●
●●
●
●●
●
●
●
●
●
●●
●
●
●●
●
●
●● ●
●
●
●
●
●
●
●●
●
●
●
●
●●
●
●
●
●
●
●●
●
●
●●
●
●
●
●
●
●●
●
●
●
●
●● ●
●
●
●●
●
●●
●
●
●●
●
●
●●
●
●
●
●●
●
●●
● ●
●
●
●
●●
●
●
●
●
●●
●●
●
●
●●
●
●●
●
●
●● ●
●
●●
●●●
●●
●
●●
● ●
●●
●
●
●
●
●
●●
●
●●
●●
●
● ●
●
●●●
● ●● ●● ●● ●
● ● ● ●●●●
● ●● ●●
● ●
● ●
● ●
●
●
●
●
●●
●
●●●
●
●●
●
●
●
●●
●● ●
●
●
●
●
●
●
●
●
●
●●●
●●●
●
● ●
●
●
●
●●
●
●●
●
●●
●
●
●
●●
●
●
●●
●
●
● ●
●
●
●
●●
●
●
●
●
●●
●
●
●●
●●
●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
● ●
●
●
●
●
●
●●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●●
● ●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●●
●
●
●
● ●●
●●
●
●
●●
●
●●●
●
●● ●
●
●●
●
●●
●
●●
●
●●
●
●
●
●
●
●
●
●●
●
●
● ●●
● ●● ● ●● ●● ● ●●
●●
● ● ● ●●
●● ●
●●●
● ●
●● ●
●
●
●●
●
●
●●
●
●
●
●●●
●●
●
●
●
●
●●
● ●
●●
●
●
●●●
●●
●● ●● ●
●
●●
●
●●
● ●
●
●
●
●●
●
●●
●
●●
●
●
●
●●
●
●
●●
●
● ●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●●
●
●
●●
●
●●
●
●
●
●
●● ●
●
●
●●
●
●●
●●
●
●●
●
●●
●
●
●
● ●
●
●●
●
●●
●
●●
●
●●
●
●
●
●●
● ●
●●●
●
●●
●
●
●●
● ●
●
●●
●●
●●
●
●●
●●
●
●
●
●
●●
●
●● ● ● ● ●●● ●●
●● ● ●●● ● ●
● ● ●●
●●●
●
●
●●●●
●
●●
●
●
●●
●
●
●
●
●
●
●
●●
●
●●
●●
●●
●
●
●
●● ●
●
●
●
●●
●●
●
●●
●●
●●
●
●●
●
●
●●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●●
●●
●
●●
●
●
●●
● ●
●●
●
●
●●
●
●●
●●
●
●
●
●●
●
●● ●
●
●●●●● ●●●
●
●●●●● ●●● ● ● ●
● ●● ●
● ●● ●●
●●
● ●●
●● ●
●●
●● ●
● ●
●●
●●
●●●●
● ●
●●
●●
●
●
●●
●
●●
●●
● ●
●
●●
●
●
●●
●
●●
●
●●
●
●
●●
●
● ●●
●●●
●
●●
●
●●
●● ●
● ●
●
●●
●●
●●
●
●
● ●
●●
●●●●
● ●
●●
●●
●●
●●
●●● ● ● ●
prestige
●● ● ● ● ● ●●● ●●
● ●
●● ●● ●
●
●●
●
●
●●●●
●
●●
●
●● ●
●●
●
●●
●
●●
●●
●
●●
● ●
●
●
●● ●
●●
●
●
●● ●
●●
●
●●
●
●
●
●
●●
●
●●
●
●
●●
●
● ●
●
●
●
●
●●
●
●
●●
●
●
●
●●
●●
●
●
●
●●
●
●
●
●●
●
●
●● ●
●
●
● ●
●
●●●
●●
●●
●
●● ●
●
●
●●
● ●
●
●●
●●
●●
●●
●
● ●●
●●
●●● ● ●●
●●●
●●
●
●●
● ● ● ●●● ●●●●
●
●
● ● ● ●●
●
● ●●
●
●● ●
●●
●
●
●
●●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
● ●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●●
●●●
●
●
●●
●
● ●
●●
●
●●● ●
●●
●●●●
●
●
●
●●
●● ●●● ● ● ●
50
●● ●
● ●
●● ● ●
● ●● ●
●
●● ● ● ●
● ●●
●●●●
● ●● ●
●● ● ●● ●●● ● ●●
● ●● ● ●● ● ● ● ●● ● ● ● ●
4
● ●● ●●●● ●
●
●●● ● ●●
●
●●
●
●●
●●
●
●
●
●● ●●●●
●
●●●
● ● ●
●
●●
●●
●
●●
●
●●
●●
●
● ●
●
●●
●
●●
●
●
●●
●●
●
●
●● ●
●
●
●
●
●●
●
●
●
●
●●
●
●
●●
●
●
●●
●
●
●
●●
●
● ●●
●●
●
●●●
●
●●
● ●
●
●
●●
● ●
●
●
●●
●
●
●
●●
●● ●●
● ● ●
●●
●
●●
● ●
●●●
● ●●●●
●
●
● ●●●●● ●
● ● ● ● ●●● ● ● ● ● ● ●●●● ● ●
● ● ●●● ●
●●
● ●● ● ● ●
● ●● ● ● ● ● ●
● ●● ●
●● ● ●●
●● ●●
●
●
●
●
●●
●
●
●
●
●●●
●●
●●
●●
●
●●
●
●
●
●●
●
●●
●
●●
●
●●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
● ●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●●
●
●● ●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●●
●
●
●
● ●●●
●
●●
●
●● ●
●
●
●
●
●
●
●
●
● ●
●●
●
●●
● ●
●● ●
●
● ●
●
● ●
●● ●● ● ● ●
●
● ● ●
●
●●●●
●
● ●●● ●
●
●●
●●
●
●
●●
●
●●●
●
●
●● ●
●
●
● ●
●●●
● ●●
●●
●
●
● ●
●
●
●●
●
●
●●
●●
●●
●
●
●●
●
●
● ●
●●
●●
●●
●
●
●●
●●
●
●
● ●
●
●●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
● ●
●
●●●●
●●
● ●
●●
●
●● ●
●
●●●
●
● ●
●
●●
●
●●
●● ●●●● ●
●● ●
●●
●
●●● ● ●● ● ●
●
●●●
● ●●● ● ● ●
●●●● ●
●●●
●●● ●
●●●
●● ●●●
● ●
●●
● ●
●
●●
●● ●
●●
●●
●●●
● ● ●
● ●●
●●●
●
● ●
●●
●
●
●●
●
●●
●●● ●
●
●
●●
●
●
●●
●
●●
●
●●●
●
● ●●●●● ●
●●●● ● ● ●●●
● ● ● ● ● ●
●●
● ● ●● ●● ● ● ● ●● ● ●
●●
●●
● ● ●● ●● ●
● ●
●●●●● ●● ●●●
● ●●
●●●
●●
●●
● ●●
●●
●● ●●
●●
●
●
●
●
●
●●●
●
●
●
●●
●
●
●●
●●
●●●
●
●●
●
●
●
●●
●
●●
●●
●
● ●
●●
● ●
●●●
●
●
●
●
●
●
●
● ●
● ●
●
●
●
●
●
●
●●
●
●
●●
●
●
●●
●
●
●
●
●
●
● ●
● ●
●●
●
●●
●
● ●
● ●
●
●
●●
●●
● ●
●●
● ●
●●●● ● ●●● ● ● ● ●
● ●● ●●●●
● ●●●●● ● ●● ●
● ●● ●
●●●
●
●●
●●●
●●
●
●
●
●
●●
●
●
●●
●
●
●
●
●
●●
●
●● ●
●
●
●●
●
●
●●
●
●●
●
●
●
●
●
●
●
●●
● ●
●●
●
●
●●●●
●
●
●
●● ●
●●
●●
●
●
●
●
●
●
●
●
●
●●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●●
●
●
●
●
● ●
●●
●●
●
●
●
●●
●●●
● ●●
● ●
●
●
●
●
●●●
● ● ●●
●●
●
●
●●●
● ●●
●●● ●● ● ●●● ● ●●
● ● ● ● ●●● ●●●●
● ● ●
●
●●●●●
●●
● ●
●●●
●
●
●●
●
●●●
● ●
●
●
●●
●
●
●
●
●●
●●
●
●
●
●●
●●
● ●
●
●
●
●●
●
●
●
●●
●
●
●
●
●●● ●
●●
●
●●
●●
●
●
●
●●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●●●
●
●●
●●
●●
● ●
●●●
●●●●
● ●●●● ●
●
●●
●
● ● ●●
●
● ●●
●
●
●
● ● ●
●●●●● ●●
● ●● ●●
●●●● ● ●●
●●●
●
●
●● ●●
●
●●
●
●
●●
●
●●●
●●
●
●●
●
●
●●
● ●
●●●●
●●
●
●
●
●
●
●
●
●
●●●●
●
●
●
●
● ●
●●
●●
● ●
●
●●
●
●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●●●
●
●
●
●●●
●●
● ●
●
●
●
●●
●
●
●
●●
●
●
●
●● ● ●●
●
●
● ●
●●●● ● ●●
●● ●
40
●
● ●
●
● ● ●●●● ●
●●●
●●●
●● ●●● ●● ●
●●●
●●
●●
●
●●
● ●
●●●●●
●●●
● ●
● ●●●
●●
●● ● ●● ●
● ●●●●●●● ●
●
●
●
●●
●●
●
●●
●
●●
●
●●
●● ●●●
●● ●●●
● ● ●●●● ●
●● ● ●●● ●
● ●●● ●● ●●
●●
●●● ●
●●● ● ●
● ●●●
●● ●●●●
●●●●
● ●
●●
●
●
● ●
●
●●● ● ●
●●
●● ●●
●
● ●● ●● ●● ●●● ● ●●● ●●
●●
● ●
●●
●●
●
●●●
●●● ●●●
●
●
●
●●
●
●● ●
●●
●●●
●
●
●●
●
● ●
● ● ●
●
●
●
●
●
●●
●
●●
●
●●
●
●●
●
●●
●
●● ●●
●●
●●
●
●
●● ●● ●● ●● ● ● ●
2
●● ● ● ● ●
●● ●● ●● ●●
●● ●●●●● ● ● ● ●
●●●
● ●
●
●●
●● ●●●●
● ● ●
●● ●
●●
● ●
●
●●
● ●
●●
●
● ●
●
●●
●●
●
●
●●
●
●
●●
●
●●
● ● ● ●● ● ● ● ●●● ●●
●● ● ● ●
● ●●●●
●
●
●
●
●
●●
●●
●●
● ●
●
●
●●●●
●●
●
●
●
●●
●●
●●
●●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
● ●
●●
●
●●
●●
●
●●
●●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●● ●
● ●●●
●●
●
●●
●●●●
●● ●
●● ●●●●●
●
●● ●
●
●
●
●
30
● ● ●● ●● ●● ●● ●●●
● ●● ●
●
● ●●●●●●
● ● ● ● ●●●●● ● ●
●●
●● ●●● ● ●
● ● ● ● ● ●● ●●●●● ● ●
● ●●●
●●
● ●●
● ●
●
●
●●●●
● ● ●●
●●●●
●
●
●
●
●●
●●
●
●
●●● ●●●● ● ● ●● ●● ●
●● ● ●
●● ●
●
●●●
●●●
● ●●
● ● ●●● ●
● ●●● ●
●● ●● ● ●
● ● ● ●● ● ●●●● ● ●● ●
●
●
● ●
●●● ●
●●●
●●●●● ●●●● ● ●● ●●● ●●●
●●●●
● ●
●●
● ●●●
● ●● ●●
●
● ●● ●● ●●●●● ● ●●
● ●● ●● ● ●● ●● ● ● ●●●
●●
● ●● ● ● ●● ●
● ●
● ●●
●●
● ● ●● ● ●●●●
●
●
● ● ●● ●
●
●● ●
●●●●
● ●●●● ●
●
●
●●●
●
●
●●
●
●●
●●
●
● ● ● ●
●●● ● ●
● ● ● ● ● ● ● ● ●● ● ●●●● ●●●●●
● ● ●●
●●
●
●● ● ●
●
● ●
● ●
● ● ●● ● ●
●● ●● ●● ● ●● ●● ●●
● ● ●●● ● ● ●
●●● ● ●● ● ●● ● ● ●●●●●● ●
●
●●
● ● ●●
●
● ●
● ●●●●
●
●● ●●●
●● ●●● ●●●●●
● ●
● ● ●
20
● ●●● ●●● ●● ●
●●
0
● ● ●● ● ● ●
● ●
● ●●● ●
●●
●
● ● ● ●●
● ● ●● ●● ●●
● ●●● ●
● ● ● ●● ● ●●
● ● ●● ●●●● ●● ●● ●●
●● ● ●●
●
●●
●
●
●●
● ● ●
●●
● ●● ● ● ●
● ● ● ●● ● ● ●●●
0 5 10 15 20
5000 10000 15000 20000 25000 education
income ● ● ● ● ● ● ● ● ●
Getting Interactive
Getting Help
Basics
Now that you have some idea of what R can do for you and are ready
to try it on your own, it is imperative that you find out how you can
continue to work with it. It likely won’t take but a minute or two be-
fore you start hitting snags, and truth be told, you’ll probably spend a
lot of time looking up how to do things even after you get very familiar
with it. However that’s not a sign of R’s difficulty so much as its capa-
bility. As you get used to the sorts of things you already are familiar
with, you’ll come across more and more alternatives and enhance-
ments, and you will actually be getting a lot more done in the same
amount of time in the end.
As a start in becoming self-sufficient, get used to using the help files.
As mentioned previously, typing ? or ?? followed by the function name,
e.g. ?hist ??scatterplot, will bring up the help file in the former case, or
do a search for whatever term you’ve inserted28 . Typing help.start() at 28
Same as help and findit in Stata
the console will bring up manuals etc. Note that while there are help
files for every function, not all have the same amount of information.
Some functions/packages come with vignettes, links to reference arti-
cles etc. Others give only the bare minimum of information needed to
use the function. Some examples are exactly what you need, some will
spend 3/4 of many lines of code simply constructing the data set to be
used for the example that’s actually of interest, others might be a single
line or two that do little to illuminate the possibilities. Such is the way
of things when you have thousands of people contributing their efforts,
27 A First Course
but do note that all help files contain the same type of information as
they are required to.
A very useful thing to help you find the information you need is
via the Rseek search engine browser addon; example results are pic-
tured at right. It will return a specialized Google search (just fyi, there
happen to be lots of webpages with the letter R in them, and given
Google’s search tendencies of late any help is useful), introductions,
associated Task Views, R-help list results etc. You might spend some
time getting acquainted with the R-help list or even subscribing to it,
but with this search engine it’s not necessary29 . 29
Furthermore, the help list has gotten
Increasingly of late, many of those who are well-known on the R- out of hand for every day perusal, often
going over 100 messages for a single
help lists, along with many other folks from all walks of life, are now daily digest with most of them quoting
seen more on StackExchange/Cross-Validated. You’ll also find fewer previous messages which were quoting
previous messages and so on. Basically
folks complaining about reading the posting guide (or lack thereof). the thing as a whole is a mess to look
at, so you’re better off searching it via
Rseek.
A Plan of Attack for Diving In
The following are some guidelines that should help someone make for
a quicker transition toward using R regularly.
Use a script editor with syntax highlighting etc., rather than the
console30 . 30
At this point there’s no real reason not
to use Rstudio.
Get used to installing packages and importing data into R, and use
Rstudio menus to help with this initially if need be.
Start with ?functionname to see how to implement any particu-
lar function first. This will get you more acquainted with the help
files and in the habit of trying the examples. Saves time in the long
run.31 31
Type example(functionname) at the
console for any function you want to see
Take simple functions or commands you use in other packages such in action.
as for mean, sd, summary, and simple graphs and reproduce them
in R with no frills. Spend some time at the Quick-R website to help
with this.
Once comfortable with some basics, do a bare bones simple mod-
eling approach that includes: importing data you are familiar with,
describing it, creating a plot, running an analysis, interpreting re-
sults. I suggest a simple analysis and data set.
Redo the things you’ve learned thus far, adding options (e.g. extra
arguments, more complexity) along the way.
There are many users and places for support on the web. Use Rseek
and regular web searching to find the names of useful functions,
peruse the Resources section at the end of this document. There is a
ton of information on R freely available.
Introduction to R 28
Alternatively, you can find some things R does that you like and/or
does better than your main statistical package, and simply pull out R
as you need it. That is how I initially did things myself, though I wish
I hadn’t. Some R is better than none to be sure, but it didn’t make
it any easier to work within the environment in general, nor does it
give a good sense of the capabilities of the program, as you may just
come to think that it’s merely a more complicated way of doing things
slightly better, instead of a much better way to do statistical analysis in
general. But again, for some that may be the most practical approach
to start.
Some Issues
Summary
Why R?
Who is R for?
R as a solution
R can do what you want, and you are not forced into limitations by de-
sign in the way you are with other packages which have no flexibility.
The question is not if R can do this or that analysis, but instead, how.
Introduction to R 30
Resources
These are resources to keep you on your R journey now that you know
a little bit about it.
Manuals
List at the R website These are accessible via the help menu.
An Introduction to R The main R manual.
Simple R Dated but still useful to someone starting out.
Reference Card Print out and keep handy when starting out with R.
Graphics
ggplot2 library website Hadley Wickham’s helpful website and package.
Paul Murrel’s website A core R developer.
Development Sites
Github Many R developers host code there.
R-forge development site Pacakges in development and unreleased packages.
Rforge development site How fun! A different ’r’ ’forge’ site.
Commercial Versions
Revolution Analytics 33 33
I guess it says something when the
S-Plus creator of SPSS jumps ship for R. I don’t
know what it says though.
Miscellaneous
Rstudio Don’t do R without it.
R blogs For those that like their R with too many links and stories about cats.
Crantastic Note new packages and updates.