I will explain some basics about R. See this page for installation of R and RStudio.

Basic Usage of R

You can write your R codes directly on R’s Console or edit some codes in a text editor, save it as a file, and call the file from R. When you save your R codes in a file, you need to save it with “.R” extension, e.g., my-code.R. You can run the script by typing source("my-code.R") on Console.

In R, commands are separated by line breaks. Beginning a new line terminates the command in the previous line. However, a new line does not begin a new command if a parenthesis is not closed or the last line ended with an operator (e.g., + or -). You can write more than one commands in a single line by separating them by semi-colon, but you should not write two or more commands in a single line.

R is case sensitive. Thus, Var1 and var1 are two different objects in R. Obeject (variables, datasets, or functions) names must be alphanumeric (and period and underbar). The first letter must be an alphabet. (Well, in fact, we can use Japanese characters for object names, but you don’t wanna do that.)

The number of spaces does not make any difference if it is not zero. You may have spaces before and after an operator. You should decide the number of spaces to maximize readability of your codes.

“#” signifies the beginning of a comment. Putting # at the beginning of a line makes the whole line a comment; putting it in the middle makes a part of the line a comment while leaving the part before # as a command. Commenting is as important as writing the body of codes (more on this in Week 3).

When you would like to know how to use an R function, refer to Help. You can open a help file by typing ?FUNCTION_NAME or help(FUNCTION_NAME). If you want to open a help file in your web browser, type help.start().

To use an R packages, type library("PACKAGE_NAME"). For instance, to use ggplot2, type library("ggplot2"). To install a package, type install.packages("PACKAGE_NAME") with your computer connected to the internet. You need to choose a repository from which you download the packages. You can also specify the repository to use in your .Rprofile (Read this page).


Use R as a Calculator

You can use R as a calculator. Here are some examples.

1 + 1     ## addition
## [1] 2
100 - 20  ## subtraction
## [1] 80
5 * 8     ## multiplication
## [1] 40
2/3       ## division
## [1] 0.6666667
2^4       ## power
## [1] 16
sqrt(2)   ## square root
## [1] 1.414214
2^(1/2)   ## same as sqrt(2)
## [1] 1.414214

Use parentheses to specify the order of operation.

(5 * (2 + 1))^3  ## First, add 1 to 2, then multiply it by 5, and finally cube it.
## [1] 3375


Variables

You can create and use variables in R. You can choose the names of variables (The names must not start with a number. You cannot use a hyphen). For instance, you can create two variable \(a\) and \(b\) by typing:

a <- 1
b <- 2

To assing a value to a variable, you use “<-” (you can also use “=”, but you should avoid using it). Once you assign a number to variables, you can freely use them. To show the values of variables, simply type the variable names.

a
## [1] 1
b
## [1] 2

You can calculate numbers by using variables.

a + b
## [1] 3
a - b
## [1] -1
a * b
## [1] 2
a/b
## [1] 0.5
b^a
## [1] 2

Now, let’s type:

c <- a
a <- 3

What numbers do a and c contain?

If you want to assign a value to a variable and print the value to the screen at the same time, put the command in a parenthesis.

(d <- 3 * 5)
## [1] 15

You can remove a variable (object) by rm() function.

rm(d)


Vectors and Matrices

Vectors

To create a vector in R, use c() (c for concatenate or comnbine).

a <- c(1, 2, 3, 4, 5)

Let’s print it on screen.

a
## [1] 1 2 3 4 5

You can make a vector of characters. For example:

univ <- c("Kobe", "Essex", "UCLA")

As this example shows, chracters should be quoted (either ’’ or “” is fine).

There are a variety of ways to create vectors in addition to specifying every element manually. The most useful function is seq() for sequence.

seq(1, 20, by = 1)  ## integers from 1 to 20.  same as 1:20
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
1:20
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
seq(1, 20, by = 2)  ## odd numbers from 1 to 19
##  [1]  1  3  5  7  9 11 13 15 17 19
seq(2, 20, by = 2)  ## even numbers from 2 to 20
##  [1]  2  4  6  8 10 12 14 16 18 20
seq(20, 1, by = -5)  ## descending order, decrement is 5
## [1] 20 15 10  5
seq(1, 100, length = 100)  ## vector with length 10 whose min and max are 1 and 100
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
##  [18]  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34
##  [35]  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51
##  [52]  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68
##  [69]  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85
##  [86]  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100

Instead of using seq(x, y, by = 1), you can use x:y.

Another function of value is rep() for repeat.

rep(3, 10)   ## vector of ten 3s
##  [1] 3 3 3 3 3 3 3 3 3 3
rep(c('a', 'b', 'c'), c(3, 1, 2))  ## 3 a's, 1 b, and 2 c's
## [1] "a" "a" "a" "b" "c" "c"

To access the \(i\)-th element of a vector, type VECTOR_NAME[i] with a square bracket. You can retrieve multiple elements at once. Here are some examples:

a <- seq(10, 100, length = 10)
b <- 10:1
a[2]
## [1] 20
b[2]
## [1] 9
a[3:5]
## [1] 30 40 50
a[c(1, 3, 5)]
## [1] 10 30 50
a[c(8, 2, 4)]
## [1] 80 20 40
b[-5]
## [1] 10  9  8  7  5  4  3  2  1


Vector Operation

You can use vectors in your calculation. Some examples:

x <- 1:10
x + 10      ## add 10 to each element of x
##  [1] 11 12 13 14 15 16 17 18 19 20
x - 5       ## subtract 5 from each element of x
##  [1] -4 -3 -2 -1  0  1  2  3  4  5
x * 2       ## multiple each element of x by 5
##  [1]  2  4  6  8 10 12 14 16 18 20
x/3         ## divide each element of x by 3
##  [1] 0.3333333 0.6666667 1.0000000 1.3333333 1.6666667 2.0000000 2.3333333
##  [8] 2.6666667 3.0000000 3.3333333
x^2         ## square each element of x
##  [1]   1   4   9  16  25  36  49  64  81 100
sqrt(x)     ## take the square root for each element of x
##  [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751
##  [8] 2.828427 3.000000 3.162278

You can also use more than one vectors.

x <- 1:10
y <- -10:-1
x + y     ## element-by-element addition
##  [1] -9 -7 -5 -3 -1  1  3  5  7  9
x - y     ## element-by-element subtraction
##  [1] 11 11 11 11 11 11 11 11 11 11
x * y     ## element-by-element multiplication
##  [1] -10 -18 -24 -28 -30 -30 -28 -24 -18 -10
x/y       ## element-by-element division
##  [1]  -0.1000000  -0.2222222  -0.3750000  -0.5714286  -0.8333333
##  [6]  -1.2000000  -1.7500000  -2.6666667  -4.5000000 -10.0000000
x^y       ## x[i] to the power of y[i]
##  [1] 1.000000e+00 1.953125e-03 1.524158e-04 6.103516e-05 6.400000e-05
##  [6] 1.286008e-04 4.164931e-04 1.953125e-03 1.234568e-02 1.000000e-01

Adding two vectors does not change the length of vector.

length(x)
## [1] 10
length(y)
## [1] 10
length(x + y)
## [1] 10

If the lengths of vectors differ, R recycles the elements of shorter vectors.

x <- 1:10
y <- c(100, 200)
x + y
##  [1] 101 202 103 204 105 206 107 208 109 210

If the length of the longer vector is not a multiple of the shorter, R gives you a warning (it still works, though).

x <- 1:10
y <- 1:3 * 100
x + y
## Warning in x + y: longer object length is not a multiple of shorter object
## length
##  [1] 101 202 303 104 205 306 107 208 309 110

You can obtain dot products and outer products by %*% and %o% (or outer()), respectively.

x <- c(1, 3, 5)
y <- 1:3 * 10
x %*% y  ## dot product of x and y
##      [,1]
## [1,]  220
x %o% y  ## outer product of x and y
##      [,1] [,2] [,3]
## [1,]   10   20   30
## [2,]   30   60   90
## [3,]   50  100  150


Matrices

To create a matrix in R, use matrix(). For example, to make 3 by 3 square matrices, type:

(A <- matrix(1:9, nrow = 3, byrow = TRUE))
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
(B <- matrix(1:9, nrow = 3, byrow = FALSE))
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

Now, what is the difference between A and B? If we focus on the elements of two matrices, A and B are the same. This is becuase both commands have 1:9 in the function. But the positions of elements differ between two, because the argument byrow is specified differently (byrow = FALSE is default). In this example, we only specify the number of rows by nrow, but in general, we also need to specify the number of columns by ncol. When the number of elements is set (9 in this example), you only have to specify either nrow or ncol. In this example, the number of columns was automatically specified: 3 (n of columns) = 9 (n of elements) / 3 (n of rows).

You can name each row and column.

row.names(A) <- c('row1', 'row2', 'row3')  ## assign name to each row
colnames(A) <- c('col1', 'col2', 'col3')   ## assign name to each column
A
##      col1 col2 col3
## row1    1    2    3
## row2    4    5    6
## row3    7    8    9

Note that row.names() has a period in its name but colnames does not.

To retrieve the element in \(i\)-th row’s \(j\)-th column, type MATRIX_NAME[i, j] Some examples:

A[1, 3]         ## element in the 1st row, 3rd column
## [1] 3
A[2, c(1, 3)]   ## the 1st and 3rd columns in the 2nd row
## col1 col3 
##    4    6
A[3, ]          ## all elements in the 3rd row
## col1 col2 col3 
##    7    8    9
A[, 2]          ## all elements in the 2nd column
## row1 row2 row3 
##    2    5    8


Matrix Operation

Basic matrix operations can be conducted as follows.

A <- matrix(1:9, ncol = 3)
B <- matrix(-4:4, ncol = 3)
A + 3       ## add 3 to each element of A
##      [,1] [,2] [,3]
## [1,]    4    7   10
## [2,]    5    8   11
## [3,]    6    9   12
2 * A       ## multiply each element of A by 2
##      [,1] [,2] [,3]
## [1,]    2    8   14
## [2,]    4   10   16
## [3,]    6   12   18
A + B       ## element by element addition: A[i, j] + B[i, j]
##      [,1] [,2] [,3]
## [1,]   -3    3    9
## [2,]   -1    5   11
## [3,]    1    7   13
A * B       ## element by element multiplication: A[i, j] * B[i, j]
##      [,1] [,2] [,3]
## [1,]   -4   -4   14
## [2,]   -6    0   24
## [3,]   -6    6   36
A %*% B     ## marix multiplication: AB
##      [,1] [,2] [,3]
## [1,]  -30    6   42
## [2,]  -39    6   51
## [3,]  -48    6   60
B %*% A     ## marix multiplication: BA
##      [,1] [,2] [,3]
## [1,]    0   -9  -18
## [2,]    6    6    6
## [3,]   12   21   30
# AB and BA are in generall different
A %*% B == B %*% A  ## comapre each element between AB and BA
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE  TRUE FALSE
## [3,] FALSE FALSE FALSE
a <- 1:3
A %*% a    ## (3x3) x (3x1) results in (3x1)
##      [,1]
## [1,]   30
## [2,]   36
## [3,]   42

To transpose a matrix, use t().

A
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
t(A)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

Obtain the inverse of a matrix by solve().

C <- matrix(c(2, 3, 5, 7, 11, 13, 17, 19, 23),
            nrow = 3)
solve(C)
##             [,1]       [,2]        [,3]
## [1,] -0.07692308 -0.7692308  0.69230769
## [2,] -0.33333333  0.5000000 -0.16666667
## [3,]  0.20512821 -0.1153846 -0.01282051

Passing a singular matrix to sovlve() causes an error.



Back to Class Materials