Learning Objectives
- Work with atomic and non-atomic objects
- Manipulation of lists
- Manipulation of matrices
Rmd (R markdown) file.lab03-first-last.Rmd, where first and last are your first and last names (e.g. lab03-gaston-sanchez.Rmd).Rmd file as an html document (default option).Rmd and html files to bCourses, in the corresponding lab assignment.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
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
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
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)
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)
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)
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)
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)
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)
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
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
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
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
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
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.
Consider the following list:
hp <- list(
first = 'Harry',
last = 'Potter',
courses = c('Potions', 'Enchantments', 'Spells'),
sport = 'quidditch',
age = 18L,
gpa = 3.9
)
Write R commands—displaying the output—to answer the following questions:
hp?# your code
class(hp)
## [1] "list"
hp?# your code
length(hp)
## [1] 6
courses?# your code
length(hp$courses)
## [1] 3
age?# your code
class(hp$age)
## [1] "integer"
gpa?# your code
class(hp$gpa)
## [1] "numeric"
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"
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)
solar listsolar. 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
)
solarUse the list solar to write R commands—displaying the output—that answer the following questions (use only the list solar, NOT the individual vectors):
# your code
solar[["planet"]][which.max(mass)]
## [1] "Jupiter"
# your code
solar[["mass"]][which.max(moons)]
## [1] 1898
# your code
sum(solar$temperature > 0)
## [1] 3
# your code
quantile(solar$temperature[which(solar$rings)],.75)
## 75%
## -132.5
# your code
solar$planet[which.max((abs(mean(mass)-mass)) + mean(mass))]
## [1] "Jupiter"