[For this exercise, first write down your answer, without using R. Then, check your answer using R.]

Answers to the exercises are available here.

**Exercise 1**

If

`M=matrix(c(1:10),nrow=5,ncol=2,`

dimnames=list(c('a','b','c','d','e'),c('A','B')))

What is the value of: `M`

**Exercise 2**

Consider the matrix `M`

,

What is the value of:

`M[1,]`

`M[,1]`

`M[3,2]`

`M['e','A']`

**Exercise 3**

Consider the matrix `N`

`N=matrix(c(1:9),nrow=3,ncol=3,`

dimnames=list(c('a','b','c'),c('A','B','C')))

What is the value of: `diag(N)`

**Exercise 4**

What is the value of: `diag(4,3,3)`

Is matrix ?

**Exercise 5**

If `M=matrix(c(1:9),3,3,byrow=T,`

dimnames=list(c('a','b','c'),c('d','e','f')))

What is the value of:

`rownames(M)`

`colnames(M)`

**Exercise 6**

What is the value of:

`upper.tri(M)`

`lower.tri(M)`

`lower.tri(M,diag=T)`

**Exercise 7**

Consider two matrix,

`M,N`

`M=matrix(c(1:9),3,3,byrow=T)`

`N=matrix(c(1:9),3,3)`

What is the value of:

`M*N`

**Exercise 8**

Consider two matrix,

`M,N`

`M=matrix(c(1:9),3,3,byrow=T)`

`N=matrix(c(1:9),3,3)`

What is the value of:

`M%*%N`

**Exercise 9**

Consider two matrix,

`M,N`

`M=matrix(c(1:9),3,3,byrow=T)`

`N=matrix(c(1:9),3,3)`

What is the value of:

`(M+N)^2`

**Exercise 10**

Consider two matrix,

`M,N`

`M=matrix(c(1:9),3,3,byrow=T)`

`N=matrix(c(1:9),3,3)`

What is the value of:

`M/N`

**Want to practice matrices a bit more? We have more exercise sets on this topic here.**

`diag`

, `t`

, `eigen`

, and `crossprod`

functions. If you want further documentation also consider chapter 5.7 from “An Introduction to R”.
Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment.

**Exercise 1**

Consider `A=matrix(c(2,0,1,3), ncol=2)`

and `B=matrix(c(5,2,4,-1), ncol=2).`

a) Find ** A** + ** B**

b) Find ** A** – ** B**

**Exercise 2**

Scalar multiplication. Find the solution for a**A** where `a=3`

and **A** is the same as in the previous question.

**Exercise 3**

Using the the `diag `

function build a diagonal matrix of size 4 with the following values in the diagonal 4,1,2,3.

**Exercise 4**

Find the solution for **Ab,** where **A** is the same as in the previous question and `b=c(7,4).`

**Exercise 5**

Find the solution for **AB,** where ** B** is the same as in question 1.

**Exercise 6**

Find the transpose matrix of **A.**

**Exercise 7**

Find the inverse matrix of **A.**

**Exercise 8**

Find the value of x on **Ax**=**b.**

**Exercise 9**

Using the function ` eigen `

find the eigenvalue for **A**.

**Exercise 10**

Find the eigenvalues and eigenvectors of ** A’A **. **Hint**: Use` crossprod`

to compute ** A’A **.

- Spatial Data Analysis: Introduction to Raster Processing (Part 1)
- Advanced Techniques With Raster Data: Part 1 – Unsupervised Classification
- Spatial Data Analysis: Introduction to Raster Processing: Part-3
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory

`rbind`

and `cbind`

is a common R task. However, when dimensions or classes differ between the objects passed to these functions, errors or unexpected results are common as well. Sounds familiar? Time to practice!
Answers to the exercises are available here.

**Exercise 1**

Try to create matrices from the vectors below, by binding them column-wise. First, without using R, write down whether binding the vectors to a matrix is actually possible; then the resulting matrix and its mode (e.g., character, numeric etc.). Finally check your answer using R.

a. `a <- 1:5 ; b <- 1:5`

b. `a <- 1:5 ; b <- c('1', '2', '3', '4', '5')`

c. `a <- 1:5 ; b <- 1:4; c <- 1:3`

**Exercise 2**

Repeat exercise 1, binding vectors row-wise instead of column-wise while avoiding any row names.

**Exercise 3**

Bind the following matrices column-wise. First, without using R, write down whether binding the matrices is actually possible; then the resulting matrix and its mode (e.g., character, numeric etc.). Finally check your answer using R.

a. `a <- matrix(1:12, ncol=4); b <- matrix(21:35, ncol=5)`

b. `a <- matrix(1:12, ncol=4); b <- matrix(21:35, ncol=3)`

c. `a <- matrix(1:39, ncol=3); b <- matrix(LETTERS, ncol=2)`

**Exercise 4**

Bind the matrix `a <- matrix(1:1089, ncol=33)`

to itself, column-wise, 20 times (i.e., resulting in a new matrix with 21*33 columns). Hint: Avoid using `cbind()`

to obtain an efficient solution. Various solutions are possible. If yours is different from those shown on the solutions page, please post yours on that page as comment, so we can all benefit.

**Exercise 5**

Try to create new data frames from the data frames below, by binding them column-wise. First, without using R, write down whether binding the data frames is actually possible; then the resulting data frame and the class of each column (e.g., integer, character, factor etc.). Finally check your answer using R.

a. `a <- data.frame(v1=1:5, v2=LETTERS[1:5]) ; b <- data.frame(var1=6:10, var2=LETTERS[6:10])`

b. `a <- data.frame(v1=1:6, v2=LETTERS[1:6]) ; b <- data.frame(var1=6:10, var2=LETTERS[6:10])`

**Exercise 6**

Try to create new data frames from the data frames below, by binding them row-wise. First, without using R, write down whether binding the data frames is actually possible; then the resulting data frame and the class of each column (e.g., integer, character, factor etc.). Finally check your answer using R, and explain any unexpected output.

a. `a <- data.frame(v1=1:5, v2=LETTERS[1:5]) ; b <- data.frame(v1=6:10, v2=LETTERS[6:10])`

b. `a <- data.frame(v1=1:6, v2=LETTERS[1:6]) ; b <- data.frame(v2=6:10, v1=LETTERS[6:10])`

**Exercise 7**

a. Use `cbind()`

to add vector `v3 <- 1:5`

as a new variable to the data frame created in exercise 6b.

b. Reorder the columns of this data frame, as follows: v1, v3, v2.

**Exercise 8**

Consider again the matrices of exercise 3b. Use both `cbind()`

and `rbind()`

to bind both matrices column-wise, adding `NA`

for empty cells.

**Exercise 9**

Consider again the data frames of exercise 5b. Use both `cbind()`

and `rbind()`

to bind both matrices column-wise, adding `NA`

for empty cells.

Image: By Hella, Handdrawing 1995.

]]>Create an array (3 dimensional) of 24 elements using the

`dim()`

function.
**Exercise 2**

Create an array (3 dimensional) of 24 elements using the `array()`

function.

**Exercise 3**

Assign some dimnames of your choice to the array using the `dimnames()`

function.

**Exercise 4**

Assign some dimnames of your choice to the array using the arguments of the `array()`

function.

**Exercise 5**

Instead of column-major array, make a row-major array (transpose).

**Exercise 6**

For this exercise, and all that follow, download this file, and read it into R using the `read.csv()`

function, e.g.:

`temp`

Copy the column named `N`

into a new variable `arr`

.

**Exercise 7**

Set dimensions of this variable and convert it into a 3 * 2 * 4 array. Add dimnames.

**Exercise 8**

Print the whole array on the screen.

**Exercise 9**

Print only elements of height 2, assuming the first dimension represents rows, the second columns and the third heigth.

**Exercise 10**

Print elements of height 1 and columns 3 and 1.

**Exercise 11**

Print element of height 2, column 4 and row 2.

**Exercise 12**

Repeat the exercises 9-11, but instead of using numbers to reference row, column and height, use `dimnames`

.

Image: Cubo completato” by Masakazu “Matto” Matsumoto from Nagoya, Japan – http://flickr.com/photos/vitroids/1527092739/. Licensed under CC BY 2.0 via Wikimedia Commons.

]]>- Multiple Regression (Part 2) – Diagnostics
- Matrix Operations Exercises
- Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-8)
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory

Please note, solutions are available here.

Create three vectors

`x,y,z`

with integers and each vector has 3 elements. Combine the three vectors to become a 3×3 matrix `A`

where each column represents a vector. Change the row names to `a,b,c`

.Think: How about each row represents a vector, can you modify your code to implement it?

**Exercise 2**

Please check your result from Exercise 1, using `is.matrix(A)`

. It should return `TRUE`

, if your answer is correct. Otherwise, please correct your answer. Hint: Note that `is.matrix()`

will return `FALSE`

on a non-matrix type of input. Eg: a vector and so on.

**Exercise 3**

Create a vector with 12 integers. Convert the vector to a 4*3 matrix `B`

using `matrix()`

. Please change the column names to `x, y, z`

and row names to `a, b, c, d`

.

The argument `byrow`

in `matrix()`

is set to be `FALSE`

by default. Please change it to `TRUE`

and print `B`

to see the differences.

**Exercise 4**

Please obtain the transpose matrix of `B`

named `tB`

.

**Exercise 5**

Now `tB`

is a 3×4 matrix. By the rule of matrix multiplication in algebra, can we perform `tB*tB`

in R language? (Is a 3×4 matrix multiplied by a 3×4 allowed?) What result would we get?

**Exercise 6**

As we can see from Exercise 5, we were expecting that `tB*tB`

would not be allowed because it disobeys the algebra rules. But it actually went through the computation in R. However, as we check the output result , we notice the multiplication with a single `*`

operator is performing the componentwise multiplication. It is not the conventional matrix multiplication. How to perform the conventional matrix multiplication in R? Can you compute matrix `A`

multiplies `tB`

?

**Exercise 7**

If we convert `A`

to a `data.frame`

type instead of a `matrix`

, can we still compute a conventional matrix multiplication for matrix `A`

multiplies matrix `A`

? Is there any way we could still perform the matrix multiplication for two `data.frame`

type variables? (Assuming proper dimension)

**Exercise 8**

Extract a sub-matrix from `B`

named `subB`

. It should be a 3×3 matrix which includes the last three rows of matrix `B`

and their corresponding columns.

**Exercise 9**

Compute `3*A`

, `A+subB`

, `A-subB`

. Can we compute `A+B`

? Why?

**Exercise 10**

Generate a n * n matrix (square matrix) `A1`

with proper number of random numbers, then generate another n * m matrix `A2`

.

If we have `A1*M=A2`

(Here * represents the conventional multiplication), please solve for `M`

.

Hint: use the `runif()`

and `solve()`

functions. E.g., `runif(9)`

should give you 9 random numbers.

**Want to practice matrices a bit more? We have more exercise sets on this topic here.**

Image: “200px-Sudoku06u” by DrBorka from nl. Licensed under CC BY-SA 3.0 via Wikimedia Commons.

]]>