R Programming Basics Slides
R Programming Basics Slides
1 / 16
What is R?
What is R?
2 / 16
What is R?
3 / 16
What is S?
S is a language that was developed by John Chambers and others at Bell Labs.
S was initiated in 1976 as an internal statistical analysis environment—originally
implemented as Fortran libraries.
Early versions of the language did not contain functions for statistical modeling.
In 1988 the system was rewritten in C and began to resemble the system that we
have today (this was Version 3 of the language). The book Statistical Models in S
by Chambers and Hastie (the white book) documents the statistical analysis
functionality.
Version 4 of the S language was released in 1998 and is the version we use today.
The book Programming with Data by John Chambers (the green book)
documents this version of the language.
4 / 16
Historical Notes
In 1993 Bell Labs gave StatSci (now Insightful Corp.) an exclusive license to
develop and sell the S language.
In 2004 Insightful purchased the S language from Lucent for $2 million and is the
current owner.
In 2006, Alcatel purchased Lucent Technologies and is now called Alcatel-Lucent.
Insightful sells its implementation of the S language under the product name
S-PLUS and has built a number of fancy features (GUIs, mostly) on top of
it—hence the “PLUS”.
In 2008 Insightful is acquired by TIBCO for $25 million
The fundamentals of the S language itself has not changed dramatically since
1998.
In 1998, S won the Association for Computing Machinery’s Software System
Award.
5 / 16
S Philosophy
6 / 16
Back to R
1991: Created in New Zealand by Ross Ihaka and Robert Gentleman. Their
experience developing R is documented in a 1996 JCGS paper.
1993: First announcement of R to the public.
1995: Martin Mächler convinces Ross and Robert to use the GNU General Public
License to make R free software.
1996: A public mailing list is created (R-help and R-devel)
1997: The R Core Group is formed (containing some people associated with
S-PLUS). The core group controls the source code for R.
2000: R version 1.0.0 is released.
2012: R version 2.15.1 is released on June 22, 2012.
7 / 16
Features of R
Syntax is very similar to S, making it easy for S-PLUS users to switch over.
Semantics are superficially similar to S, but in reality are quite different (more on
that later).
Runs on almost any standard computing platform/OS (even on the PlayStation 3)
Frequent releases (annual + bugfix releases); active development.
8 / 16
Features of R (cont’d)
Quite lean, as far as software goes; functionality is divided into modular packages
Graphics capabilities very sophisticated and better than most stat packages.
Useful for interactive work, but contains a powerful programming language for
developing new tools (user −→ programmer)
Very active and vibrant user community; R-help and R-devel mailing lists and
Stack Overflow
9 / 16
Features of R (cont’d)
It’s free!
(Both in the sense of beer and in the sense of speech.)
10 / 16
Free Software
11 / 16
Drawbacks of R
12 / 16
Design of the R System
13 / 16
Design of the R System
14 / 16
Some R Resources
15 / 16
Some Useful Books on S/R
Standard texts
Chambers (2008). Software for Data Analysis, Springer. (your textbook)
Chambers (1998). Programming with Data, Springer.
Venables & Ripley (2002). Modern Applied Statistics with S, Springer.
Venables & Ripley (2000). S Programming, Springer.
Pinheiro & Bates (2000). Mixed-Effects Models in S and S-PLUS, Springer.
Murrell (2005). R Graphics, Chapman & Hall/CRC Press.
Other resources
Springer has a series of books called Use R!.
A longer list of books is at http://www.r-project.org/doc/bib/R-books.html
16 / 16
Introduction to the R Language
Data Types and Basic Operations
1 / 27
Objects
2 / 27
Numbers
3 / 27
Attributes
4 / 27
Entering Input
At the R prompt we type expressions. The <- symbol is the assignment operator.
> x <- 1
> print(x)
[1] 1
> x
[1] 1
> msg <- "hello"
The # character indicates a comment. Anything to the right of the # (including the #
itself) is ignored.
5 / 27
Evaluation
When a complete expression is entered at the prompt, it is evaluated and the result of
the evaluated expression is returned. The result may be auto-printed.
6 / 27
Printing
7 / 27
Creating Vectors
8 / 27
Mixing Objects
When different objects are mixed in a vector, coercion occurs so that every element in
the vector is of the same class.
9 / 27
Explicit Coercion
Objects can be explicitly coerced from one class to another using the as.* functions, if
available.
10 / 27
Explicit Coercion
11 / 27
Matrices
Matrices are vectors with a dimension attribute. The dimension attribute is itself an
integer vector of length 2 (nrow, ncol)
12 / 27
Matrices (cont’d)
13 / 27
Matrices (cont’d)
Matrices can also be created directly from vectors by adding a dimension attribute.
14 / 27
cbind-ing and rbind-ing
15 / 27
Lists
Lists are a special type of vector that can contain elements of different classes. Lists
are a very important data type in R and you should get to know them well.
> x <- list(1, "a", TRUE, 1 + 4i)
> x
[[1]]
[1] 1
[[2]]
[1] "a"
[[3]]
[1] TRUE
[[4]]
[1] 1+4i
16 / 27
Factors
Factors are used to represent categorical data. Factors can be unordered or ordered.
One can think of a factor as an integer vector where each integer has a label.
Factors are treated specially by modelling functions like lm() and glm()
Using factors with labels is better than using integers because factors are
self-describing; having a variable that has values “Male” and “Female” is better
than a variable that has values 1 and 2.
17 / 27
Factors
18 / 27
Factors
The order of the levels can be set using the levels argument to factor(). This can
be important in linear modelling because the first level is used as the baseline level.
19 / 27
Missing Values
20 / 27
Missing Values
21 / 27
Data Frames
22 / 27
Data Frames
23 / 27
Names
R objects can also have names, which is very useful for writing readable code and
self-describing objects.
24 / 27
Names
$b
[1] 2
$c
[1] 3
25 / 27
Names
And matrices.
26 / 27
Summary
Data Types
atomic classes: numeric, logical, character, integer, complex
vectors, lists
factors
missing values
data frames
names
27 / 27
Introduction to the R Language
Data Types and Basic Operations
1 / 14
Subsetting
There are a number of operators that can be used to extract subsets of R objects.
[ always returns an object of the same class as the original; can be used to select
more than one element (there is one exception)
[[ is used to extract elements of a list or a data frame; it can only be used to
extract a single element and the class of the returned object will not necessarily be
a list or data frame
$ is used to extract elements of a list or data frame by name; semantics are
similar to hat of [[.
2 / 14
Subsetting
3 / 14
Subsetting a Matrix
Matrices can be subsetted in the usual way with (i, j) type indices.
> x[1, ]
[1] 1 3 5
> x[, 2]
[1] 3 4
4 / 14
Subsetting a Matrix
5 / 14
Subsetting a Matrix
Similarly, subsetting a single column or a single row will give you a vector, not a matrix
(by default).
6 / 14
Subsetting Lists
> x[[1]]
[1] 1 2 3 4
> x$bar
[1] 0.6
> x[["bar"]]
[1] 0.6
> x["bar"]
$bar
[1] 0.6
7 / 14
Subsetting Lists
$baz
[1] "hello"
8 / 14
Subsetting Lists
The [[ operator can be used with computed indices; $ can only be used with literal
names.
9 / 14
Subsetting Nested Elements of a List
10 / 14
Partial Matching
11 / 14
Removing NA Values
12 / 14
Removing NA Values
What if there are multiple things and you want to take the subset with no missing
values?
13 / 14
Removing NA Values
> airquality[1:6, ]
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
> good <- complete.cases(airquality)
> airquality[good, ][1:6, ]
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
7 23 299 8.6 65 5 7 14 / 14
Introduction to the R Language
Vectorized Operations
1/3
Vectorized Operations
Many operations in R are vectorized making code more efficient, concise, and easier to
read.
> x <- 1:4; y <- 6:9
> x + y
[1] 7 9 11 13
> x > 2
[1] FALSE FALSE TRUE TRUE
> x >= 2
[1] FALSE TRUE TRUE TRUE
> y == 8
[1] FALSE FALSE TRUE FALSE
> x * y
[1] 6 14 24 36
> x / y
[1] 0.1666667 0.2857143 0.3750000 0.4444444
2/3
Vectorized Matrix Operations
3/3
Introduction to the R Language
Reading and Writing Data
1 / 22
Reading Data
2 / 22
Writing Data
3 / 22
Reading Data Files with read.table
The read.table function is one of the most commonly used functions for reading
data. It has a few important arguments:
file, the name of a file, or a connection
header, logical indicating if the file has a header line
sep, a string indicating how the columns are separated
colClasses, a character vector indicating the class of each column in the dataset
nrows, the number of rows in the dataset
comment.char, a character string indicating the comment character
skip, the number of lines to skip from the beginning
stringsAsFactors, should character variables be coded as factors?
4 / 22
read.table
For small to moderately sized datasets, you can usually call read.table without
specifying any other arguments
R will automatically
skip lines that begin with a #
figure out how many rows there are (and how much memory needs to be
allocated)
figure what type of variable is in each column of the table
Telling R all these things directly makes R run faster and more efficiently.
read.csv is identical to read.table except that the default separator is a
comma.
5 / 22
Reading in Larger Datasets with read.table
With much larger datasets, doing the following things will make your life easier and
will prevent R from choking.
Read the help page for read.table, which contains many hints
Make a rough calculation of the memory required to store your dataset. If the
dataset is larger than the amount of RAM on your computer, you can probably
stop right here.
Set comment.char = "" if there are no commented lines in your file.
6 / 22
Reading in Larger Datasets with read.table
Use the colClasses argument. Specifying this option instead of using the default
can make ’read.table’ run MUCH faster, often twice as fast. In order to use this
option, you have to know the class of each column in your data frame. If all of
the columns are “numeric”, for example, then you can just set colClasses =
"numeric". A quick an dirty way to figure out the classes of each column is the
following:
initial <- read.table("datatable.txt", nrows = 100)
classes <- sapply(initial, class)
tabAll <- read.table("datatable.txt",
colClasses = classes)
Set nrows. This doesn’t make R run faster but it helps with memory usage. A
mild overestimate is okay. You can use the Unix tool wc to calculate the number
of lines in a file.
7 / 22
Know Thy System
In general, when using R with larger datasets, it’s useful to know a few things about
your system.
How much memory is available?
What other applications are in use?
Are there other users logged into the same system?
What operating system?
Is the OS 32 or 64 bit?
8 / 22
Calculating Memory Requirements
I have a data frame with 1,500,000 rows and 120 columns, all of which are numeric
data. Roughly, how much memory is required to store this data frame?
9 / 22
Textual Formats
dumping and dputing are useful because the resulting textual format is edit-able,
and in the case of corruption, potentially recoverable.
Unlike writing out a table or csv file, dump and dput preserve the metadata
(sacrificing some readability), so that another user doesn’t have to specify it all
over again.
Textual formats can work much better with version control programs like
subversion or git which can only track changes meaningfully in text files
Textual formats can be longer-lived; if there is corruption somewhere in the file, it
can be easier to fix the problem
Textual formats adhere to the “Unix philosophy”
Downside: The format is not very space-efficient
10 / 22
dput-ting R Objects
Another way to pass data around is by deparsing the R object with dput and reading it
back in using dget.
11 / 22
Dumping R Objects
Multiple objects can be deparsed using the dump function and read back in using
source.
12 / 22
Interfaces to the Outside World
Data are read in using connection interfaces. Connections can be made to files (most
common) or to other more exotic things.
file, opens a connection to a file
gzfile, opens a connection to a file compressed with gzip
bzfile, opens a connection to a file compressed with bzip2
url, opens a connection to a webpage
13 / 22
File Connections
> str(file)
function (description = "", open = "", blocking = TRUE,
encoding = getOption("encoding"))
14 / 22
Connections
In general, connections are powerful tools that let you navigate files or other external
objects. In practice, we often don’t need to deal with the connection interface directly.
is the same as
15 / 22
Reading Lines of a Text File
The readLines function can be used to simply read lines of a text file and store them
in a character vector.
writeLines takes a character vector and writes each element one line at a time to a
text file.
16 / 22
Reading Lines of a Text File
17 / 22
Introduction to the R Language
Control Structures
1 / 14
Control Structures
Control structures in R allow you to control the flow of execution of the program,
depending on runtime conditions. Common structures are
if, else: testing a condition
for: execute a loop a fixed number of times
while: execute a loop while a condition is true
repeat: execute an infinite loop
break: break the execution of a loop
next: skip an interation of a loop
return: exit a function
Most control structures are not used in interactive sessions, but rather when writing
functions or longer expresisons.
2 / 14
Control Structures: if
if(<condition>) {
## do something
} else {
## do something else
}
if(<condition1>) {
## do something
} else if(<condition2>) {
## do something different
} else {
## do something different
}
3 / 14
if
if(x > 3) {
y <- 10
} else {
y <- 0
}
So is this one.
4 / 14
if
if(<condition1>) {
if(<condition2>) {
5 / 14
for
for loops take an interator variable and assign it successive values from a sequence or
vector. For loops are most commonly used for iterating over the elements of an object
(list, vector, etc.)
for(i in 1:10) {
print(i)
}
This loop takes the i variable and in each iteration of the loop gives it values 1, 2, 3,
..., 10, and then exits.
6 / 14
for
These three loops have the same behavior.
x <- c("a", "b", "c", "d")
for(i in 1:4) {
print(x[i])
}
for(i in seq_along(x)) {
print(x[i])
}
for(letter in x) {
print(letter)
}
x <- matrix(1:6, 2, 3)
for(i in seq_len(nrow(x))) {
for(j in seq_len(ncol(x))) {
print(x[i, j])
}
}
Be careful with nesting though. Nesting beyond 2–3 levels is often very difficult to
read/understand.
8 / 14
while
While loops begin by testing a condition. If it is true, then they execute the loop body.
Once the loop body is executed, the condition is tested again, and so forth.
count <- 0
While loops can potentially result in infinite loops if not written properly. Use with
care!
9 / 14
while
Sometimes there will be more than one condition in the test.
z <- 5
x0 <- 1
tol <- 1e-8
repeat {
x1 <- computeEstimate()
11 / 14
repeat
The loop in the previous slide is a bit dangerous because there’s no guarantee it will
stop. Better to set a hard limit on the number of iterations (e.g. using a for loop) and
then report whether convergence was achieved or not.
12 / 14
next, return
for(i in 1:100) {
if(i <= 20) {
## Skip the first 20 iterations
next
}
## Do something here
}
return signals that a function should exit and return a given value
13 / 14
Control Structures
Summary
Control structures like if, while, and for allow you to control the flow of an R
program
Infinite loops should generally be avoided, even if they are theoretically correct.
Control structures mentiond here are primarily useful for writing programs; for
command-line interactive work, the *apply functions are more useful.
14 / 14