Lecture 3: R-Blocks
Matrices & Lists
Ivan Belik
Assembled and built based on:
https://openlibra.com/en/book/download/an-introduction-to-r-2
1
R: Creating Matrices
• To create matrices we use the matrix() function
• The matrix() function takes the following arguments:
• data an R object: vector
• nrow the desired number of rows
• ncol the desired number of columns
• byrow a logical statement to populate the matrix by either row or by column
• Example 1:
2
R: Matrix characteristics
• We can also check some characteristics:
• dim() – the dimension of matrix (i.e., number of rows and columns)
• nrow() – the number of rows
• ncol() – the number of columns
dim(Matrix1) will give us: nrow(Matrix1) will give us: ncol(Matrix1) will give us:
> dim(Matrix1) > nrow(Matrix1) > ncol(Matrix1)
> [1] 3 3 [1] 3 [1] 3
3
R: Creating Matrices
• we can drop the argument names in matrix() as long as you remember that:
• the first argument asks for the data
• the second - for the rows
• the third - for the columns
• Example 2:
• Let’s create a rectangular matrix (3 by 7) and fill it with NA’s
• NA is a useful special object existing in R
• NA is a kind of a special zero and most computations involving NA return NA
• NA + 1 evaluates to NA
• NA == TRUE returns NA
• In addition, we used dim() function
in the code to check matrix dimension
4
R: Creating Matrices
• Example 3
Let’s fill a matrix with a vector of values
Note: we did not use ncol argument (but, in the Example 3 the matrix with nrow=4 is the same one as with ncol=3)
5
R: Creating Matrices
• Example 4
You can also create matrices by pasting together vectors using:
• rbind() function: it combines vectors by rows
6
R: Creating Matrices
• Example 4
Another way to create matrices by pasting vectors together using:
• cbind() function: it combines vectors by columns
7
R: Matrices
• As we could see, rbind() and cbind() functions automatically label row or column names
• You can also use:
• rownames() function - to manipulate rows’ names
• colnames() function - to manipulate columns’ names
Let us consider an example on the following slide
8
R: Matrices
9
R: Matrices
• The diag() function is useful for creating the identity matrix
10
R: Indexing matrices
• Use [i, j] to retrieve the j-th element of the i-th row:
• Note: the matrix is filled by columns as the parameter byrow = FALSE
byrow = FALSE by default – no need to specify it in the matrix()-function if we agree with the default value of the given parameter
11
R: Indexing matrices
• You can also extract entire rows as vectors by leaving the column entry blank, and vice versa
12
R: Matrix operations
• R can do matrix arithmetic. Below is a list of some basic operations we can do:
13
R: Matrix operations
14
R: Matrix operations
• To compute eigenvalues and vectors you can use the eigen() function
• Unlike most functions we have encountered so far, it does not return a vector or a matrix but a list
• Reminder: List can be a collection of different types of objects
• The eigen(X) function returns a list containing:
• eigenvalues
• eigenvectors
15
R: Matrix operations
• You can extract named elements from a list with the $ symbol
• To see the names of the elements we use function names()
• Consider the following code:
16
R: Lists and matrices
• We can use list() function to store vectors and matrices in one variable (which is a list-type):
17
R: Lists and matrices
• To access elements of the list:
18
R: Lists and matrices
• To access the list’s elements we can write one-line request:
19
R: Lists and matrices
• We can also assign names to the elements of the list
• Then we can access the elements using $assigned_name
20
R: Lists: more details
• You can do different manipulations with variables in the lists:
21