1 Extracting Data from a Matrix
In workshop week 6 you were introduced to subsetting a vector
using the square brackets [ ]. We also use this technique
to extract information from a matrix,
but we need to provide the row number and the column number
of the matrix element that we are interested in.
In general, to extract the value contained in the
row and
column cell of matrix , we would execute the command:
Using the matrices that you have created in this workshop,
execute the following extraction commands.
Remember that the row number is first and the column number is second.
Note that the number you put into the square brackets cannot be greater
than the dimension of the matrix. For example, the command C[ 2, 3 ]
produces the message Error: subscript out of bounds.
Note that we can obtain the dimensions of a matrix using the function
dim, e.g. dim(A) returns c(3,2).
Now that we know how to extract data from a matrix,
we can edit its content by using subsetting to identify a particular
element and assigning a new value to that position. For example:
-
-
-
Note that the number in the second row and second column had changed
from 2 to 4.