Learning Objectives

General Instructions


1) Matrices: examples

The function matrix() takes a vector x as input, and returns a matrix arranging the elements in x according to the specified number of rows nrow and columns ncol. For example:

# example 1
m1 <- matrix(1:6, nrow = 2, ncol = 3)
m1
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
# example 2
m2 <- matrix(1:6, nrow = 2, ncol = 3, byrow = TRUE)
m2
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
# example 3
m3 <- matrix(1:6, nrow = 3, ncol = 2)
m3
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6

1.1) Subsetting

To do subsetting (i.e. subscripting, indexinging) on a matrix, you use bracket notation

# cell in row 1, column 3 of matrix m1
m1[1,3]
## [1] 5
# 2nd column of matrix m2
m2[ ,2]
## [1] 2 5
# 1st row of matrix m3
m3[1, ]
## [1] 1 4

1.2) Row and Column binding

Another way to form matrices is by joining or binding either vectors or matrices by rows or by columns. For example:

a <- c(2, 4, 6)
b <- c(1, 3, 5)
# column bind
cbind(a, b)
##      a b
## [1,] 2 1
## [2,] 4 3
## [3,] 6 5
cbind(m3, b)
##          b
## [1,] 1 4 1
## [2,] 2 5 3
## [3,] 3 6 5
# row bind
rbind(b, a)
##   [,1] [,2] [,3]
## b    1    3    5
## a    2    4    6
rbind(m2, a)
##   [,1] [,2] [,3]
##      1    2    3
##      4    5    6
## a    2    4    6

2) Your Turn: matrices

  1. Use R commands to create a matrix mat1 (see below) from the input vector x = 1:15:
# mat1
1    4    7   10   13
2    5    8   11   14
3    6    9   12   15
# your code
x = 1:15
mat1 <- matrix(x,nrow=3,ncol=5)
  1. Use R commands to create a matrix mat2 (see below) from the input vector x = 1:15:
# mat2
 1    2    3
 4    5    6
 7    8    9
10   11   12
13   14   15
# your code
mat2 <- matrix(x, nrow=5,ncol=3,byrow=TRUE)
  1. Use R commands to create a matrix mat3 (see below) from the input vector x = c(6, 4, 2):
# mat3
6    6    6
4    4    4
2    2    2
# your code
x = c(6,4,2)
mat3 <- matrix(x, nrow=3,ncol=3)
  1. Use R commands to create a matrix mat4 (see below) from the input vector x = c(6, 4, 2):
# mat4
6    6    6
4    4    4
2    2    2
6    6    6
4    4    4
2    2    2
# your code
mat4 = matrix(x, nrow=6,ncol=3)
  1. Use R commands to create a matrix mat5 (see below) from the input vector x = c(6, 4, 2):
# mat5
6    6    6    6    6    6
4    4    4    4    4    4
2    2    2    2    2    2
# your code
mat5 <- matrix(x,nrow=3,ncol=6)
  1. Create matrix mat6 (see below) from first three rows of mat2, and last three rows of mat4
# mat6
1    2    3
4    5    6
7    8    9
6    6    6
4    4    4
2    2    2
# your code
mat6 <- matrix(rbind(mat2[1:3,],mat4[4:6,]),nrow=6,ncol=3)
  1. Look at the documentation of diag() and find out how to use it to extract the elements in the diagonal of mat3 (see below)
# your code
diag(mat3)
## [1] 6 4 2
  1. Use the diag() function to create an identity matrix of dimensions 4 rows and 4 columns (see below)
1    0    0    0
0    1    0    0
0    0    1    0
0    0    0    1
# your code
diag(,4,4)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
  1. Using matrix mat2, subset it to obtain the following elements:
5    6
8    9
# your code
mat2[2:3,2:3]
##      [,1] [,2]
## [1,]    5    6
## [2,]    8    9
  1. Using matrix mat5, subset it to obtain the following elements:
6    6    6    6    6    6
2    2    2    2    2    2
# your code
mat5[-2,]
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    6    6    6    6    6    6
## [2,]    2    2    2    2    2    2
  1. Using matrix mat1, write code to input a couple of missing values (see below)
# missing values
 1    4    7   10   NA
 2    5   NA   11   14
NA    6    9   12   15
# your code
mat1[3,1] = 0
mat1[2,3] = 0
mat1[1,5] = 0

3) List Basics

Consider the information for Leia Organa:

Leia Organa is a force-sensitive woman, 150 cm tall, and weighs 49 kgs. She grew up in Alderaan, and she is a princess.

How can we store Leia’s information? One option is to create vectors:

name <- c("first" = "Leia", "last" = "Organa")
body <- c("height" = 150, "weight"= 49)
force <- TRUE
home <- "Alderaan"

Because all these vectors refer to Leia, we can take a step further and use an R list to put them all in one place:

leia <- list(
  "name" = name,
  "body" = body,
  "force" = force,
  "home" = home
)

leia
## $name
##    first     last 
##   "Leia" "Organa" 
## 
## $body
## height weight 
##    150     49 
## 
## $force
## [1] TRUE
## 
## $home
## [1] "Alderaan"

To create a list use the function list(). You can pass any number of objects inside lists, separated by comma. Naming elements in a list is optional. This means that we could also create a list for Leia as follows:

leia2 <- list(
  name,
  body,
  force,
  home
)

leia2
## [[1]]
##    first     last 
##   "Leia" "Organa" 
## 
## [[2]]
## height weight 
##    150     49 
## 
## [[3]]
## [1] TRUE
## 
## [[4]]
## [1] "Alderaan"

The lists leia and leia2 store the same vectors, in the same order. The only difference is that leia has named elements, whereas list2 has unnamed elements. I strongly recommend giving names to the elements in a list because this makes it easier to understand which elements are being manipulated referreing to them by their names.


4) Simple Lists

Consider the following list:

hp <- list(
  first = 'Harry',
  last = 'Potter',
  courses = c('Potions', 'Enchantments', 'Spells'),
  sport = 'quidditch',
  age = 18L,
  gpa = 3.9
)

4.1) Your Turn

Write R commands—displaying the output—to answer the following questions:

  1. What is the class of hp?
# your code
class(hp)
## [1] "list"
  1. How many elements are in hp?
# your code
length(hp)
## [1] 6
  1. What is the length of courses?
# your code
length(hp$courses)
## [1] 3
  1. What is the data type of the element age?
# your code
class(hp$age)
## [1] "integer"
  1. What is the data type of the element gpa?
# your code
class(hp$gpa)
## [1] "numeric"
  1. If you combine age and gpa in a new vector, what is the data type of this vector?
# your code
class(sum(hp$age,hp$gpa))
## [1] "numeric"

5) Planets

Here are some vectors with data about the planets of the Solar system.

planet <- c('Mercury',  'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune')

mass <- c(0.33, 4.87, 5.97, 0.642, 1898, 568, 86.8, 102)

temperature <- c(167L, 464L, 15L, -65L, -110L, -140L, -195L, -200L)

moons <- c(0L, 0L, 1L, 2L, 79L, 62L, 27L, 14L)

rings <- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)

5.1) Your Turn: solar list

  1. Use these vectors to create a list solar. When printed, your solar list should be displayed as:
$planet
[1] "Mercury" "Venus"   "Earth"   "Mars"    "Jupiter" "Saturn"  "Uranus"  "Neptune"

$mass
[1]    0.330    4.870    5.970    0.642 1898.000  568.000   86.800  102.000

$temperature
[1]  167  464   15  -65 -110 -140 -195 -200

$moons
[1]  0  0  1  2 79 62 27 14

$rings
[1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
# your solar list
solar <- list(
  "planet" = planet,
  "mass" = mass,
  "temperature" = temperature,
  "moons" = moons,
  "rings" = rings
)

5.2) Your Turn: Manipulation of solar

Use the list solar to write R commands—displaying the output—that answer the following questions (use only the list solar, NOT the individual vectors):

  1. What is the name of the heaviest planet?
# your code
solar[["planet"]][which.max(mass)]
## [1] "Jupiter"
  1. What is the mass of the planet with the most number of moons?
# your code
solar[["mass"]][which.max(moons)]
## [1] 1898
  1. How many planets have positive temperatures?
# your code

sum(solar$temperature > 0)
## [1] 3
  1. What is the third quartile (i.e. bottom 75th percentile) of temperature for planets with rings?
# your code
quantile(solar$temperature[which(solar$rings)],.75)
##    75% 
## -132.5
  1. What is the name of the planet whose mass is furthest from the average mass (of all planets)?
# your code
solar$planet[which.max((abs(mean(mass)-mass)) + mean(mass))]
## [1] "Jupiter"