Learn To Use: Your Hands-On Guide

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

LEARN TO USE R

Your hands-on guide


2 Introduction 17 Painless data
6 Getting your visualization
data into R 26 Syntax quirks you’ll
10 Easy ways to do want to know
basic data analysis 33 Useful resources

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.

Learning the shortcuts


Wickham, the RStudio chief scientist, says
these are the three most important key-
board shortcuts in RStudio:

NN Tab is a generic auto-complete function.


If you start typing in the console or editor
and hit the tab key, RStudio will suggest
functions or file names; simply select the
one you want and hit either tab or enter
to accept it.

■■ 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:

Setting your working library(“thepackagename”)

directory If you’d like to make sure your packages


stay up to date, you can run:
Change your working directory with the
setwd() function, such as: update.packages()

setwd(“~/mydirectory”) and get the latest versions for all your


installed packages.
Note that the slashes always have to be for-
ward slashes, even if you’re on a Windows If you no longer need or want a package on
system. For Windows, the command might your system, use the function:
look something like:
remove.packages(“thepackagename”)
setwd(“C:/Sharon/Documents/
RProjects”)
Help!
If you are using RStudio, you can also use
If you want to find out more about a func-
the menu to change your working directory
tion, you can type a question mark followed
under Session > Set Working Directory.
by the function name -- one of the rare
times parentheses are not required in R,
Installing and using packages like so:

Chances are if you’re going to be doing, ?functionName


well, pretty much anything in R, you’re
This is a shortcut to the help function,
going to want to take advantage of some of
which does use parentheses:
the thousands of add-on packages avail-
able for R at CRAN, the Comprehensive R help(functionName)
Archive Network. The command for install-
Although I’m not sure why you’d want to
ing a package is:
use this as opposed to the shorter ?func-
install.packages(“thepackagename”) tionName command.

4
R: a beginner’s guide
COMPUTERWORLD.COM

If you already know what a function does


and just want to see formats for using it
properly, you can type:

example(functionName)

and you’ll get a list with examples of the


function being used, if there’s one available.
The arguments (args) function:

args(functionName)

just displays a list of a function’s


arguments.

If you want to search through R’s help


documentation for a specific term, you can
use:

help.search(“your search term”)

That also has a shortcut:

??(“my search term”)

No parentheses are needed if the search


term is a single word without spaces.

5
R: a beginner’s guide
COMPUTERWORLD.COM

Get your data into R


Once you’ve installed and configured R printing with more options, but R begin-
to your liking, it’s time to start using it to ners rarely seem to use it.
work with data. Yes, you can type your data
directly into R’s interactive console. But
for any kind of serious work, you’re a lot
Existing local data
more likely to already have data in a file R has a function dedicated to reading
somewhere, either locally or on the Web. comma-separated files. To import a local
Here are several ways to get data into R for CSV file named filename.txt and store the
further work. data into one R variable named mydata, the
syntax would be:

Sample data mydata <- read.csv(“filename.txt”)

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

mydata <- read.table(“filename.txt”,


sep=”\t”, header=TRUE)

The command above also indicates there’s


a header row in the file with header=TRUE.

If, say, your separator is a character such


as | you would change the separator part of
the command to sep=”|”

Categories or values? Because of R’s roots


as a statistical tool, when you import non-
numerical data, R may assume that char- ■■ UCLA statistics grad student Miles Chen
acter strings are statistical factors -- things shows an RStudio point-and-click data import.
like “poor,” “average” and “good” -- or “suc-
cess” and “failure.”
Copying data snippets
But your text columns may not be catego-
If you’ve got just a small section of data
ries that you want to group and measure,
already in a table -- a spreadsheet, say, or a
just names of companies or employees. If
Web HTML table -- you can control-C copy
you don’t want your text data to be read in
those data to your Windows clipboard and
as factors, add stringsAsFactor=FALSE to
import them into R.
read.table, like this:
The command below handles clipboard
mydata <- read.table(“filename.
data with a header row that’s separated by
txt”, sep=”\t”, header=TRUE,
tabs, and stores the data in a data frame
stringsAsFactor=FALSE)
(x):
If you’d prefer, R allows you to use a series
x <- read.table(file = “clipboard”,
of menu clicks to load data instead of ‘read-
sep=”\t”, header=TRUE)
ing’ data from the command line as just
described. To do this, go to the Workspace You can read more about using the Win-
tab of RStudio’s upper-right window, find dows clipboard in R at the R For Dummies
the menu option to “Import Dataset,” then website.
choose a local text file or URL.
On a Mac, the pipe (“pbpaste”) function
As data are imported via menu clicks, will access data you’ve copied with com-
the R command that RStudio generated mand-c, so this will do the equivalent of the
from your menu clicks will appear in your previous Windows command:
console. You may want to save that data-
x <- read.table(pipe(“pbpaste”),
reading command into a script file if you’re
sep=”\t”)
using this for significant analysis work, so
that others -- or you -- can reproduce that
work. Other formats
This 3-minute YouTube video, recorded by There are R packages that will read files
UCLA statistics grad student Miles Chen, from Excel, SPSS, SAS, Stata and various
shows an RStudio point-and-click data relational databases. I don’t bother with
import. the Excel package; it requires both Java

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-

Remote data tistical analysis functions on the file.

read.csv() and read.table() work pretty


much the same to access files from the Web
Help with external data
as they do for local data. R enthusiasts have created add-on pack-
ages to help other users download data into
Do you want Google Spreadsheets data in
R with a minimum of fuss.
R? You don’t have to download the spread-
sheet to your local system as you do with a For instance, the financial analysis pack-
CSV. Instead, in your Google spreadsheet age Quantmod, developed by quantitative
-- properly formatted with just one row for software analyst Jeffrey Ryan, makes it easy
headers and then one row of data per line to not only pull in and analyze stock prices
-- select File > Publish to the Web. (This but graph them as well.
will make the data public, although only to
All you need are four short lines of code
someone who has or stumbles upon the
to install the Quantmod package, load it,
correct URL. Beware of this process, espe-
retrieve a company’s stock prices and then
cially with sensitive data.)
chart them using the barChart function.
Select the sheet with your data and click Type in and run the following in your R edi-
“Start publishing.” You should see a box tor window or console for Apple data:
with the option to get a link to the pub-
install.packages(‘quantmod’)
lished data. Change the format type from
Web page to CSV and copy the link. Now library(‘quantmod’)
you can read those data into R with a com-
getSymbols(“AAPL”)
mand such as:
barChart(AAPL)
mydata <- read.csv(“http://bit.
ly/10ER84j”) Want to see just the last couple of weeks?
You can use a command like this:
The command structure is the same for any
file on the Web. For example, Pew Research barChart(AAPL, subset=’last 14 days’)

8
R: a beginner’s guide
COMPUTERWORLD.COM

chartSeries(AAPL, subset=’last 14 save the script file when you’re finished. If


days’) you haven’t been doing that, you can find a
history of all the commands you’ve typed
Or grab a particular date range like this:
in the history tab in the top right window;
barChart(A select the ones you want and click the “to
APL[‘2013-04-01::2013-04-12’]) source” menu option to copy them into a
file in the script window for saving.
Quantmod is a very powerful financial
analysis package, and you can read more You can also save your entire workspace.
about it on the Quantmod website. While you’re in R, use the function:

There are many other packages with R save.image()


interfaces to data sources such as twit-
That stores your workspace to a file named
teR for analyzing Twitter data; Quandl
.RData by default. This will ensure you
and rdatamarket for access to millions
don’t lose all your work in the event of a
of data sets at Quandl and Data Market,
power glitch or system reboot while you’ve
respectively; and several for Google Analyt-
stepped away.
ics, including rga, RGoogleAnalytics and
ganalytics. When you close R, it asks if you want to
save your workspace. If you say yes, the
Looking for a specific type of data to pull
next time you start R that workspace will
into R but don’t know where to find it? You
be loaded. That saved file will be named
can try searching Quandl and Datamarket,
.RData as well. If you have different projects
where data can be downloaded in R format
in different directories, each can have its
even without needing to install the site-
own .RData workspace file.
specific packages mentioned above.
You can also save an individual R object for

Removing unneeded data later loading with the save function:

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”)

Saving your data


Once you’ve read in your data and set up
your objects just the way you want them,
you can save your work in several ways. It’s
a good idea to store your commands in a
script file, so you can repeat your work if
needed.

How best to save your commands? You


can type them first into the RStudio script
editor (top left window) instead of directly
into the interactive console, so you can

9
R: a beginner’s guide
COMPUTERWORLD.COM

Easy ways to do basic


data analysis
So you’ve read your data into an R object. Or:
Now what?
tail(mydata, 10)

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)

R will display mydata’s column headers and


■■ Results of the str() function on the
first 6 rows by default. Want to see, oh, the
sample data set PlantGrowth.
first 10 rows instead of 6? That’s:
For a vector, str() tells you how many items
head(mydata, n=10)
there are -- for 8 items, it’ll display as [1:8]
Or just: -- along with the type of item (number,
character, etc.) and the first few entries.
head(mydata, 10)
Various other data types return slightly dif-
Note: If your object is just a 1-dimensional
ferent results.
vector of numbers, such as (1, 1, 2, 3, 5, 8,
13, 21, 34), head(mydata) will give you the If you want to see just the column names in
first 6 items in the vector. the data frame called mydata, you can use
the command:
To see the last few rows of your data, use
the tail() function: colnames(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)

summary(mydata) and you’ll get several more statistics


from the data including standard devia-
tion, “mad” (mean absolute deviation),
skew (measuring whether or not the data
distribution is symmetrical) and kurtosis
(whether the data have a sharp or flatter
peak near its mean).

R has the statistical functions you’d expect,


including mean(), median(), min(), max(),
sd() [standard deviation], var() [vari-
ance] and range()which you can run on a
1-dimensional vector of numbers. (Several
of these functions -- such as mean() and
median() -- will not work on a 2-dimen-
■■ Results of the summary function on
sional data frame).
a data set called diamonds, which is
included in the ggplot2 add-on package. Oddly, the mode() function returns infor-
mation about data type instead of the
That returns some basic calculations for
statistical mode; there’s an add-on package,
each column. If the column has numbers,
modeest, that adds a mfv() function (most
you’ll see the minimum and maximum val-
frequent value) to find the statistical mode.
ues along with median, mean, 1st quartile
and 3rd quartile. If it’s got factors such as R also contains a load of more sophisti-
fair, good, very good and excellent, you’ll cated functions that let you do analyses
get the number of each factor listed in the with one or two commands: probability dis-
column. tributions, correlations, significance tests,
regressions, ANOVA (analysis of variance
The summary() function also returns stats
between groups) and more.
for a 1-dimensional vector.
As just one example, running the correla-
If you’d like even more statistical summa-
tion function cor() on a dataframe such as:
ries from a single command, install and

11
R: a beginner’s guide
COMPUTERWORLD.COM

cor(mydata) trim argument for mean() that lets you


exclude some outliers.
will give you a matrix of correlations for
each column of numerical data compared Not all R functions need a robust data
with every other column of numerical data. set to be useful for statistical work. For
example, how many ways can you select a
committee of 4 people from a group of 15?
You can pull out your calculator and find 15!
divided by 4! times 11! ... or you can use the
R choose() function:
■■ Results of the correlation function on
choose(15,4)
the sample data set of U.S.arrests.
Or, perhaps you want to see all of the pos-
Note: Be aware that you can run into prob-
sible pair combinations of a group of 5
lems when trying to run some functions
people, not simply count them. You can
on data where there are missing values. In
create a vector with the people’s names and
some cases, R’s default is to return NA even
store it in a variable called mypeople:
if just a single value is missing. For exam-
ple, while the summary() function returns mypeople <- c(“Bob”, “Joanne”,
column statistics excluding missing values “Sally”, “Tim”, “Neal”)
(and also tells you how many NAs are in
In the example above, c() is the combine
the data), the mean() function will return
function.
NA if even only one value is missing in a
vector. Then run the combn() function, which
takes two arguments -- your entire set first
In most cases, adding the argument:
and then the number you want to have in
na.rm=TRUE each group:

to NA-sensitive functions will tell that func- combn(mypeople, 2)


tion to remove any NAs when performing
calculations, such as:

mean(myvector, na.rm=TRUE)

If you’ve got data with some missing values,


read a function’s help file by typing a ques-
tion mark followed by the name of the func-
tion, such as:

?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:

[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4


Get slices or subsets of your 22.8 19.2 17.8

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:

To create a vector of items that are not con- mtcars[mtcars$mpg>20,c(“mpg”,”hp”)]


tiguous, you need to use the combine func-
using the column names.
tion c(). Typing mtcars[,(2,4)] without the
c will not work. You need that c in there: Why do you need to specify mtcars$mpg
in the row spot but “mpg” in the column
mtcars[,c(2,4)]
spot? Just another R syntax quirk is the
What if want to select your data by data best answer I can give you.
characteristic, such as “all cars with mpg >
If you’re finding that your selection state-
20”, and not column or row location? If you
ment is starting to get unwieldy, you can
use the column name notation and add a
put your row and column selections into
condition like:
variables first, such as:
mtcars$mpg>20
mpg20 <- mtcars$mpg > 20
you don’t end up with a list of all rows
cols <- c(“mpg”, “hp”)
where mpg is greater than 20. Instead, you
get a vector showing whether each row Then you can select the rows and columns
meets the condition, such as: with those variables:

[1] TRUE TRUE TRUE TRUE FALSE mtcars[mpg20, cols]


FALSE FALSE TRUE TRUE
making for a more compact select state-
[10] FALSE FALSE FALSE FALSE FALSE ment but more lines of code.
FALSE FALSE FALSE TRUE
Getting tired of including the name of the
[19] TRUE TRUE TRUE FALSE FALSE data set multiple times per command? If
FALSE FALSE TRUE TRUE you’re using only one data set and you

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:

So, instead of having to type: subset(mtcars, mpg==max(mpg), 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-

notation cal expression). As with subset(), column


names stand alone after the data frame
Bracket syntax is pretty common in R code, name, so mpg>20 and not mtcars$mpg >
but it’s not your only option. If you dislike 20.
that format, you might prefer the subset()
filter(mtcars, mpg>20)
function instead, which works with vectors
and matrices as well as data frames. The To choose only certain columns, you use
format is: the select() function with syntax such as
select(dataframename, columnName1,
subset(your data object, logical
columnName2). No quotation marks are
condition for the rows you want to
needed with the column names:
return, select statement for the col-
umns you want to return) select(mtcars, mpg, hp)

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

function) %.% secondfunction(argument


for second function) %.%
thirdfunction(argument for third
function)

So viewing just mpg and hp for rows where


mpg is greater than 20:

mtcars %.% filter(mpg > 20) %.%


select(mpg, hp)
■■ R’s table function returns a count
No need to keep repeating the data frame of each factor in your data.
name. To order those results from highest
If you are interested in learning more about
to lowest mpg, add the arrange() function
statistical functions in R and how to slice
to the chain with desc(columnName) for
and dice your data, there are a number of
descending order:
free academic downloads with many more
mtcars %.% filter(mpg > 20) details. These include Learning statistics
%.% select(mpg, hp) %.% with R by Daniel Navarro at the Univer-
arrange(desc(mpg)) sity of Adelaide in Australia (500+ page
PDF download, may take a little while).
You can find out more about dplyr in the
And although not free, books such as
dplyr package’s introduction vignette.
The R Cookbook and R in a Nutshell have
a lot of good examples and well-written
Counting factors explanations.

To tally up counts by factor, try the table


command. For the diamonds data set, to
see how many diamonds of each category
of cut are in the data, you can use:

table(diamonds$cut)

This will return how many diamonds of


each factor -- fair, good, very good, pre-
mium and ideal -- exist in the data. Want to
see a cross-tab by cut and color?

table(diamonds$cut, diamonds$color)

16
R: a beginner’s guide
COMPUTERWORLD.COM

Painless data visualization


One of the most appealing things about R position them for easier reading with the
is its ability to create data visualizations las=1 argument:
with just a couple of lines of code.
plot(mtcars$disp, mtcars$mpg,
For example, it takes just one line of code xlab=”Engine displacement”,
-- and a short one at that -- to plot two vari- ylab=”mpg”, main=”MPG vs engine dis-
ables in a scatterplot. Let’s use as an exam- placement”, las=1)
ple the mtcars data set installed with R by
default. To plot the engine displacement
column disp on the x axis and mpg on y:

plot(mtcars$disp, mtcars$mpg)

■■ Adding a main headline and


axes labels to an R plot.

What’s las and why is it 1? las refers to label


■■ Default scatterplot in R. style, and it’s got four options. 0 is the
default, with text always parallel to its axis.
You really can’t get much easier than that.
1 is always horizontal, 2 is always perpen-
Of course that’s a pretty no-frills graphic. If dicular to the axis and 3 is always vertical.
you’d like to label your x and y axes, use the For much more on plot parameters, run the
parameters xlab and ylab. To add a main help command on par like so:
headline, such as “Page views by time of
?par
day,” use the parameter main:
In addition to the basic dataviz function-
plot(mtcars$disp, mtcars$mpg,
ality included with standard R, there are
xlab=”Engine displacement”,
numerous add-on packages to expand R’s
ylab=”mpg”, main=”MPG compared with
visualization capabilities. Some packages
engine displacement”)
are for specific disciplines such as biosta-
If you find having the y-axis labels rotated tistics or finance; others add general visual-
90 degrees annoying (as I do), you can ization features.

17
R: a beginner’s guide
COMPUTERWORLD.COM

Why use an add-on package if you don’t


need something discipline-specific? If
you’re doing more complex dataviz, or want
to pretty up your graphics for presenta-
tions, some packages have more robust
options. Another reason: The organization
and syntax of an add-on package might
appeal to you more than do the R defaults.

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 data set you’re plotting. But what’s with


“aes()” and “geom_point()”?

“aes” stands for aesthetics -- what are


considered visual properties of the graph.
Those are things like position in space,
color and shape.

“geom” is the graphing geometry you’re


using, such as lines, bars or the shapes of
your points.

Now if “line” and “bar” also seem like


aesthetic properties to you, similar to
shape, well, you can either accept that’s
how it works or do some deep reading into
the fundamentals behind the Grammar of
Graphics. (Personally, I just take Wick-
ham’s word for it.)

Want a line graph instead? Simply swap


■■ Some scatterplots such as this don’t show out geom_point() and replace it with geom_
the full picture because one point actually rep- line() , as in this example that plots tem-
resents more than one entry in your data. perature vs pressure in R’s sample pressure
qplot(cty, hwy, data=mpg) data set:

The “jitter” geom parameter introduces just ggplot(pressure, aes(x=temperature,


a little randomness in the point placement y=pressure)) + geom_line()
so you can better see multiple points:

qplot(cty, hwy, data=mpg,


geom=”jitter”)

As you might have guessed, if there’s a


“quick plot” function in ggplot2 there’s also
a more robust, full-featured plotting func-
tion. That’s called ggplot() -- yes, while the
add-on package is called ggplot2, the func-
tion is ggplot() and not ggplot2(). ■■ Creating a line graph with ggplot2.

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

You can also use the ylim argument with


ggplot to change where the y axis starts. If
mydata is the name of your data frame, xcol
is the name of the column you want on the
x axis and ycol is the name of the column
you want on the y axis, use the ylim argu-
ment like this:

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:

ggplot(pressure, aes(x=temperature, barplot(BOD$demand, main=”Graph of


y=pressure)) + geom_line() + demand”, names.arg = BOD$Time)
geom_point()
Sometimes you’d like to graph the counts of
The point here (pun sort of intended) is a particular variable but you’ve got just raw
that you can start off with a simple graphic data, not a table of frequencies. R’s table()
and then add all sorts of customizations: function is a quick way to generate counts
Set the size, shape and color of the points, for each factor in your data.
plot multiple lines with different colors,
The R Graphics Cookbook uses an example
add labels and a ton more. See Bar and line
of a bar graph for the number of 4-, 6- and
graphs (ggplot2) for a few examples, or
8-cylinder vehicles in the mtcars data
the The R Graphics Cookbook by Winston
set. Cylinders are listed in the cyl column,
Chang for many more.
which you can access in R using mtcars$cyl.

Here’s code to get the count of how many


Bar graphs entries there are by cylinder with the
To make a bar graph from the sample table() function; it stores results in a vari-
BOD data frame included with R, the basic able called cylcount:
R function is barplot(). So, to plot the
cylcount <- table(mtcars$cyl)
demand column from the BOD data set on
a bar graph, you can use the command:

barplot(BOD$demand)

Add main=”Graph of demand” if you want


a main headline on your graph:

barplot(BOD$demand, main=”Graph of
demand”)
■■ Creating a bar plot.

That creates a table called cylcount


containing:

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(mtcars$cyl) where columnName is the name of your


column in a mydata dataframe that you
want to visualize, and n is the number of
bins you want.

The ggplot2 commands are:

qplot(columnName, data=mydata,
binwidth=n)

For quick plots and, for the more robust


ggplot():

ggplot(mydata, aes(x=columnName)) +
geom_histogram(binwidth=n)

You may be starting to see strong similari-


ties in syntax for various ggplot() examples.
While the ggplot() function is somewhat
less intuitive, once you wrap your head
■■ What happens to your bar chart when you
around its general principles, you can do
don’t instruct R not to plot continuous variables.
other types of graphics in a similar way.
However, this defaults to an assump-
tion that 4, 6 and 8 are part of a variable
set that could run from 4 through 8, so it
Additional graphics options
shows blank entries for 5 and 7. There are many more graphics types in R
than these few I’ve mentioned. Boxplots,
To treat cylinders as distinct groups -- that
a statistical staple showing minimum and
is, you’ve got a group with 4 cylinders, a
maximum, first and third quartiles and
group with 6 and a group with 8, not the
median, have their own function called,
possibility of entries anywhere between 4
intuitively, boxplot(). If you want to see a
and 8 -- you want cylinders to be treated as
boxplot of the mpg column in the mtcars
a statistical factor:
data frame it’s as simple as:
qplot(factor(mtcars$cyl))
boxplot(mtcars$mpg)
To create a bar graph with the more robust
To see side-by-side boxplots in a single
ggplot() function, you can use syntax such
plot, such as the x, y and z measurements
as:
of all the diamonds in the diamonds sam-
ggplot(mtcars, aes(factor(cyl))) + ple data set included in ggplot2:
geom_bar()

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:

Creating a heat map in R is more complex rainbow(5)


but not ridiculously so. There’s an easy-to-
For many more details, check the help com-
follow tutorial on Flowing Data.
mand on a palette such as:
You can do graphical correlation matri-
?rainbow
ces with the corrplot add-on package and
generate numerous probability distribu-
tions. See some of the links here or in the
resources section to find out more.

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:

Chances are, you’ll want to use color to barplot(testscores, col=testcolors)


show certain characteristics of your data,
Note that the name of a color must be in
as opposed to simply assigning random
quotation marks, but a variable name that
colors in a graphic. That goes a bit beyond
holds a list of colors should not be within
beginning R, but to give one example, say
quote marks.
you’ve got a vector of test scores:
Add a graph headline:
testscores <- c(96, 71, 85, 92, 82,
78, 72, 81, 68, 61, 78, 86, 90) barplot(testscores, col=testcolors,
main=”Test scores”)
You can do a simple barplot of those scores
like this: And have the y axis go from 0 to 100:

barplot(testscores) barplot(testscores, col=testcolors,


main=”Test scores”, ylim=c(0,100))
And you can make all the bars blue like this:
Then use las-1 to style the axis label to
barplot(testscores, col=”blue”)
be horizontal and not turned 90 degrees
But what if you want the scores 80 and vertical:
above to be blue and the lower scores to be
barplot(testscores, col=testcolors,
red? To do this, create a vector of colors
main=”Test scores”, ylim=c(0,100),
of the same length and in the same order
las=1)
as your data, adding a color to the vector
based on the data. In other words, since And you’ve got a color-coded bar graph.
the first test score is 96, the first color in
your color vector should be blue; since the
second score is 71, the second color in your
color vector should be red; and so on.

Of course, you don’t want to create that


color vector manually! Here’s a statement
that will do so:

testcolors <- ifelse(testscores >=


80, “blue”, “red”)
■■ A color-coded bar graph.
If you’ve got any programming experience,
you might guess that this creates a vec- By the way, if you wanted the scores sorted
tor that loops through the testscores data from highest to lowest, you could have set
and runs the conditional statement: ‘If this your original testscores variable to:

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.

■■ Coloring bars by factor.

24
R: a beginner’s guide
COMPUTERWORLD.COM

mat -- myplot.jpg saves as a JPEG, myplot.


png saves as a PNG and so on.

One final note: If you’re working in RStu-


dio and would like to see a larger version
of your plot, click the Zoom button and a
larger window with the current graphic will
open. And, also in RStudio, to see previ-
ous plots from your session, click the back
arrow.

■■ Exporting your graphics from RStudio.

If you are using “plain” R in Windows, you


can also right-click the graphics window to
save the file.

To save a plot with R commands and not


point-and-click, first create a container for
your image using a function such as jpeg(),
png(), svg() or pdf(). Those functions need
to have a file name as one argument and
optionally width and height, such as:

jpeg(“myplot.jpg”, width=350,
height=420)

Generate the plot with whatever graphics


functions it requires, such as:

barplot(BOD$demand, col=rainbow(6))

And then issue the command:

dev.off()

That will save your graphic to its container.

If you are using ggplot2, you can also


use the function ggsave(), which defaults
to saving the last plot you created using
ggplot2 at the size you displayed it. Based
on the filename you pass to the ggsave()
function, it will save in the appropriate for-

25
R: a beginner’s guide
COMPUTERWORLD.COM

Syntax quirks you’ll


want to know
I mentioned at the outset that R syntax know the ins and outs of R’s 5 -- yes, count
is a bit quirky, especially if your frame of ‘em, 5 -- assignment options, check out the
reference is, well, pretty much any other R manual’s Assignment Operators page.)
programming language. Here are some
One more note about variables: R is a case-
unusual traits of the language you may find
sensitive language. So, variable x is not the
useful to understand as you embark on your
same as X. That applies to pretty much
journey to learn R.
everything in R; for example, the function
subset() is not the same as Subset().
Assigning values to variables
In pretty much every other programming c is for combine (or
language I know, the equals sign assigns a
certain value to a variable. You know, x = 3
concatenate, and sometimes
means that x now holds the value of 3. convert/coerce.)
Not in R. At least, not necessarily. When you create an array in most program-
ming languages, the syntax goes something
In R, the primary assignment operator is
like this:
<- as in:
myArray = array(1, 1, 2, 3, 5, 8);
x <- 3
Or:
But not:
int myArray = {1, 1, 2, 3, 5, 8};
x = 3
Or maybe:
To add to the potential confusion, the
equals sign actually can be used as an myArray = [1, 1, 2, 3, 5, 8]
assignment operator in R -- but not all the
In R, though, there’s an extra piece: To put
time. When can you use it and when can
multiple values into a single variable, you
you not?
need the c() function, such as:
The best way for a beginner to deal with
my_vector <- c(1, 1, 2, 3, 5, 8)
this is to use the preferred assignment
operator <- and forget that equals is If you forget that c, you’ll get an error.
ever allowed. Hey, if it’s good enough for When you’re starting out in R, you’ll prob-
Google’s R style guide -- they advise not ably see errors relating to leaving out that
using equals to assign values to variables -- c() a lot. (At least I certainly did.)
it’s good enough for me.
And now that I’ve stressed the importance
(If this isn’t a good enough explanation for of that c() function, I (reluctantly) will tell
you, however, and you really really want to you that there’s a case when you can leave

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)

A matrix needs to have all the same data


type in every column, such as numbers
everywhere.

Data frames are like matrices except one


column can have a different data type from
another column, and each column must
■■ If you’ve got a vector with lots of val-
have a name. If you’ve got data in a format
ues so the printout runs across multiple
that might work well as a database table (or
lines, each line will start with a number in
well-formed spreadsheet table), it will also
brackets, telling you which vector item num-
probably work well as an R data frame.
ber that particular line is starting with.
In a data frame, you can think of each row
If you want to mix numbers and strings
as similar to a database record and each
or numbers and TRUE/FALSE types, you
column like a database field. There are lots
need a list. (If you don’t create a list, you
of useful functions you can apply to data
may be unpleasantly surprised that your
frames, some of which I’ve gone over in
variable containing (3, 8, “small”) was
earlier sections, such as summary() and the
turned into a vector of characters (“3”, “8”,
psych package’s describe().
“small”) ).
And speaking of quirks: There are several
And by the way, R assumes that 3 is the
ways to find an object’s underlying data
same class as 3.0 -- numeric (i.e., with
type, but not all of them return the same
a decimal point). If you want the integer
value. For example, class() and str() will
3, you need to signify it as 3L or with the
return data. frame on a data frame object,
as.integer() function. In a situation where
but mode() returns the more generic list.
this matters to you, you can check what
type of number you’ve got by using the If you’d like to learn more details about
class() function: data types in R, you can watch this video
lecture by Roger Peng, associate profes-
class(3)
sor of biostatistics at the Johns Hopkins
class(3.0) Bloomberg School of Public Health:

class(3L)

class(as.integer(3))

There are several as() functions for con-


verting one data type to another, including
as.character(), as.list() and as.data.frame().

29
R: a beginner’s guide
COMPUTERWORLD.COM

provide a list of objects in your current R


session.

Want to see your current working direc-


tory? pwd just throws an error; what you
want is getwd().

rm(my_variable) will delete a variable from


your current session.

R does include a Unix-like grep() function.


For more on using grep in R, see this brief
■■ Roger Peng, associate professor of biosta- writeup on Regular Expressions with The R
tistics at the Johns Hopkins Bloomberg School Language at regular-expressions.info.
of Public Health, explains data types in R.

One more useful concept to wrap up this Terminating your R


section -- hang in there, we’re almost
done: factors. These represent categories
expressions
in your data. So, if you’ve got a data frame R doesn’t need semicolons to end a line of
with employees, their department and code (although it’s possible to put multiple
their salaries, salaries would be numerical commands on a single line separated by
data and employees would be characters semicolons, you don’t see that very often).
(strings in many other languages); but Instead, R uses line breaks (i.e., new line
you’d likely want department to be a factor characters) to determine when an expres-
-- in other words, a category you may want sion has ended.
to group or model your data by. Factors
What if you want one expression to go
can be unordered, such as department, or
across multiple lines? The R interpreter
ordered, such as “poor”, “fair”, “good” and
tries to guess if you mean for it to continue
“excellent.”
to the next line: If you obviously haven’t
finished a command on one line, it will
R command line differs from assume you want to continue instead of

the Unix shell throwing an error. Open some parenthe-


ses without closing them, use an open
When you start working in the R environ- quote without a closing one or end a line
ment, it looks quite similar to a Unix shell. with an operator like + or - and R will wait
In fact, some R command-line actions to execute your command until it comes
behave as you’d expect if you come from a across the expected closing character and
Unix environment, but others don’t. the command otherwise looks finished.

Want to cycle through your last few com-


mands? The up arrow works in R just as it Syntax cheating: Run SQL
does in Unix -- keep hitting it to see prior
commands.
queries in R
If you’ve got SQL experience and R syntax
The list function, ls(), will give you a list,
starts giving you a headache -- especially
but not of files as in Unix. Rather, it will
when you’re trying to figure out how to get

30
R: a beginner’s guide
COMPUTERWORLD.COM

a subset of data with proper R syntax -- you


might start longing for the ability to run a
quick SQL SELECT command query your
data set.

You can.

The add-on package sqldf lets you run


SQL queries on an R data frame (there are
separate packages allowing you to connect
R with a local database). Install and load
sqldf, and then you can issue commands
such as:

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.

And speaking of cheating, if you don’t want


to use the command line to examine and Saving and exporting your
edit your data, R has a couple of options. data
The edit() function brings up an editor
where you can look at and edit an R object, In addition to saving your entire R work-
such as space with the save.image() function and
various ways to save plots to image files,
edit(mtcars) you can save individual objects for use in
other software. For example, if you’ve got a
data frame just so and would like to share it
with colleagues as a tab- or comma-delim-
ited file, say for importing into a spread-
sheet, you can use the command:

31
R: a beginner’s guide
COMPUTERWORLD.COM

write.table(myData, “testfile.txt”,
sep=”\t”)

This will export all the data from an R


object called myData to a tab-separated file
called testfile.txt in the current working
directory. Changing sep=”\t” to sep=”c” will
generated a comma-separated file and so
on.

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

Statistical Analysis Ten Things You Can


general R book or ebook Do in R That You for Excel
with R online reference
Wouldve Done in users
Introduction to Data data Microsoft Excel
ebook
Science analysis
R Reference Card for data
Reproducible PDF
Reports Data Mining mining
Research with R and book or ebook
in R Spatial Cheat Sheet geospatial online reference
RStudio
Exploring Everyday Web interface for graphics-
online tool
Things with R and general R book or ebook ggplot2 ggplot2
Ruby R Tutorial general R online tutorials
Visualize This graphics book or ebook r4stats.com general R online tutorials
Statistics and R on How to Visualize and
general R community graphics online tutorials
Google+ Compare Distributions
#rstats hashtag general R community Getting Started with
graphics online tutorials
Stackoverflow general R community Charts in R

33
R: a beginner’s guide
COMPUTERWORLD.COM

Resource Topic Type Resource Topic Type


Producing Simple R: A Self-learn
graphics online tutorials general R PDF
Graphs with R Tutorial
graphics- Introduction to graphics-
Quick Intro to ggplot2 online tutorials PDF
ggplot2 ggplot2 ggplot2
Introducing R general R online tutorials Statistics with R Com-
general R PDF
Using R general R online tutorials puting and Graphics

Aggregating and data Using R for your Basic


online tutorials general R R code
restructuring data reshaping Statistical Needs

Higher Order Func- R pro- Short Courses by general R, R code and


online tutorials Hadley Wickham graphics slides
tions in R gramming
Introductory Econo- R code and
Introducing R general R
metrics Using Quandl statistics online tutorials slides
and R Rseek general R search
graphics- R site search general R search
ggplot2 Guide online tutorials
ggplot2 search,
R mailing list search general R
r4stats.com general R online tutorials community
Introduction to dplyr general R online tutorials R pro-
RStudio IDE software
Applied Time Series time gramming
online tutorials
Analysis series R pro-
Revolution R software
13 resources for time time gramming
online tutorials
series analysis series Enterprise Runtime R pro-
software
repro- for R gramming
knitr in a knutshell ducible online tutorials interac-
Shiny for interactive
research tive software
Web apps
The Undergraduate PDF or Google graphics
general R
Guide to R Doc Swirl statistics software
Little Book of R for time R pro-
online tutorials R Style Guides style guide
Time Series series gramming
ggplot2 workshop graphics- Up and Running with
online tutorials general R video class
presentation ggplot2 R
graphics- Computing for Data
ggplot2_tutorial.R online tutorials general R video class
ggplot2 Analysis
More and Fancier data
graphics online tutorials Data Analysis video class
Graphics analysis
How to turn CSV Statistics One statistics video class
data into interactive Twotorials general R video tutorials
graphics online tutorials
visualizations with R
and rCharts Google Developers'
general R video tutorials
Intro to R
R Reference Card general R PDF
Introduction to Data general R,
Introduction to R general R PDF video tutorials
Science with R ggplot2
Handling and Process- Data Analysis and general R,
text in R PDF video tutorials
ing Strings in R Visualization Using R statistics
Learning Statistics Programming in R at
statistics PDF general R website
with R Dummies.com

34
R: a beginner’s guide
COMPUTERWORLD.COM

Resource Topic Type R Graphics Cookbook. If you want to do


beyond-the-basics graphics in R, this is a
R-bloggers general R blog
useful resource both for its graphics recipes
Revolutions general R blog
and brief introduction to ggplot2. While
10 R Packages I Wish I R this goes way beyond the graphics capabili-
blog post
Knew About Earlier packages
ties that I need in R, I’d recommend this if
R programming for you’re looking to move beyond advanced-
R pro-
those coming from blog post
gramming beginner plotting. By Winston Chang, a
other languages
software engineer at RStudio.
A brief introduction to
general R blog post
'apply' in R R in Action: Data analysis and graphics
History of R Financial with R. This book aims at all levels of users,
graphics blog post
Time Series Plotting
with sections for beginning, intermediate
Translating between R and advanced R ranging from “Exploring
general R blog post
and SQL
R data structures” to running regressions
Graphs & Charts in and conducting factor analyses. The begin-
base R, ggplot2 and graphics blog post
rCharts ner’s section may be a bit tough to follow
if you haven’t had any exposure to R, but
When to use Excel, for Excel
blog post it offers a good foundation in data types,
when to use R? users
A First Step Towards R for Excel imports and reshaping once you’ve had a
blog post bit of experience. There are some particu-
From Spreadsheets users
Using dates and times R pro- larly useful explanations and examples for
blog post
in R gramming aggregating, restructuring and subsetting
Scraping Pro-Football data, as well as a lot of applied statistics.
Data and Interactive Note that if your interest in graphics is
graphics blog post
Charts using rCharts, learning ggplot2, there’s relatively little
ggplot2, and shiny
on that here compared with base R graph-
Grouping & Summa- slide ics and the lattice package. You can see
general R
rizing Data in R presentation
an excerpt from the book online: Aggrega-
R Instructor general R app
tion and restructuring data. By Robert I.
Kabacoff.

Books and e-books The Art of R Programming. For those who


want to move beyond using R “in an ad hoc
R Cookbook. Like the rest of the O’Reilly way ... to develop[ing] software in R.” This
Cookbook series, this one offers how-to is best if you’re already at least moderately
“recipes” for doing lots of different tasks, proficient in another programming lan-
from the basics of R installation and creat- guage. It’s a good resource for systemati-
ing simple data objects to generating prob- cally learning fundamentals such as types
abilities, graphics and linear regressions. It of objects, control statements (unlike many
has the added bonus of being well written. R purists, the author doesn’t actively dis-
If you like learning by example or are seek- courage for loops), variable scope, classes
ing a good R reference book, this is well and debugging -- in fact, there’s nearly as
worth adding to your reference library. By large a chapter on debugging as there is on
Paul Teetor, a quantitative developer work- graphics. With some robust examples of
ing in the financial sector.

35
R: a beginner’s guide
COMPUTERWORLD.COM

solving real-world statistical problems in R. rates (including a helpful section on how to


By Norman Matloff. get Twitter OAuth authorization working in
R), simple map mashups and basic linear
R in a Nutshell. A reasonably readable
regression. Although Stanton calls this an
guide to R that teaches the language’s
“electronic textbook,” Introduction to Data
fundamentals -- syntax, functions, data
Science has a conversational style that’s
structures and so on -- as well as how-to
pleasantly non-textbook like. There used to
statistical and graphics tasks. Useful if you
be a downloadable PDF, but now the only
want to start writing robust R programs, as
versions are for OS X or iOS.
it includes sections on functions, object-
oriented programming and high-perfor- R for Everyone. Author Jared P. Lander
mance R. By Joseph Adler, a senior data promises to go over “20% of the function-
scientist at LinkedIn. ality needed to accomplish 80% of the
work.” And in fact, topics that are actually
Visualize This. Note; Most of this book is
covered, are covered pretty well; but be
not about R, but there are several examples
warned that some items appearing in the
of visualizing data with R. And there’s so
table of contents can be a little thin. This
much other interesting info here about how
is still a well-organized reference, though,
to tell stories with data that it’s worth a
with information that beginning and inter-
read. By Nathan Yau, who runs the popular
mediate users might want to know: import-
Flowing Data blog and whose doctoral dis-
ing data, generating graphs, grouping and
sertation was on “personal data collection
reshaping data, working with basic stats
and how we can use visualization to learn
and more.
about ourselves.”
Statistical Analysis With R: Beginner’s
R For Dummies. I haven’t had a chance to
Guide. This book has you “pretend” you’re
read this one, but it’s garnered some good
a strategist for an ancient Chinese king-
reviews on Amazon.com. If you’re familiar
dom analyzing military strategies with R.
with the Dummies series and have found
If you find that idea hokey, move along to
them helpful in the past, you might want to
see another resource; if not, you’ll get a
check this one out. You can get a taste of
beginner-level introduction to various tasks
the authors’ style in the Programming in R
in R, including tasks you don’t always see
section of Dummies.com, which has more
in an intro text, such as multiple linear
than a 100 short sections such as How to
regressions and forecasting. Note: My early
construct vectors in R and How to use the
e-version had a considerable amount of bad
apply family of functions in R. By Joris
spaces in my Kindle app, but it was still
Meys and Andrie de Vries.
certainly readable and usable.
Introduction to Data Science. It’s highly
Reproducible Research with R and RStudio.
readable, packed with useful examples and
Although categorized as a “bioinformat-
free -- what more could you want? This
ics” textbook (and priced that way - even
e-book isn’t technically an “R book,” but it
the Kindle edition is more than $50), this
uses R for all of its examples as it teaches
is more general advice on steps to make
concepts of data analysis. If you’re familiar
sure you can document and present your
with that topic you may find some of the
work. This includes numerous sections on
explanations rather basic, but there’s still a
creating report documents using the knitr
lot of R code for things like analyzing tweet
package, LaTeX and Markdown -- tasks not

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

with downloadable code. All are made with


ggplot2 based on visualization ideas in Cre-
Online tools
ating More Effective Graphs. Maintained by Web interface for ggplot2. This online
Joanna Zhao and Jennifer Bryan. tool by UCLA Ph.D. candidate Jeroen
Ooms creates an interactive front end for
Beautiful Plotting in R: A ggplot2 Cheat-
ggplot2, allowing users to input tasks they
sheet. Easy to read with a lot of useful
want to do and get a plot plus R code in
information, from starting with default
return. Useful for those who want to learn
plots to customizing title, axes, legends;
more about using ggplot2 for graphics
creating multi-panel plots and more. By
without having to read through lengthy
Zev Ross.
documentation.
Frequently Asked Questions about R. Some
Ten Things You Can Do in R That You
basics about reading, writing, sorting and
Wouldve Done in Microsoft Excel. From
shaping data as well as a lineup of how to
the R for Dummies Web site, these code
do various statistical operations and a few
samples aim to help Excel users feel more
specialized graphics such as spaghetti plots.
comfortable with R.
From UCLA’s Institute for Digital Research
and Education.

R Reference Card for Data Mining. This


Videos
is a task-oriented compilation of useful R Twotorials. You’ll either enjoy these snappy
packages and functions for things ranging 2-minute “twotorial” videos or find them, oh,
from text mining and time series analysis corny or over the top. I think they’re both
to more general subjects like graphics and informative and fun, a welcome antidote to
data manipulation. Since decriptions are the typically dry how-to’s you often find in
somewhat bare-boned, this will likely be statistical programming. Analyst Anthony
more useful to either remind you of func- Damico takes on R in 2-minute chunks,
tions you’ve seen before or give you sug- from “how to create a variable with R” to
gestions for things to try. For much more “how to plot residuals from a regression in
on the subject, head to the author’s R and R;” he also tackles an occasional problem
Data Mining website, which includes exam- such as “how to calculate your ten, fifteen,
ples and other documentation. including a or twenty thousandth day on earth with R.”
substantial portion of his book R and Data I’d strongly recommend giving this a look if
Mining published by Elsevier in 2012. By textbook-style instruction leaves you cold.
Yanchang Zhao.

Spatial Cheat Sheet. For those doing GIS


and spatial analysis work, this list offers
some key functions and packages for work-
ing with spatial vector and raster data. By
Barry Stephen Rowlingson at Lancaster
University in the U.K.

■■ Sample “Twotorial” video.

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.

■■ Intro video for the Coursera Com-


puting for Data Analysis course

Coursera: Data Analysis. This was more


of an applied statistics class that uses R as
■■ This video in the Google Develop-
opposed to one that teaches R; but if you’ve
ers’ R series introduces functions in R.
got the R basics down and want to see it in
Up and Running with R. This lynda.com action, this might be a good choice. There
video class covers the basics of topics are no upcoming scheduled sessions for
such as using the R environment, read- this at Coursera, but instructor Jeff Leek
ing in data, creating charts and calculating -- an assistant professor of biostatistics at
statistics. The curriculum is limited, but Johns Hopkins, posted his lecture videos
presenter Barton Poulson tries to explain on YouTube, and Revolution Analytics col-
what he’s doing and why, not simply run lected links to them all by week.
commands. He also has a more in-depth
6-hour class, R Statistics Essential Train-
ing. Lynda.com is a subscription service
that starts at $25/month, but several of the
videos are available free for you to view and
see if you like the instruction style, and
there’s a 7-day free trial available.

Coursera: Computing for Data Analysis.


Coursera’s free online classes are time-
sensitive: You’ve got to enroll while they’re
taking place or you’re out of luck. However,
■■ Intro video for Coursera Data
if there’s no session starting soon, instruc-
Analysis online class

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

and tutorials regression, confidence intervals and p-val-


ues. By Kelly Black, associate professor at
Try R This beginner-level interactive online Clarkson University.
course will probably seem somewhat basic
r4stats.com. This site is probably best
for anyone who has experience in another
known in the R community for author Bob
programming language. However, even if
Muenchen’s tracking of R’s popularity vs.
the focus on pirates and plunder doesn’t
other statistical software. However, in the
appeal to you, it may be a good way to get
Examples section, he’s got some R tutorials

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

and adding graphics and tables, along with


links to resources for more info.
Communities
Pretty much every social media plat-
More free downloads and form has an R group. I’d particularly
recommend:
websites from academia:
Statistics and R on Google+. Community
Introducing R. Slide presentation from the members are knowledgeable and helpful,
UCLA Institute for Digital Research and and various conversation threads engage
Education, with downloadable data and both newbies and experts.
code.
Twitter #rstats hashtag. Level of discourse
Introducing R. Although titled for begin- here ranges from beginner to extremely
ners and including sections on getting advanced, with a lot of useful R resources
started and reading data, this also shows and commentary getting posted.
how to use R for various types of linear
You can also find R groups on Linke-
models. By German Rodriguez at Princeton
dIn, Reddit and Facebook, among other
University’s Office of Population Research.
platforms.
R: A self-learn tutorial. Intro PDF from
Stackoverflow has a very active R commu-
National Center for Ecological Analysis and
nity where people ask and answer coding
Synthesis at UC Santa Barbara. While a bit
questions. If you’ve got a specific coding
dry, it goes over a lot of fundamentals and
challenge, it’s definitely worth searching
includes exercises.
here to see if someone else has already
Statistics with R Computing and Graphics. asked about something similar.
Unlike many PDF downloads from aca-
There are dozens of R User Meetups world-
demia, this one is both short (15 pages) and
wide. In addition, there are other user
basic, with some suggested informal exer-
groups not connected with Meetup.com.
cises as well as explanations on things like
Revolution Analytics has an R User Group
getting data into R and statistical modeling
Directory.
(understanding statistical concepts like lin-
ear modeling is assumed). By Kjell Konis,
then at the University of Oxford. Blogs & blog posts
Little Book of R for Time Series. This is R-bloggers. This site aggregates posts
extremely useful if you want to use R for and tutorials from more than 250 R blogs.
analyzing data collected over time, and also While both skill level and quality can vary,
has some introductory sections for general this is a great place to find interesting posts
R use even if you’re not doing time series. about R -- especially if you look at the “top
By Avril Coghlan at the Wellcome Trust articles of the week” box on the home page.
Sanger Instituie, Cambridge, U.K.
Revolutions. There’s plenty here of inter-
Introduction to ggplot2. 11-page PDF with est to all levels of R users. Although author
some ggplot basics, by N. Matloff at UC Revolution Analytics is in the business of
Davis. selling enterprise-class R platforms, the
blog is not focused exclusively on their
products.

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.

A First Step Towards R From Spreadsheets.


Some advice on both when and how to start Misc
moving from Excel to R, with a link to a Google’s R Style Guide. Want to write neat
follow-up post, From spreadsheet thinking code with a consistent style? You’ll prob-
to R thinking. ably want a style guide; and Google has
helpfully posted their internal R style for

44
R: a beginner’s guide
COMPUTERWORLD.COM

all to use. If that one doesn’t work for you,


Hadley Wickham has a fairly abbreviated R
Software
style guide based on Google’s but “with a Comprehensive R Archive Network
few tweaks.” (CRAN). The most important of all: home
of the R Project for Statistical Computing,
RStudio documentation. If you’re using
including downloading the basic R plat-
RStudio, it’s worth taking a look at parts
form, FAQs and tutorials as well as thou-
of the documentation at some point so you
sands of add-on packages. Also features
can take advantage of all it has to offer.
detailed documentation and a number of
History of R Financial Time Series Plotting. links to more resources.
Although, as the name implies, this focuses
RStudio. You can download the free RStu-
on financial time-series graphics, it’s also
dio IDE as well as RStudio’s Shiny project
a useful look at various options for plot-
aimed at turning R analyses into interactive
ting any data over time. With lots of code
Web applications.
samples along with graphics. By Timely
Portfolio on GitHub. Revolution Analytics. In addition to its
commercial Revolution R Enterprise, you
Grouping & Summarizing Data in R. There
can request a download of their free Revo-
are so many ways to do these tasks in R
lution R Community (you’ll need to provide
that it can be a little overwhelming even for
an email address). Both are designed to
those beyond the beginner stage to decide
improve R performance and reliability.
which to use when. This downloadable
Slideshare presentation by analyst Jeffrey Tibco. This software company recently
Breen from the Greater Boston useR Group released a free Tibco Enterprise Runtime
is a useful overview. for R Developers Edition to go along with
its commercial Tibco Enterprise Runtime
for R engine aimed at helping to integrate R
Apps analysis into other enterprise platforms.
R Instructor. This app is primarily a well-
Shiny for interactive Web apps. This open-
designed, very thorough index to R, offer-
source project from RStudio is aimed at
ing snippets on how to import, summarize
creating interactive Web applications from
and plot data, as well as an introductory
R analysis and graphics. There’s a Shiny
section. An “I want to...” section gives
tutorial at the RStudio site; to see more
short how-to’s on a variety of tasks such
examples, Show Me Shiny offers a gallery
as changing data classes or column/row
of apps with links to code.
names, ordering or subsetting data and
more. Similar information is available free Swirl. This R package for interactive learn-
online; the value-add is if you want the ing teaches basic statistics and R together.
info organized in an attractive mobile app. See more info on version 2.0.
Extras include instructional videos and a
“statistical tests” section explaining when
to use various tests as well as R code for
each. For iOS and Android, about $5.

45

You might also like