Press "Enter" to skip to content

Subsetting Matrices In R

Dave Mason continues his look at matrices in R:

We can extract an entire row from a matrix. To do this, specify the desired row only within the square brackets [ ]. The placeholder where you would otherwise specify the column is left empty.

> #Points scored by Kendrick Perkins.
> points_scored_by_quarter[1,]
1st 2nd 3rd 4th 2 2 6 0
> points_scored_by_quarter["Perkins",]
1st 2nd 3rd 4th 2 2 6 0 

Conversely, we can extract a column from a matrix. Specify the column within the square brackets¬†[ ]and omit the row. The result is a vector, thus the pivot effect–the row names are displayed in the output (not the column name).

Dave points out that working with matrices is basically an extension of working with vectors.