Learn To Use: Your Hands-On Guide
Learn To Use: Your Hands-On Guide
Learn To Use: Your Hands-On Guide
by Sharon Machlis
edited by Johanna Ambrosio
R: a beginner’s guide
COMPUTERWORLD.COM
Introduction
R is hot. Whether measured by more than as easy to run multiple data sets through
6,100 add-on packages, the 41,000+ mem- spreadsheet formulas to check results as it
bers of LinkedIn’s R group or the 170+ R is to put several data sets through a script,
Meetup groups currently in existence, there he explains.
can be little doubt that interest in the R sta-
Indeed, the mantra of “Make sure your
tistics language, especially for data analysis,
work is reproducible!” is a common theme
is soaring.
among R enthusiasts.
Why R? It’s free, open source, powerful and
highly extensible. “You have a lot of pre-
packaged stuff that’s already available, so
Who uses R?
you’re standing on the shoulders of giants,” Relatively high-profile users of R include:
Google’s chief economist told The New York
Facebook: Used by some within the com-
Times back in 2009.
pany for tasks such as analyzing user
Because it’s a programmable environment behavior.
that uses command-line scripting, you can
Google: There are more than 500 R users
store a series of complex data-analysis
at Google, according to David Smith at
steps in R. That lets you re-use your analy-
Revolution Analytics, doing tasks such as
sis work on similar data more easily than if
making online advertising more effective.
you were using a point-and-click interface,
notes Hadley Wickham, author of several National Weather Service: Flood forecasts.
popular R packages and chief scientist with
Orbitz: Statistical analysis to suggest best
RStudio.
hotels to promote to its users.
That also makes it easier for others to vali-
Trulia: Statistical modeling.
date research results and check your work
for errors -- an issue that cropped up in the Source: Revolution Analytics
news recently after an Excel coding error
Why not R? Well, R can appear daunting at
was among several flaws found in an influ-
first. That’s often because R syntax is dif-
ential economics analysis report known as
ferent from that of many other languages,
Reinhart/Rogoff.
not necessarily because it’s any more dif-
The error itself wasn’t a surprise, blogs ficult than others.
Christopher Gandrud, who earned a doc-
“I have written software professionally in
torate in quantitative research methodol-
perhaps a dozen programming languages,
ogy from the London School of Economics.
and the hardest language for me to learn
“Despite our best efforts we always will”
has been R,” writes consultant John D.
make errors, he notes. “The problem is that
Cook in a Web post about R programming
we often use tools and practices that make
for those coming from other languages.
it difficult to find and correct our mistakes.”
“The language is actually fairly simple, but
Sure, you can easily examine complex for- it is unconventional.”
mulas on a spreadsheet. But it’s not nearly
2
R: a beginner’s guide
COMPUTERWORLD.COM
And so, this guide. Our aim here isn’t R code editor allowing you to create a file
mastery, but giving you a path to start with multiple lines of R code -- or open an
using R for basic data work: Extracting key existing file -- and then run the entire file or
statistics out of a data set, exploring a data portions of it.
set with basic graphics and reshaping data
Bottom left is the interactive console where
to make it easier to analyze.
you can type in R statements one line
at a time. Any lines of code that are run
Your first step from the editor window also appear in the
console.
To begin using R, head to r-project.org to
download and install R for your desktop or The top right window shows your work-
laptop. It runs on Windows, OS X and “a space, which includes a list of objects cur-
wide variety of Unix platforms,” but not yet rently in memory. There’s also a history tab
on Android or iOS. with a list of your prior commands; what’s
handy there is that you can select one,
Installing R is actually all you need to get
some or all of those lines of code and one-
started. However, I’d suggest also installing
click to send them either to the console or
the free R integrated development environ-
to whatever file is active in your code editor.
ment (IDE) RStudio. It’s got useful features
you’d expect from a coding platform, such The window at bottom right shows a plot
as syntax highlighting and tab for sug- if you’ve created a data visualization with
gested code auto-completion. I also like its your R code. There’s a history of previous
four-pane workspace, which better man- plots and an option to export a plot to an
ages multiple R windows for typing com- image file or PDF. This window also shows
mands, storing scripts, viewing command external packages (R extensions) that are
histories, viewing visualizations and more. available on your system, files in your work-
ing directory and help files when called
from the console.
■■ Although you don’t need the free RStudio IDE to NN Control + the up arrow (command +
get started, it makes working with R much easier. up arrow on a Mac) is a similar auto-
complete tool. Start typing and hit that
The top left window is where you’ll prob-
key combination, and it shows you a list
ably do most of your work. That’s the R
of every command you’ve typed starting
3
R: a beginner’s guide
COMPUTERWORLD.COM
with those keys. Select the one you want If you don’t want to type the command,
and hit return. This works only in the in RStudio there’s a Packages tab in the
interactive console, not in the code editor lower right window; click that and you’ll
window. see a button to “Install Packages.” (There’s
also a menu command; the location varies
NN Control + enter (command + enter on
depending on your operating system.)
a Mac) takes the current line of code in
the editor, sends it to the console and To see which packages are already installed
executes it. If you select multiple lines of on your system, type:
code in the editor and then hit ctrl/cmd +
installed.packages()
enter, all of them will run.
Or, in RStudio, go to the Packages tab in
For more about RStudio features, including
the lower right window.
a full list of keyboard shortcuts, head to the
online documentation. To use a package in your work once it’s
installed, load it with:
4
R: a beginner’s guide
COMPUTERWORLD.COM
example(functionName)
args(functionName)
5
R: a beginner’s guide
COMPUTERWORLD.COM
If you just want to play with some test data (Aside: What’s that <- where you expect to
to see how they load and what basic func- see an equals sign? It’s the R assignment
tions you can run, the default installation of operator. I said R syntax was a bit quirky.
R comes with several data sets. Type: More on this in the section on R syntax
quirks.)
data()
And if you’re wondering what kind of object
into the R console and you’ll get a listing
is created with this command, mydata is
of pre-loaded data sets. Not all of them are
an extremely handy data type called a data
useful (body temperature series of two bea-
frame -- basically a table of data. A data
vers?), but these do give you a chance to
frame is organized with rows and columns,
try analysis and plotting commands. And
similar to a spreadsheet or database table.
some online tutorials use these sample sets.
The read.csv function assumes that your
One of the less esoteric data sets is mtcars,
file has a header row, so row 1 is the name
data about various automobile models that
of each column. If that’s not the case, you
come from Motor Trends. (I’m not sure
can add header=FALSE to the command:
from what year the data are from, but given
that there are entries for the Valiant and mydata <- read.csv(“filename.txt”,
Duster 360, I’m guessing they’re not very header=FALSE)
recent; still, it’s a bit more compelling than
In this case, R will read the first line as data,
whether beavers have fevers.)
not column headers (and assigns default
You’ll get a printout of the entire data set if column header names you can change
you type the name of the data set into the later).
console, like so:
If your data use another character to sepa-
mtcars rate the fields, not a comma, R also has the
more general read.table function. So if your
There are better ways of examining a data
separator is a tab, for instance, this would
set, which I’ll get into later in this series.
work:
Also, R does have a print() function for
6
R: a beginner’s guide
COMPUTERWORLD.COM
7
R: a beginner’s guide
COMPUTERWORLD.COM
and Perl, and in general I’d rather export a Center data about mobile shopping are
spreadsheet to CSV in hopes of not running available as a CSV file for download. You
into Microsoft special-character prob- can store the data in a variable called pew_
lems. For more info on other formats, see data like this:
UCLA’s How to input data into R which
pew_data <- read.csv(“http://bit.
discusses the foreign add-on package for
ly/11I3iuU”)
importing several other statistical software
file types. It’s important to make sure the file you’re
downloading is in an R-friendly format
If you’d like to try to connect R with a data-
first: in other words, that it has a maximum
base, there are several dedicated packages
of one header row, with each subsequent
such as RPostgreSQL, RMySQL, RMongo,
row having the equivalent of one data
RSQLite and RODBC.
record. Even well-formed government data
(You can see the entire list of available R might include lots of blank rows followed
packages at the CRAN website.) by footnotes -- that’s not what you want in
an R data table if you plan on running sta-
8
R: a beginner’s guide
COMPUTERWORLD.COM
save(variablename, file=”filename.
If you’re finished with variable x and want
rda”)
to remove it from your workspace, use the
rm() remove function: Reload it at any time with:
rm(x) load(“filename.rda”)
9
R: a beginner’s guide
COMPUTERWORLD.COM
Examine your data object Tail can be useful when you’ve read in data
from an external source, helping to see if
Before you start analyzing, you might anything got garbled (or there was some
want to take a look at your data object’s footnote row at the end you didn’t notice).
structure and a few row entries. If it’s a
To quickly see how your R object is struc-
2-dimensional table of data stored in an R
tured, you can use the str() function:
data frame object with rows and columns --
one of the more common structures you’re str(mydata)
likely to encounter -- here are some ideas.
This will tell you the type of object you
Many of these also work on 1-dimensional
have; in the case of a data frame, it will
vectors as well.
also tell you how many rows (observations
Many of the commands below assume that in statistical R-speak) and columns (vari-
your data are stored in a variable called ables to R) it contains, along with the type
mydata (and not that mydata is somehow of data in each column and the first few
part of these functions’ names). entries in each column.
If you type:
head(mydata)
tail(mydata)
10
R: a beginner’s guide
COMPUTERWORLD.COM
Likewise, if you’re interested in the row load the psych package. Install it with this
names -- in essence, all the values in the command:
first column of your data frame -- use:
install.packages(“psych”)
rownames(mydata)
You need to run this install only once on a
system. Then load it with:
Pull basic stats from your library(psych)
data frame You need to run the library command each
Because R is a statistical programming time you start a new R session if you want
platform, it’s got some pretty elegant ways to use the psych package.
to extract statistical summaries from data.
Now try the command:
To extract a few basic stats from a data
frame, use the summary() function: describe(mydata)
11
R: a beginner’s guide
COMPUTERWORLD.COM
mean(myvector, na.rm=TRUE)
?median
■■ Use the combine function to see all pos-
The function description should say sible combinations from a group.
whether the na.rm argument is needed to
Probably most experienced R users would
exclude missing values.
combine these two steps into one like this:
Checking a function’s help files -- even for
combn(c(“Bob”, “Joanne”, “Sally”,
simple functions -- can also uncover addi-
“Tim”, “Neal”),2)
tional useful options, such as an optional
12
R: a beginner’s guide
COMPUTERWORLD.COM
But separating the two can be more read- That will give you a 1-dimensional vector of
able for beginners. numbers like this:
data [12] 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4
33.9 21.5 15.5
Maybe you don’t need correlations for
every column in your data frame and you [23] 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
just want to work with a couple of columns, 15.0 21.4
not 15. Perhaps you want to see data that
The numbers in brackets are not part of
meets a certain condition, such as within 3
your data, by the way. They indicate what
standard deviations. R lets you slice your
item number each line is starting with. If
data sets in various ways, depending on the
you’ve only got one line of data, you’ll just
data type.
see [1]. If there’s more than one line of data
To select just certain columns from a data and only the first 11 entries can fit on the
frame, you can either refer to the columns first line, your second line will start with
by name or by their location (i.e., column 1, [12], and so on.
2, 3, etc.).
Sometimes a vector of numbers is exactly
For example, the mtcars sample data frame what you want -- if, for example, you want
has these column names: mpg, cyl, disp, hp, to quickly plot mtcars$mpg and don’t need
drat, wt, qsec, vs, am, gear and carb. item labels, or you’re looking for statistical
info such as variance and mean.
Can’t remember the names of all the col-
umns in your data frame? If you just want Chances are, though, you’ll want to sub-
to see the column names and nothing else, set your data by more than one column
instead of functions such as str(mtcars) at a time. That’s when you’ll want to use
and head(mtcars) you can type: bracket notation, what I think of as rows-
comma-columns. Basically, you take the
names(mtcars)
name of your data frame and follow it by
That’s handy if you want to store the names [rows,columns]. The rows you want come
in a variable, perhaps called mtcars.col- first, followed by a comma, followed by the
names (or anything else you’d like to call columns you want. So, if you want all rows
it): but just columns 2 through 4 of mtcars,
you can use:
mtcars.colnames <- names(mtcars)
mtcars[,2:4]
But back to the task at hand. To access
only the data in the mpg column in mtcars, Do you see that comma before the 2:4?
you can use R’s dollar sign notation: That’s leaving a blank space where the
“which rows do you want?” portion of the
mtcars$mpg
bracket notation goes, and it means “I’m
More broadly, then, the format for access- not asking for any subset, so return all.”
ing a column by name would be: Although it’s not always required, it’s not a
bad practice to get into the habit of using
dataframename$columnname
a comma in bracket notation so that you
13
R: a beginner’s guide
COMPUTERWORLD.COM
remember whether you were slicing by col- [28] TRUE FALSE FALSE FALSE TRUE
umns or rows.
To turn that into a listing of the data you
If you want multiple columns that aren’t want, use that logical test condition and
contiguous, such as columns 2 AND 4 but row-comma-column bracket notation.
not 3, you can use the notation: Remember that this time you want to select
rows by condition, not columns. This:
mtcars[,c(2,4)]
mtcars[mtcars$mpg>20,]
A couple of syntax notes here:
tells R to get all rows from mtcars where
R indexes from 1, not 0. So your first col-
mpg > 20, and then to return all the
umn is at [1] and not [0].
columns.
R is case sensitive everywhere. mtcars$mpg
If you don’t want to see all the column data
is not the same as mtcars$MPG.
for the selected rows but are just interested
mtcars[,-1] will not get you the last column in displaying, say, mpg and horsepower
of a data frame, the way negative indexing for cars with an mpg greater than 20, you
works in many other languages. Instead, could use the notation:
negative indexing in R means exclude that
mtcars[mtcars$mpg>20,c(1,4)]
item. So, mtcars[,-1] will return every col-
umn except the first one. using column locations, or:
14
R: a beginner’s guide
COMPUTERWORLD.COM
are not making any changes to the data What if you wanted to find the row with the
that need to be saved, you can attach and highest mpg?
detach a copy of the data set temporarily.
subset(mtcars, mpg==max(mpg))
The attach() function works like this:
If you just wanted to see the mpg informa-
attach(mtcars) tion for the highest mpg:
mpg20 <- mtcars$mpg > 20 If you just want to use subset to extract
some columns and display all rows, you can
You can leave out the data set reference and
either leave the row conditional spot blank
type this instead:
with a comma, similar to bracket notation:
mpg20 <- mpg > 20
subset(mtcars, , c(“mpg”, “hp”))
After using attach() remember to use the
Or, indicate your second argument is for
detach function when you’re finished:
columns with select= like this:
detach()
subset(mtcars, select=c(“mpg”, “hp”))
Some R users advise avoiding attach()
Update: The dplyr package, released in early
because it can be easy to forget to detach().
2014, is aimed at making manipulation of
If you don’t detach() the copy, your vari-
data frames faster and more rational, with
ables could end up referencing the wrong
similar syntax for a variety of tasks. To
data set.
select certain rows based on specific logical
criteria, you’d use the filter() function with
Alternative to bracket the syntax filter(dataframename, logi-
So, in the mtcars example, to find all rows You can also combine filter and subset
where mpg is greater than 20 and return with the dplyr %.% “chaining” operation
only those rows with their mpg and hp data, that allows you to string together multiple
the subset() statement would look like: commands on a data frame. The chaining
syntax in general is:
subset(mtcars, mpg>20, c(“mpg”,
“hp”)) dataframename %.%
firstfunction(argument for first
15
R: a beginner’s guide
COMPUTERWORLD.COM
table(diamonds$cut)
table(diamonds$cut, diamonds$color)
16
R: a beginner’s guide
COMPUTERWORLD.COM
plot(mtcars$disp, mtcars$mpg)
17
R: a beginner’s guide
COMPUTERWORLD.COM
Using ggplot2
In particular, the ggplot2 package is quite
popular and worth a look for robust visu-
alizations. ggplot2 requires a bit of time to
learn its “Grammar of Graphics” approach.
But once you’ve got that down, you have a ■■ A scatterplot from ggplot2
tool to create many different types of visu- using the qplot() function.
alizations using the same basic structure.
The qplot default starts the y axis at a value
If ggplot2 isn’t installed on your system yet, that makes sense to R. However, you might
install it with the command: want your y axis to start at 0 so you can
install.packages(“ggplot2”) better see whether changes are truly mean-
ingful (starting a graph’s y axis at your first
You only need to do this once. value instead of 0 can sometimes exagger-
To use its functions, load the ggplot2 pack- ate changes).
age into your current R session -- you only Use the ylim argument to manually set your
need to do this once per R session -- with lower and upper y axis limits:
the library() function:
qplot(disp, mpg, ylim=c(0,35),
library(ggplot2) data=mtcars)
Onto some ggplot2 examples. Bonus intermediate tip: Sometimes on a
ggplot2 has a “quick plot” function called scatterplot you may not be sure if a point
qplot() that is similar to R’s basic plot() represents just one observation or multiple
function but adds some options. The basic ones, especially if you’ve got data points
quick plot code: that repeat -- such as in this example that
ggplot2 creator Hadley Wickham generated
qplot(disp, mpg, data=mtcars) with the command:
generates a scatterplot.
18
R: a beginner’s guide
COMPUTERWORLD.COM
The code structure for a basic graph with It may be a little confusing here since both
ggplot() is a bit more complicated than in the data set and one of its columns are
either plot() or qplot(); it goes as follows: called the same thing: pressure. That first
“pressure” represents the name of the data
ggplot(mtcars, aes(x=disp, y=mpg)) + frame; the second, “y=pressure,” represents
geom_point() the column named pressure.
The first argument in the ggplot() function, In these examples, I set only x and y aes-
mtcars, is fairly easy to understand -- that’s thetics. But there are lots more aesthetics
we could add, such as color, axes and more.
19
R: a beginner’s guide
COMPUTERWORLD.COM
ggplot(mydata, aes(x=xcol, y=ycol), ■■ Bar chart with R’s bar plot() function.
ylim=0) + geom_line()
To label the bars on the x axis, use the
Perhaps you’d like both lines and points on names.arg argument and set it to the col-
that temperature vs. pressure graph? umn you want to use for labels:
barplot(BOD$demand)
barplot(BOD$demand, main=”Graph of
demand”)
■■ Creating a bar plot.
468
20
R: a beginner’s guide
COMPUTERWORLD.COM
11 7 14
Histograms
Now you can create a bar graph of the cyl-
Histograms work pretty much the same,
inder count:
except you want to specify how many buck-
barplot(cylcount) ets or bins you want your data to be sepa-
rated into. For base R graphics, use:
ggplot2’s qplot() quick plotting function
can also create bar graphs: hist(mydata$columnName, breaks = n)
qplot(columnName, data=mydata,
binwidth=n)
ggplot(mydata, aes(x=columnName)) +
geom_histogram(binwidth=n)
21
R: a beginner’s guide
COMPUTERWORLD.COM
boxplot(diamonds$x, diamonds$y, So, if you want five colors from the rainbow
diamonds$z) palette, use:
Using color
Looking at nothing but black and white
graphics can get tiresome after a while. Of
course, there are numerous ways of using
color in R.
■■ Using three colors in the R rainbow palette.
Colors in R have both names and numbers
Now that you’ve got a list of colors, how do
as well as the usual RGB hex code, HSV
you get them in your graphic? Here’s one
(hue, saturation and value) specs and oth-
way. Say you’re drawing a 3-bar barchart
ers. And when I say “names,” I don’t mean
using ggplot() and want to use 3 colors
just the usual “red,” “green,” “blue,” “black”
from the rainbow palette. You can create a
and “white.” R has 657 named colors. The
3-color vector like:
colors() or colours() function -- R does not
discriminate against either American or mycolors <- rainbow(3)
British English -- gives you a list of all of
Or for the heat.colors pallette:
them. If you want to see what they look like,
not just their text names, you can get a full, mycolors <- heat.colors(3)
multi-page PDF chart with color numbers,
Now instead of using the geom_bar()
colors names and swatches, sorted in vari-
function without any arguments, add
ous ways. Or you can find just the names
fill=mycolors to geombar() like this:
and color swatches for each.
ggplot(mtcars, aes(x=factor(cyl))) +
There are also R functions that automati-
geom_bar(fill=mycolors)
cally generate a vector of n colors using a
specific color palette such as “rainbow” or You don’t need to put your list of colors
“heat”: in a separate variable, by the way; you can
merge it all in a single line of code such as:
rainbow(n)
ggplot(mtcars, aes(x=factor(cyl))) +
heat.colors(n)
geom_bar(fill=rainbow(3))
terrain.colors(n)
But it may be easier to separate the colors
topo.colors(n) out if you want to create your own list of
colors instead of using one of the defaults.
cm.colors(n)
22
R: a beginner’s guide
COMPUTERWORLD.COM
The basic R plotting functions can also entry in testscores is greater than or equal
accept a vector of colors, such as: to 80, add “blue” to the testcolors vec-
tor; otherwise add “red” to the testcolors
barplot(BOD$demand, col=rainbow(6))
vector.’
You can use a single color if you want all
Now that you’ve got the list of colors prop-
the items to be one color (but not mono-
erly assigned to your list of scores, just add
chrome), such as
the testcolors vector as your desired color
barplot(BOD$demand, col=”royalblue3”) scheme:
23
R: a beginner’s guide
COMPUTERWORLD.COM
testscores <- sort(c(96, 71, 85, 92, Why stat = “identity”? That’s needed here
82, 78, 72, 81, 68, 61, 78, 86, 90), to show that the y axis represents a numer-
decreasing = TRUE) ical value as opposed to an item count.
The sort() function defaults to ascending ggplot2’s qplot() also has easy ways to
sort; for descending sort you need the addi- color bars by a factor, such as number of
tional argument: decreasing = TRUE. cylinders, and then automatically gener-
ate a legend. Here’s an example of graph
If that code above is starting to seem
counting the number of 4-, 6- and 8-cylin-
unwieldy to you as a beginner, break it
der cars in the mtcars data set:
into two lines for easier reading, and per-
haps also set a new variable for the sorted qplot(factor(cyl), data=mtcars,
version: geom=”bar”, fill=factor(cyl))
testscores <- c(96, 71, 85, 92, 82, But, as I said, we’re getting somewhat
78, 72, 81, 68, 61, 78, 86, 90) beyond a beginner’s overview of R when
coloring by factor. For a few more examples
testscores_sorted <- sort(testscores,
and details for many of the themes cov-
decreasing = TRUE)
ered here, you might want to see the online
If you had scores in a data frame called tutorial Producing Simple Graphs with R.
results with one column of student names For more on graphing with color, check
called students and another column of out a source such as the R Graphics Cook-
scores called testscores, you could use the book. The ggplot2 documentation also has
ggplot2 package’s ggplot() function as well: a lot of examples, such as this page for bar
geometry.
ggplot(results, aes(x=students,
y=testscores)) + geom_
bar(fill=testcolors, stat = Exporting your graphics
“identity”)
You can save your R graphics to a file for
use outside the R environment. RStudio
has an export option in the plots tab of the
bottom right window.
24
R: a beginner’s guide
COMPUTERWORLD.COM
jpeg(“myplot.jpg”, width=350,
height=420)
barplot(BOD$demand, col=rainbow(6))
dev.off()
25
R: a beginner’s guide
COMPUTERWORLD.COM
26
R: a beginner’s guide
COMPUTERWORLD.COM
it out -- if you’re referring to consecutive have for, while and repeat loops, you’ll
values in a range with a colon between more likely see operations applied to a data
minimum and maximum, like this: collection using apply() functions or by
using the plyr() add-on package functions.
my_vector <- (1:10)
But first, some basics.
I bring up this exception because I’ve run
into that style quite a bit in R tutorials and If you’ve got a vector of numbers such as:
texts, and it can be confusing to see the c
my_vector <- c(7,9,23,5)
required for some multiple values but not
others. Note that it won’t hurt anything and, say, you want to multiply each by 0.01
to use the c with a colon-separated range, to turn them into percentages, how would
though, even if it’s not required, such as: you do that? You don’t need a for, foreach
or while loop. Instead, you can create a new
my_vector <- c(1:10)
vector called my_pct_vectors like this:
One more very important point about the
my_pct_vector <- my_vector * 0.01
c() function: It assumes that everything in
your vector is of the same data type -- that Performing a mathematical operation on
is, all numbers or all characters. If you cre- a vector variable will automatically loop
ate a vector such as: through each item in the vector.
my_vector <- c(1, 4, “hello”, TRUE) Typically in data analysis, though, you
want to apply functions to subsets of data:
You will not have a vector with two integer
Finding the mean salary by job title or the
objects, one character object and one logi-
standard deviation of property values by
cal object. Instead, c() will do what it can
community. The apply() function group
to convert them all into all the same object
and plyr add-on package are designed for
type, in this case all character objects. So
that.
my_vector will contain “1”, “4”, “hello” and
“TRUE”. In other words, c() is also for “con- There are more than half a dozen functions
vert” or “coerce.” in the apply family, depending on what type
of data object is being acted upon and what
To create a collection with multiple object
sort of data object is returned. “These func-
types, you need a list, not a vector. You
tions can sometimes be frustratingly diffi-
create a list with the list() function, not c(),
cult to get working exactly as you intended,
such as:
especially for newcomers to R,” says a blog
My_list <- list(1,4,”hello”, TRUE) post at Revolution Analytics, which focuses
on enterprise-class R.
Now you’ve got a variable that holds the
number 1, the number 4, the character Plain old apply() runs a function on either
object “hello” and the logical object TRUE. every row or every column of a 2-dimen-
sional matrix where all columns are the
Loopless loops same data type. For a 2-D matrix, you also
need to tell the function whether you’re
Iterating through a collection of data with applying by rows or by columns: Add the
loops like “for” and “while” is a corner- argument 1 to apply by row or 2 to apply by
stone of many programming languages. column. For example:
That’s not the R way, though. While R does
27
R: a beginner’s guide
COMPUTERWORLD.COM
apply(my_matrix, 1, median) then, yes, you’ve got to know the ins and
outs of data types. But my assumption is
returns the median of every row in my_
that you’re here to try generating quick
matrix and
plots and stats before diving in to create
apply(my_matrix, 2, median) complex code.
calculates the median of every column. So, to start off with the basics, here’s what
I’d suggest you keep in mind for now: R
Other functions in the apply() family such
has multiple data types. Some of them
as lapply() or tapply() deal with different
are especially important when doing basic
input/output data types. Australian statisti-
data work. And some functions that are
cal bioinformatician Neal F.W. Saunders
quite useful for doing your basic data work
has a nice brief introduction to apply in R
require your data to be in a particular type
in a blog post if you’d like to find out more
and structure.
and see some examples. (In case you’re
wondering, bioinformatics involves issues More specifically, R has the “Is it an inte-
around storing, retrieving and organizing ger or character or true/false?” data type,
biological data, not just analyzing it.) the basic building blocks. R has several of
these including integer, numeric, charac-
Many R users who dislike the the apply
ter and logical. Missing values are repre-
functions don’t turn to for-loops, but
sented by NaN (if a mathematical function
instead install the plyr package created by
won’t work properly) or NA (missing or
Hadley Wickham. He uses what he calls
unavailable).
the “split-apply-combine” model of dealing
with data: Split up a collection of data the As mentioned in the prior section, you can
way you want to operate on it, apply what- have a vector with multiple elements of the
ever function you want to each of your data same type, such as:
group(s) and then combine them all back
1, 5, 7
together again.
or
The plyr package is probably a step beyond
this basic beginner’s guide; but if you’d like “Bill”, “Bob”, “Sue”
to find out more about plyr, you can head to
>
Wickham’s plyr website. There’s also a use-
ful slide presentation on plyr in PDF format A single number or character string is also
from Cosma Shalizi, an associate professor a vector -- a vector of 1. When you access
of statistics at Carnegie Mellon University, the value of a variable that’s got just one
and Vincent Vu. Another PDF presentation value, such as 73 or “Learn more about R at
on plyr is from an introduction to R work- Computerworld.com,” you’ll also see this in
shop at Iowa State University. your console before the value:
[1]
R data types in brief (very That’s telling you that your screen print-
brief) out is starting at vector item number one.
If you’ve got a vector with lots of values
Should you learn about all of R’s data types
so the printout runs across multiple lines,
and how they behave right off the bat, as
each line will start with a number in brack-
a beginner? If your goal is to be an R ninja
28
R: a beginner’s guide
COMPUTERWORLD.COM
ets, telling you which vector item number R also has special vector and list types
that particular line is starting with. (See the that are of special interest when analyzing
screen shot, below.) data, such as matrices and data frames. A
matrix has rows and columns; you can find
a matrix dimension with dim() such as
dim(my_matrix)
class(3L)
class(as.integer(3))
29
R: a beginner’s guide
COMPUTERWORLD.COM
30
R: a beginner’s guide
COMPUTERWORLD.COM
You can.
sqldf(“select * from mtcars where mpg ■■ Invoking R’s data editing win-
> 20 order by mpg desc”) dow with the edit() function.
This will find all rows in the mtcars sample This can be useful if you’ve got a data set
data frame that have an mpg greater than with a lot of columns that are wrapping in
20, ordered from highest to lowest mpg. the small command-line window. However,
since there’s no way to save your work
Most R experts will discourage newbies as you go along -- changes are saved only
from “cheating” this way: Falling back when you close the editing window -- and
on SQL makes it less likely you’ll power there’s no command-history record of what
through learning R syntax. However, it’s you’ve done, the edit window probably isn’t
there for you in a pinch -- or as a useful your best choice for editing data in a proj-
way to double-check whether you’re get- ect where it’s important to repeat/repro-
ting back the expected results from an R duce your work.
expression.
In RStudio you can also examine a data
object (although not edit it) by clicking on
Examine and edit data with a it in the workspace tab in the upper right
GUI window.
31
R: a beginner’s guide
COMPUTERWORLD.COM
write.table(myData, “testfile.txt”,
sep=”\t”)
32
R: a beginner’s guide
COMPUTERWORLD.COM
60+ R resources to
improve your data skills
This list was originally published as part of Resource Topic Type
the Computerworld Beginner’s Guide to R R User Meetups general R community
but has since been expanded to also include RStudio R pro-
documentation
resources for advanced beginner and interme- Documentation gramming
diate users. CRAN general R official R site
These websites, videos, blogs, social media/ online interac-
Try R general R
tive class
communities, software and books/ebooks
can help you do more with R; my favorites 4 data wrangling
tasks in R for general R online reference
are listed in bold. advanced beginners
Data manipulation online reference
general R
R Resources tricks: Even better in R & PDF
Cookbook for R general R online reference
Quick-R general R online reference
Resource Topic Type
Short List of R
R Cookbook general R book or ebook general R online reference
Commands
R Graphics Cookbook graphics book or ebook
FAQ About R general R online reference
R In Action general R book or ebook
graphics-
Chart Chooser in R online reference
The Art of R ggplot2
general R book or ebook
Programming
graphics-
R Graphic Catalog online reference
R for Everyone general R book or ebook ggplot2
R in a Nutshell general R book or ebook graphics-
ggplot2 Cheat Sheet online reference
R For Dummies general R book or ebook ggplot2
33
R: a beginner’s guide
COMPUTERWORLD.COM
34
R: a beginner’s guide
COMPUTERWORLD.COM
35
R: a beginner’s guide
COMPUTERWORLD.COM
36
R: a beginner’s guide
COMPUTERWORLD.COM
often covered in-depth in general R books. as complete, can be helpful for answering
The author has posted source code for some “How do I do that?” questions.
generating the book on GitHub, though, if
Quick-R. This site has a fair amount of
you want to create an electronic version of
samples and brief explanations grouped
it yourself.
by major category and then specific items.
Exploring Everyday Things with R and For example, you’d head to “Stats” and
Ruby. This book oddly goes from a couple then “Frequencies and crosstabs” to get
of basic introductory chapters to some an explainer of the table() function. This
fairly robust, beyond-beginner program- ranges from basics (including useful how-
ming examples; for those who are just to’s for customizing R startup) through
starting to code, much of the book may beyond-beginner statistics (matrix algebra,
be tough to follow at the outset. However, anyone?) and graphics. By Robert I. Kaba-
the intro to R is one of the better ones I’ve coff, author of R in Action.
read, including lot of language fundamen-
R Reference Card. If you want help remem-
tals and basics of graphing with ggplot2.
bering function names and formats for vari-
Plus experienced programmers can see how
ous tasks, this 4-page PDF is quite useful
author Sau Sheong Chang splits up tasks
despite its age (2004) and the fact that
between a general language like Ruby and
a link to what’s supposed to be the latest
the statistics-focused R.
version no longer works. By Tom Short, an
engineer at the Electric Power Research
Online references Institute.
4 data wrangling tasks in R for advanced A short list of R the most useful commands.
beginners. This follow-up to our Beginner’s Commands grouped by function such as
Guide outlines how to do several specific input, “moving around” and “statistics
data tasks in R: add columns to an exist- and transformations.” This offers minimal
ing data frame, get summaries, sort results explanations, but there’s also a link to a
and reshape data. With sample code and longer guide to Using R for psychologi-
explanations. cal research. HTML format makes it easy
to cut and paste commands. Also some-
Data manipulation tricks: Even better in
what old, from 2005. By William Revelle,
R. From working with dates to reshaping
psychology professor at Northwestern
data to if-then-else statements, see how to
University.
perform common data munging tasks. You
can also download these R tips & tricks as Chart Chooser in R. This has numerous
a PDF (free Insider registration required). examples of R visualizations and sample
code to go with them, including bar, col-
Cookbook for R. Not to be confused with
umn, stacked bar & column, bubble charts
the R Cookbook book mentioned above,
and more. It also breaks down the visual-
this website by software engineer Winston
izations by categories like comparison, dis-
Chang (author of the R Graphics Cook-
tribution and trend. By Greg Lamp, based
book) offers how-to’s for tasks such as data
on Juice Labs’ Chart Chooser for Excel and
input and output, statistical analysis and
PowerPoint.
creating graphs. It’s got a similar format
to an O’Reilly Cookbook; and while not R Graph Catalog. Lots of graph and other
plot examples, easily searchable and each
37
R: a beginner’s guide
COMPUTERWORLD.COM
38
R: a beginner’s guide
COMPUTERWORLD.COM
Google Developers’ Intro to R. This series tor Roger Peng, associate professor of
of 21 short YouTube videos includes some biostatistics at Johns Hopkins University,
basic R concepts, a few lessons on reshap- posted his lectures on YouTube; Revolution
ing data and some info on loops. In addi- Analytics then collected them on a handy
tion, six videos focus on a topic that’s often single page. While I found some of these
missing in R intros: working with and a bit difficult to follow at times, they are
writing your own functions. This YouTube packed with information, and you may find
playlist offers a good programmer’s intro- them useful.
duction to the language -- just note that if
you’re looking to learn more about visual-
izations with R, that’s not one of the topics
covered.
39
R: a beginner’s guide
COMPUTERWORLD.COM
Coursera: Statistics One If you don’t mind some practice and get more comfortable
going through a full, 12-week stats course using R syntax.
along with learning R, Princeton Uni-
An Introduction to R. Let’s not forget the
versity senior lecturer Andrew Conway’s
R Project site itself, which has numerous
class includes an introduction to R. “All
resources on the language including this
the examples and assignments will involve
intro. The style here is somewhat dry, but
writing code in R and interpreting R out-
you’ll know you’re getting accurate, up-to-
put,” says the course description. You can
date information from the R Core Team.
check the Coursera link to see if and when
future sessions are scheduled. How to Visualize and Compare Distri-
butions. This short and highly readable
Introduction to Data Science with R. At
Flowing Data tutorial goes over traditional
$160 this O’Reilly training course is some-
visualizations such as histograms and box
what pricey considering how many free and
plots. With downloadable code.
lower-cost video classes there are out there.
However, if you’re looking for a step-by- Handling and Processing Strings in
step intro to R, this is a useful course, start- R. This PDF download covers many
ing with language and ggplot2 visualization things you’re want to do with text, from
basics through modeling. It’s taught by string lengths and formatting to search
RStudio Master Instructor Garrett Grol- and replace with regular expressions to
emund, who focuses on hands-on learning basic text analysis. By statistician Gaston
as well as explaining a few of the language’s Sanchez.
quirks. If cost is an issue and you’re not in
Learning statistics with R: A tutorial for
a hurry, sign up for O’Reilly’s deal emails
psychology students and other beginners
and you may eventually find a 50% off sale.
by Daniel Navarro at the University of
Data Analysis and Visualization Using R. Adelaide (PDF). 500+ pages that go from
Free course that uses both video and inter- “Why do we learn statistics” and “Statis-
active R to teach language basics, ggplot2 tics in every day life” to linear regression
visualization basics, some statistical tests and ANOVA (ANalysis Of VAriance). If you
and exploratory data analysis includ- don’t need/want a primer in statistics, there
ing data.table. Videos by Princeton Ph.D. are still many sections that focus specifi-
student David Robinson and Neo Christo- cally on R.
pher Chung, Ph.D, filmed and edited at the
R Tutorial. A reasonably robust beginning
Princeton Broadcast Center.
guide that includes sections on data types,
probability and plots as well as sections
Other online introductions focused on statistical topics such as linear
40
R: a beginner’s guide
COMPUTERWORLD.COM
such as basic graphics and graphics with Producing Simple Graphs with R. Although
ggplots. He’s also posted code for tasks 6+ years old now, this gives a few more
such as data import and extracting por- details and examples for several of the visu-
tions of your data comparing R with alter- alization concepts touched on in our begin-
natives such as SAS and SPSS. ner’s guide. By Frank McCown at Harding
University.
Aggregating and restructuring data. This
excerpt from R in Action goes over one of Short courses. Materials from various
the most important subjects in using R: courses taught by Hadley Wickham, chief
reshaping your data so it’s in the format scientist at RStudio and author of several
needed for analysis and then grouping popular R packages including ggplot2.
and summarizing that data by factors. In Features slides and code for topics beyond
addition to touching on base-R functions beginning R, such as R development master
like the useful-but-not-always-well-known class.
aggregate(), it also covers melt() and cast()
Quick introduction to ggplot2. Very nice,
with the reshape package. By Robert I.
readable and -- as promised -- quick intro-
Kabacoff.
duction to the ggplot2 add-on graphic
Getting started with charts in R. From the package in R, incuding lots of sample plots
popular FlowingData visualization web- and code. By Google engineer Edwin Chen.
site run by Nathan Yau, this tutorial offers
ggplot2 workshop presentation. This
examples of basic plotting in R. Includes
robust, single-but-very-long-page tutorial
downloadable source code. (While many
offers a detailed yet readable introduction
FlowingData tutorials now require a paid
to the ggplot2 graphing package. What sets
membership to the site, as of May 2013 this
this apart is its attention to its theoretical
one did not.)
underpinnings while also offering useful,
Using R for your basic statistical Needs concrete examples. From a presentation at
LISA Short Course. Aimed at those who the Advances in Visual Methods for Lin-
already know stats but want to learn R, this guistics conference. By Josef Fruehwald,
is a file of R code with comments, making it then a PhD candidate at the University of
easy to run (and alter) the code yourself. Pennsylvania.
The programming is easy to follow, but if
ggplot2_tutorial.R. This online page at
you haven’t brushed up on your stats lately,
RPubs.com, prepared for the Santa Barbara
be advised that comments such as
R User Group, includes a lot of commented
Suppose we’d like to produce a reduced R code and graph examples for creating
set of independent variables. We could use data visualizations with ggplot2.
the function # step() to perform stepwise
More and Fancier Graphics. This one-page
model selection based on AIC which is
primer features loads of examples, includ-
-2log(Likelihood) + kp? Where k=2 # and
ing explainers of a couple of functions that
p = number of model parameters (beta
let you interact with R plots, locator() and
coefficients).
identify() as well as a lot of core-R plotting
customization. By William B. King, Coastal
may be tough to follow. By Nels Johnson at
Carolina University.
Virginia Tech’s Laboratory for Interdisci-
plinary Statistical Analysis.
41
R: a beginner’s guide
COMPUTERWORLD.COM
ggplot2 Guide. This ggplot2 explainer skips its of R’s apply family. This post goes over
the simpler qplot option and goes straight 6 extremely useful base R functions with
to the more powerful but complicated readable explanations and helpful examples.
ggplot command, starting with basics of a By John Mules White, “soon-to-be scientist
simple plot and going through geoms (type at Facebook.”
of plot), faceting (plotting by subsets),
Introductory Econometrics Using Quandl
statistics and more. By data analyst George
and R While this does indeed promote
Bull at Sharp Statistics.
Quandl as your data source, that data is
Using R. In addition to covering basics, free, and for those interested in using R
there are useful sections on data manipu- for regressions, you’ll find several detailed
lation -- an important topic not easily walk-throughs from data import through
covered for beginners -- as well as getting statistical analysis.
statistical summaries and generating basic
Introduction to dplyr. The dplyr package
graphics with base R, the Lattice package
(by ggplot2 creator Hadley Wickham) sig-
and ggplot2. Short explainers are inter-
nificantly speeds up operations like group-
spersed with demo code, making this useful
ing and sorting of data frames. It also aims
as both a tutorial and reference site. By
to rationalize such functions by using a
analytics consultant Alastair Sanderson,
common syntax. In this short introductory
formerly research fellow in the Astrophys-
vignette, you’ll learn about “five basic data
ics & Space Research (ASR) Group at the
manipulation” -- filter(), arrange(), select(),
University of Birmingham in the U.K.
mutate() and summarise() -- including
The Undergraduate Guide to R. This is a examples, as well as how to chain them
highly readable, painless introduction to R together for more streamlined, readable
that starts with installation and the com- code. Another useful package for manipu-
mand environment and goes through data lating data in R: doBy.
types, input and output, writing your own
Applied Time Series Analysis. Text-based
functions and programming tips. Viewable
online class from Penn State “to learn and
as a Google Doc or downloadable as a PDF,
apply statistical methods for the analysis
plus accompanying files. By Trevor Martin,
of data that have been observed over time.”
then at Princeton University, funded in part
Access to the articles is free, although there
by an NIH grant.
is no community or instructor participation.
How to turn CSV data into interactive visu-
13 resources for time series analysis. A
alizations with R and rCharts. 9-page slide-
video and 12 slide presentations by Rob
show gives step-by-step instructions on
J. Hyndman, author of Forecasting time
various options for generating interactive
series using R. Also has links to exercises
graphics. The charts and graphs use jQuery
and answers to the exercises.
libraries as the underlying technology but
only a couple of line of R code are needed. knitr in a knutshell. knitR is designed to
By Sharon Machlis, Computerworld. easily create reports and other documents
that can combine text, R code and the
Higher Order Functions in R. If you’re at
results of R code -- in short, a way to share
the point where you want to apply func-
your R analyses with others. This “minimal
tions on multiple vectors and data frames,
tutorial” by Karl Broman goes over subjects
you may start bumping up against the lim-
such as creating Markdown documents
42
R: a beginner’s guide
COMPUTERWORLD.COM
43
R: a beginner’s guide
COMPUTERWORLD.COM
Post: 10 R packages I wish I knew about Using dates and times in R. This post from
earlier. Not sure all of these would be in a presentation by Bonnie Dixon at the
my top 10, but unless you’ve spent a fair Davis R Users’ group goes over some of the
amount of time exploring packages, you’ll intricacies of dates and times in R, includ-
likely find at least a couple of interesting ing various date/time classes as well as
and useful R add-ons. different options for performing date/time
calculations and other manipulations.
Post: R programming for those coming
from other languages. If you’re an experi- Scraping Pro-Football Data and Interactive
enced programmer trying to learn R, you’ll Charts using rCharts, ggplot2, and shiny.
probably find some useful tips here. This is a highly useful example of begin-
ning-to-end data analysis with R. You’ll
Post: A brief introduction to ‘apply’ in R. If
see a sample of how to scrape data off a
you want to learn how the apply() function
website, clean and restructure the data and
family works, this is a good primer.
then visualize it in several ways, including
Translating between R and SQL. If you’re interactive Web graphics -- all with down-
more experienced (and comfortable) with loadable code. By Vivek Patil, an associate
SQL than R, it can be frustrating and professor at Gonzaga University.
confusing at times to figure out how to do
basic data tasks such as subsetting your
data. Statistics consultant Patrick Burns
Search
shows how to do common data slicing in Searching for “R” on a general search
both SQL and R, making it easier for expe- engine like Google can be somewhat frus-
rienced database users to add R to their trating, given how many utterly unrelated
toolkit. English words include the letter r. Some
Graphs & Charts in base R, ggplot2 and search possibilities:
rCharts. There are lots of sample charts RSeek is a Web search engine that just
with code here, showing how to do similar returns results from certain R-focused
visualization tasks with basic R, the ggplot2 websites.
add-on package and rCharts for interactive
HTML visualizations. R site search returns results just from
R functions, package “vignettes” (docu-
When to use Excel, when to use R? For mentation that helps explain how a func-
spreadsheet users starting to learn R, this tion works) and task views (focusing on
is a useful question to consider. Michael a particular field such as social science or
Milton, author of Head First Data Analysis econometrics).
(which discusses both Excel and R), offers
practical (and short) advice on when to use You can also search the R mailing list
each. archives.
44
R: a beginner’s guide
COMPUTERWORLD.COM
45