R tutorial - Learn How to Subset Matrices in R

Learn how to extract elements, rows, and columns from matrices in R using bracket notation. The tutorial covers single elements, whole rows or columns, multiple selections, and logical filtering, showing how R's column-major indexing works when you use a single index.
Chapters
Just as for vectors, there are situations in which you want to select single elements or entire parts of a matrix to continue your analysis with. Again, you can use square brackets for this, but the fact that you're dealing with two dimensions now complicates things a bit. Have a look at this matrix containing some random numbers. If you want to select a single element from the matrix, you'll have to specify both the row and the column of the element of interest. Suppose we want to select the number 15 located at the first row and the third column. We type M open brackets, 1 comma, 3 close brackets. As you can probably tell, the first index refers to the row, the second one refers to the column. Likewise, to select the number one at row three and column two, we write the following line. Works like a charm. Notice that the results are single values, so actually vectors of length one. Now what if you want to select an entire row or an entire column from this matrix? You can do this by letting out some of the indices between square brackets. Instead of writing 3 comma 2 inside square brackets, to select the element at row 3 and column 2, you can leave out the 2 and keep the 3 comma part. Now you select all elements that are in row three, namely 6, 1, 4 and 2. Notice here that the result is not a matrix anymore. It's also a vector, but this time one that contains more than one element. You selected a single row from the matrix. So a vector suffices to store this one dimensional information. To select columns, you can work similarly, but this time the index that comes before the comma should be removed. To select the entire third column, you should write M open brackets, comma, three close brackets. Again, a vector result, this time of length 3 corresponding to the third column of M. Now, what happens when you decide not to include a comma? To clearly discern between column and row indices, let's simply try it out and see if we can explain it. Suppose you simply type m and then 4 inside square brackets. The result is 11. How did R get to that? Well, when you pass a single index to subset, a matrix, R simply goes through the matrix column by column from left to right. The first index is then 5, the second one 12, the third one 6, and the fourth one is 11 in the next column. This means that if we pass M9, we should should get four in the third row and the third column. Correct. There aren't a lot of cases in which using a single index without commas is useful, but I just wanted to point out that the comma is really Crucial here. In vector subsetting, you also learned how to select multiple elements in matrices. This is of course also possible, and the principles are just the same. Say, for example, you want to select a value 14 and 8 in the middle of the matrix. This command will do that for you. You select elements that are on the second row and on the second and third column. Again, the result is a vector, because one dimension suffices. Beware. However, you can select elements that don't have one of row or column index in common. If you Want to select 11 on row 1 and column 2, add and 8 on row 2 and column 3, this column will not give the wanted result. Instead, a submatrix gets returned that spans the elements on row one and two and column two and three. These submatrices can also be built up from disjoint places in your matrix. Creating a submatrix that contains elements on row one and three and columns one, three and four, for example, would look like this. Now remember these other ways of performing subsetting. By using names and with logical vectors. These work just as well for matrices. Let's first have a look at subsetting by names. We'll have to name the matrix for that, of course. In fact, subsetting by name works exactly the same as by index, but you just replace the indices with the corresponding names. To select eight, for example, you could use row index two and column index three. Or use the row names R2 and column name C. You can even use a combination of both. Just remember to surround the row and column names with quotes. Selecting multiple elements and sub matrices from a matrix is straightforward as well. To select elements on row R3 and in the last two columns, you can use this line. Finally, you can also use logical vectors. Again, the same rules apply. Rows and columns corresponding to a true are kept, while those corresponding to a false are left out. To select the same elements as in the previous call, you can use this command. The rules of vector recycling also apply here. Suppose that you only pass a vector of length 2 to perform a selection on the columns. The column selection vector gets recycled to false, true, false, false and true, giving the same result as before. As you can see, there's nothing new under the matrix sum apart from the additional dimension. When you compare two vectors, all different techniques to perform subsetting and all of the technicalities remain the same.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







