If you have a matrix, how do you access a specific column or row in it?
We’ll use a small set of data that I will make below. I will one vectors comprised numbers from 1 to 9.
X <- c(1,2,3,4,5,6,7,8,9)
Next, let’s create a matrix and put our vector in it to create a 3x3 matrix.
example_matrix <- matrix(X,
byrow = T,
nrow = 3)
example_matrix
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
To access a specific full column or full row, we use brackets. Bracket notation is used to refer to row first, then column: [row #, column#]
#to access the entirety of one column
example_matrix[,1]
## [1] 1 4 7
#to access the entirety of one row
example_matrix[1,]
## [1] 1 2 3