R Data Structures 2: Matrix
Creating a Matrix, Dimension Names, Column Bind & Row Bind, Subsets, and Immutability
Check out the work by . I’m getting him to make a 3d painted version of this.
You can see the previous post on vectors here.
A matrix in R is a matrix like in linear algebra mathematics. A matrix is a table, but without any of the labeling.
The Python equal to this would be a numpy array. Matrices are almost useless in R (we have dataframes). if you are sending some data to a neural network, data cleaning, then this is pretty useful. Other than that, meh.
Here’s some code you’ll find useful when it comes to matrices.
Table of Contents:
Creating a Matrix
Dimension Names
Column Bind and Row Bind
Subsets
Immutability
1 - Creating a Matrix
1.1 Numeric Matrix
To make a matrix, we use the matrix() function in R. To use this, we’ll want to supply it with a large vector. Then we’ll want to tell it the number of rows, and columns we want our matrix to use. Here is the official documentation if you want to see it:
In this example, we’ll make a matrix then goes from 1 to 8, and this matrix will have 4 rows, and 2 columns.
my_matrix=matrix(1:8, nrow=4)
print(my_matrix)
Notice I didn’t tell R how many columns I need. This is because I can either specify the number of columns, or the number of rows. R is smart enough to put two and two together.
1.2 Character Matrix
When you build a matrix in R, you don’t have to only fill it with numeric values. You can also fill it with character values. When a matrix has only characters, we call that a character matrix.
my_matrix=matrix(c('a','b','c','d','e','f','g','h'), nrow=4)
print(my_matrix)
2 - Dimension Names
For our matrix, we can even give the the columns a name, and the rows a name as well. To do this, we will use the dimnames parameter in the matrxi() function. Example below:
Row Names : WXYZ
Col Names: AB
my_matrix=matrix(c('a','b','c','d','e','f','g','h'),
nrow=4,dimnames=list(c('W','X','Y','Z'),c('A','B')))
print(my_matrix)
we can access the rows/column names by using the colnames(), and the rownames() functions. Put in the matrix inside the brackets
print(colnames(my_matrix))
3 - Column Bind and Row Bind
3.1 cbind()
If we want to add in an extra column to that right, that is referred to as column bind. In R, we have a function called cbind(), where we insert a vector, and it adds that as a column to the right.
my_matrix=cbind(my_matrix,c('H','E','L','O'))
print(my_matrix)
Notice that the newly created column does not have a name. We can just use the dimnames to give our column a name.
3.2 rbind()
If we have a matrix like the one above, we can use the rbind() function to add an more row to our matrix. Point the matrix, and the vector to add in, and you are done.
my_matrix=rbind(my_matrix,c('K','E','K'))
print(my_matrix)
You can also give two vectors to both rbind(), and cbind() to turn the two vectors into a matrix.
But, no one does that, because that’s dumb.
4 - Subsets
4.1 Rows and Columns
In R, we can either point the index of where our row, or column is located. Or, point the name of the row/column, to get it to fetch the line we are after. The format for R’s subsets is:
my_matrix[row_index,column_index,drop=FALSE]
We want drop=FALSE to be set in place, or R will keep trying to convert our column into a vector. That’s annoying af to deal with. In this example, we’ll fetch everything in column ‘B’, and everything in the 3rd column.
print(my_matrix[,c(2,3),drop=F])
In this example, we’ll get R to fetch the rows called X, and Y.
print(my_matrix[c('X','Y'),,drop=F])
4.2 Boolean Approach
For this example, we’ll want to work with a numeric matrix. Where all of the values are numeric.
my_matrix=matrix(1:12,nrow=4)
print(my_matrix)
Another way to subset in R is to use the [ ] brackets after our matrix, but don’t use any commas inside them. This will tell R to look at our matrix. Then, iterate over every single value there. And Finally, return every single value that would’ve returned True for our filter.
In this example, we’ll get R to fetch me all the values in our matrix that are even numbers.
5 - Immutability
Matrices in R are considered immutable. This means once created, they cannot be changed. The only way to alter them is to reassign the new value back into our old variable. This will overwrite the previous definition. We have already seen the rbind, and cbind do exactly that above.
When we do the Boolean approach, we can actually assign whatever the output would’ve been to a different value. This will tell R to do a mapping on our matrix.
Here, we’ll use the Boolean approach, and change every single even number to a 0.
my_matrix[my_matrix %%2 ==0]=0
print(my_matrix)
Note: R is smart enough to know where the original values came from, so we are good on that end.