## 9.8 Exercise 4. Matrix manipulation

Create the script “exercise4.R” and save it to the “Rcourse/Module1” directory: you will save all the commands of exercise 4 in that script.
Remember you can comment the code using #.

correction

``````getwd()
setwd("Rcourse/Module1")
setwd("~/Rcourse/Module1")``````

1- Create three numeric vectors x, y, z, each of 4 elements of your choice.

correction

``````x <- 2:5
y <- 6:9
z <- 7:4``````

Use rbind() to create a matrix mat (3 rows and 4 columns) out of x, y and z.

correction

``mat <- rbind(x, y, z)``

2- Create the same matrix now using the matrix function. Repeat the same using the byrow=TRUE parameter: what is different?

correction

``````mat <- matrix(data=c(x, y, z), nrow=3, ncol=4)
# "byrow=TRUE": what is different in the output ?
mat_byrow <- matrix(data=c(x, y, z), nrow=3, ncol=4, byrow=TRUE)``````

3- Add names to mat’s columns: “a”, “b”, “c”, “d”, respectively.

correction

``colnames(mat) <- c("a", "b", "c", "d")``

4- Calculate the sum of each row, and the sum of each column

correction

``rowSums(mat); colSums(mat)``

5- Create the matrix mat2 as:

``mat2 <- matrix(c(seq(from=1, to=10, by=2), 5:1, rep(x=2017, times=5)), ncol=3)``

What does function seq() do?

correction

seq generate sequences of numbers. Here, it creates a sequences from 1 to 10 with a step of 2 numbers.

6- What are the dimensions of mat2 (number of rows and number of columns)?

correction

``````# number of rows
nrow(mat2)
# number of columns
ncol(mat2)
# dimensions: number of rows, number of columns
dim(mat2)``````

7- Add column names to mat2: “day”, “month” and “year”, respectively.

correction

``colnames(mat2) <- c("day", "month", "year")``

8- Add row names to mat2: letters “A” to “E”

correction

``````rownames(mat2) <- c("A", "B", "C", "D", "E")
rownames(mat2) <- LETTERS[1:5]``````

9- Shows row(s) of mat2 where the month column is greater than or equal to 3.

correction

``````# select column month
mat2[, "month"]
# element(s) of column month that is (are) greater than or equal to 3
mat2[,"month"] >= 3
# finally select row(s) where the month columns is greater than or equal to 3
mat2[mat2[,"month"] >= 3,]``````

10- Replace all elements of mat2 that are equal to 2017 with 2018.

correction

``````# which elements of mat2 that are exactly equal to 2017
mat2==2017
# retrieve actual elements
mat2[mat2==2017]
# replace all 2017 with 2018
mat2[mat2==2017] <- 2018``````

11- Multiply all elements of the 2nd column of mat2 by 7. Reassign mat2!

correction

``````# multiply all elements of the 2nd column of mat2 by 7
mat2[,2] * 7
# reassign mat2 with the new values of column 2
mat2[,2] <- mat2[,2] * 7``````

12- Add the column named “time” to mat2, that contains values 8, 12, 11, 10, 8. Save in the new object mat3.

correction

``mat3 <- cbind(mat2, time=c(8, 12, 11, 10, 8))``

13- Replace all elements of mat3 that are less than 3 with NA.

correction

``````# which elements of mat3 that are less than 3
mat3 < 3
# actually elements of mat3 that are less than 3
mat3[mat3 < 3]
# reassign elements of mat3 that are less than 3 with NA
mat3[mat3 < 3] <- NA``````

14- Remove rows from mat3 if a NA is present. Save in the new object mat4.

correction

``mat4 <- na.omit(mat3)``

15- Retrieve the smaller value of each column of mat4.

Try different approaches:

• Retrieve the minimum for each column one by one.

correction

``````min(mat4[,"day"])
min(mat4[,"month"])
min(mat4[,"year"])
min(mat4[,"time"])``````
• Retrieve the minimum of all columns simultaneously using the apply() function.

correction

``````# mat4: object
# 2: by column
# min: function to apply
apply(mat4, 2, min)``````