If you're eager to learn more about R and want a taste of what it's like, here's an introduction to the basic operations in R from Joseph Adler's R in a Nutshell.
Let’s get started using R. When you enter an expression into the R console and press the Enter key, R will evaluate that expression and display the results (if there are any). If the statement results in a value, R will print that value. For example, you can use R to do simple math:
> 1 + 2 + 3 [1] 6 > 1 + 2 * 3 [1] 7 > (1 + 2) * 3 [1] 9
The interactive R interpreter will automatically print an object returned by an expression entered into the R console. Notice the funny “[1]” that accompanies each returned value. In R, any number that you enter in the console is interpreted as a vector. A vector is an ordered collection of numbers. The “[1]” means that the index of the first item displayed in the row is 1. In each of these cases, there is also only one element in the vector.
You can construct longer vectors using the c(...) function. (c
stands for “combine.”) For example:
> c(0, 1, 1, 2, 3, 5, 8) [1] 0 1 1 2 3 5 8
is a vector that contains the first seven elements of the Fibonacci sequence. As an example of a vector that spans multiple lines, let’s use the sequence operator to produce a vector with every integer between 1 and 50:
> 1:50 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [23] 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 [45] 45 46 47 48 49 50
Notice the numbers in the brackets on the lefthand side of the results. These indicate the index of the first element shown in each row.
When you perform an operation on two vectors, R will match the elements of the two vectors pairwise and return a vector. For example:
> c(1, 2, 3, 4) + c(10, 20, 30, 40) [1] 11 22 33 44 > c(1, 2, 3, 4) * c(10, 20, 30, 40) [1] 10 40 90 160 > c(1, 2, 3, 4) - c(1, 1, 1, 1) [1] 0 1 2 3
If the two vectors aren’t the same size, R will repeat the smaller sequence multiple times:
> c(1, 2, 3, 4) + 1 [1] 2 3 4 5 > 1 / c(1, 2, 3, 4, 5) [1] 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000 > c(1, 2, 3, 4) + c(10, 100) [1] 11 102 13 104 > c(1, 2, 3, 4, 5) + c(10, 100) [1] 11 102 13 104 15 Warning message: In c(1, 2, 3, 4, 5) + c(10, 100) : <a id="I_indexterm3_d1e1453" class="indexterm"></a><a id="I_indexterm3_d1e1458" class="indexterm"></a>longer object length is not a multiple of shorter object length
Note the warning if the second sequence isn’t a multiple of the first.
In R, you can also enter expressions with characters:
> "Hello world." [1] "Hello world."
This is called a character vector in R. This example is actually a character vector of length 1. Here is an example of a character vector of length 2:
> c("Hello world", "Hello R interpreter")
[1] "Hello world" "Hello R interpreter"
(In other languages, like C, “character” refers to a single character, and an ordered set of characters is called a string. A string in C is equivalent to a character value in R.)
You can add comments to R code. Anything after a pound sign (“#”) on a line is ignored:
> # Here is an example of a comment at the beginning of a line > 1 + 2 + # and here is an example in the middle + 3 [1] 6
In R, the operations that do all of the work are called functions. We’ve already used a few functions above (you can’t do anything interesting in R without them). Functions are just like what you remember from math class. Most functions are in the following form:
f(argument1, argument2, ...)
Where f is the name of the function, and
argument1, argument2, . . . are the
arguments to the function. Here are a few more
examples:
> exp(1) [1] 2.718282 > cos(3.141593) [1] -1 > log2(1) [1] 0
In each of these examples, the functions only took one argument. Many functions require more than one argument. You can specify the arguments by name:
> log(x=64, base=4) [1] 3
Or, if you give the arguments in the default order, you can omit the names:
> log(64,4) [1] 3
Not all functions are of the form f(...). Some of
them are in the form of operators.[5] For example, we used the addition operator (“+”) above. Here
are a few examples of operators:
> 17 + 2 [1] 19 > 2 ^ 10 [1] 1024 > 3 == 4 [1] FALSE
We’ve seen the first one already: it’s just addition. The second
operator is the exponentiation operator, which is interesting because it’s
not a commutative operator. The third operator is the equality operator.
(Notice that the result returned is FALSE; R has a
Boolean data type.)
Like most other languages, R lets you assign values to
variables and refer to them by name. In R, the assignment operator is <-. Usually,
this is pronounced as “gets.” For example, the statement:
x <- 1
is usually read as “x gets 1.” (If you’ve ever done any work with theoretical computer science, you’ll probably like this notation: it looks just like algorithm pseudocode.)
After you assign a value to a variable, the R interpreter will substitute that value in place of the variable name when it evaluates an expression. Here’s a simple example:
> x <- 1 > y <- 2 > z <- c(x,y) > # evaluate z to see what's stored as z > z [1] 1 2
Notice that the substitution is done at the time that the value is
assigned to z, not at the time that
z is evaluated. Suppose that you were to type in the
preceding three expressions and then change the value of
y. The value of z would not
change:
> y <- 4 > z [1] 1 2
R provides several different ways to refer to a member (or set of members) of a vector. You can refer to elements by location in a vector:
> b <- c(1,2,3,4,5,6,7,8,9,10,11,12) > b [1] 1 2 3 4 5 6 7 8 9 10 11 12 > # let's fetch the 7th item in vector b > b[7] [1] 7 > # fetch items 1 through 6 > b[1:6] [1] 1 2 3 4 5 6 > # fetch only members of b that are congruent to zero (mod 3) > # (in non-math speak, members that are multiples of 3) > b[b %% 3 == 0] [1] 3 6 9 12
You can fetch multiple items in a vector by specifying the indices of each item as an integer vector:
> # fetch items 1 through 6 > b[1:6] [1] 1 2 3 4 5 6 > # fetch 1, 6, 11 > b[c(1,6,11)] [1] 1 6 11
You can fetch items out of order. Items are returned in the order that they are referenced:
> b[c(8,4,9)] [1] 8 4 9
You can also specify which items to fetch through a logical vector. As an example, let’s fetch only multiples of 3 (by selecting items that are congruent to 0 mod 3):
> b %% 3 == 0 [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE [12] TRUE > b[b %% 3 == 0] [1] 3 6 9 12
In R, there are two additional operators that can be used for
assigning values to symbols. First, you can use a single equals sign (“=”) for assignment.[6] This operator assigns the symbol on the left to the object
on the right. In many other languages, all assignment statements use
equals signs. If you are more comfortable with this notation, you are free
to use it. However, I will be using only the <-
assignment operator in this book because I think it is easier to read.
Whichever notation you prefer, be careful because the = operator does not mean “equals.” For
that, you need to use the == operator:
> one <- 1 > two <- 2 > # This means: assign the value of "two" to the variable "one" > one = two > one [1] 2 > two [1] 2 > # let's start again > one <- 1 > two <- 2 > # This means: does the value of "one" equal the value of "two" > one == two [1] FALSE
In R, you can also assign an object on the left to a symbol on the right:
> 3 -> three > three [1] 3
In some programming contexts, this notation might help you write clearer code. (It may also be convenient if you type in a long expression and then realize that you have forgotten to assign the result to a symbol.)
A function in R is just another object that is assigned to a symbol. You can define your own functions in R, assign them a name, and then call them just like the built-in functions:
> f <- function(x,y) {c(x+1, y+1)}
> f(1,2)
[1] 2 3
This leads to a very useful trick. You can often type the name of a function to see the code for it. Here’s an example:
> f
function(x,y) {c(x+1, y+1)}
[5] When you enter a binary or unary operator into R, the R interpreter will actually translate the operator into a function; there is a function equivalent for each operator. We’ll talk about this more in Chapter 5, An Overview of the R Language.
[6] Note that you cannot use the <- operator
when passing arguments to a function; you need to map values to
argument names using the “=” symbol. Using the
<- operator in a function will assign the value
to the variable in the current environment and then pass the value
returned to the function. This might be what you want, but it probably
isn’t.
R is rapidly becoming the standard for developing statistical software, and R in a Nutshell provides a quick and practical way to learn this increasingly popular open source language and environment. You'll not only learn how to program in R, but also how to find the right user-contributed R packages for statistical modeling, visualization, and bioinformatics.




Help









