In this lesson, we’ll learn about making & working with scalars, vectors and matrices in R.
A new workflow! GitHub repo FIRST, then R project.
In your new project:
r-vectors.qmd
(IN YOUR QUARTO DOC)
Create vectors in R using c()
. Note that all elements in
a vector must be a single class - if numbers and strings are combined,
the whole thing will be of class character.
For example:
# Create and store the vector:
marmots <- c("blue", "green", 4, "yellow")
# Return it:
marmots
## [1] "blue" "green" "4" "yellow"
# Check the class:
class(marmots)
## [1] "character"
If all values are numeric, however, it will be stored as a number:
# Create and store the vector:
pika <- c(12.4, 6.8, 2.9, 8.8, 5)
# Return it:
pika
## [1] 12.4 6.8 2.9 8.8 5.0
# Check the class:
class(pika)
## [1] "numeric"
Notice in the vector above, these are class numeric. If
values should be integers (often the case with count data), you can add
an L
after the value.
# Create the integer vector:
bear <- c(20L, 3L, 5L, 18L, 23L)
# Look at it:
bear
## [1] 20 3 5 18 23
# Check the class:
class(bear)
## [1] "integer"
We learn something important here: even numbers can be stored in R in different ways: floats are numbers that have decimals (these show up as class “numeric”) and integers, numbers without decimals (these show up as class “integer”).
You’ll learn about data representation and other classes of data in EDS 221.
You can add or subtract numeric vectors of equal length using basic operations. For example, let’s make two new vectors of length 4, then try adding & subtracting them:
ringtail <- c(4.3, 8.9, 2.6, 7.1)
fox <- c(9.0, 12.5, 5.4, 10.9)
# Addition:
ringtail + fox
## [1] 13.3 21.4 8.0 18.0
# Subtraction:
fox - ringtail
## [1] 4.7 3.6 2.8 3.8
# Scalar multiplier:
100 * ringtail
## [1] 430 890 260 710
# Dot product:
ringtail %*% fox
## [,1]
## [1,] 241.38
A matrix contains data of a single class (usually numbers). Which means that we can think of the contents of a matrix as a single sequence of values, that are constrained (wrapped) to the specified dimensions of the matrix.
For example, let’s say we have 10 values:
# Make a sequence of values from 1 - 10
my_values <- seq(from = 1, to = 10)
# Look at it (always)
my_values
## [1] 1 2 3 4 5 6 7 8 9 10
Now, we can convert this into a matrix by letting R know how many rows these values should be “wrapped” into (the default is to populate by column…see documentation, and always look at what you’ve created):
my_matrix <- matrix(data = my_values, nrow = 2, ncol = 5, byrow = TRUE)
# Check it out!
my_matrix
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 6 7 8 9 10
Try some other variations to make a matrix from
my_values
to test it out. What happens if you don’t have
enough elements in the matrix to contain your vector? What happens if
your matrix has more elements than your vector?
For example:
matrix(data = my_values, nrow = 3, ncol = 4, byrow = TRUE)
## Warning in matrix(data = my_values, nrow = 3, ncol = 4, byrow = TRUE): data
## length [10] is not a sub-multiple or multiple of the number of rows [3]
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 1 2
So…always, always, always look at what you’ve created.
Scalar multiplication of a matrix is straightforward: just use the multiply operator (*):
4 * my_matrix
## [,1] [,2] [,3] [,4] [,5]
## [1,] 4 8 12 16 20
## [2,] 24 28 32 36 40
Addition/subtraction requires matrices of the same dimension. Let’s make another 2x5 matrix:
my_values_2 <- seq(from = 21, to = 30)
my_matrix_2 <- matrix(my_values_2, nrow = 2, byrow = TRUE)
# Check it out:
my_matrix_2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 21 22 23 24 25
## [2,] 26 27 28 29 30
Add the two matrices:
my_matrix + my_matrix_2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 22 24 26 28 30
## [2,] 32 34 36 38 40
Similarly for subtraction:
my_matrix_2 - my_matrix
## [,1] [,2] [,3] [,4] [,5]
## [1,] 20 20 20 20 20
## [2,] 20 20 20 20 20
As we saw in lecture, matrix multiplication is a bit more complicated (dot products of rows by columns become elements in the resulting matrix). Here’s a reminder:
We multiply matrices in R using the same operator as the dot product
for vectors: %*%
For example:
# Make a couple of 2x2 matrices:
cats <- matrix(data = c(0,4,3,1), nrow = 2, byrow = TRUE)
dogs <- matrix(data = c(6,-3,0,2), nrow = 2, byrow = TRUE)
# Look at them:
cats
## [,1] [,2]
## [1,] 0 4
## [2,] 3 1
dogs
## [,1] [,2]
## [1,] 6 -3
## [2,] 0 2
# Matrix multiplication:
cats %*% dogs
## [,1] [,2]
## [1,] 0 8
## [2,] 18 -7
Confirm that this is correct by doing the matrix multiplication by hand.