O'Reilly logo

Data Analysis with R - Second Edition by Tony Fischetti

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Matrices

In addition to the vector data structure, R has the matrix, data frame, list, and array data structures. Though we will be using all of these types (except arrays) in this book, we only need to review the first two in this chapter.

A matrix in R, like in math, is a rectangular array of values (of one type) arranged in rows and columns and can be manipulated as a whole. Operations on matrices are fundamental to data analysis.

One way of creating a matrix is to just supply a vector to the matrix() function:

 > a.matrix <- matrix(c(1, 2, 3, 4, 5, 6)) 
 > a.matrix 
       [,1] 
  [1,]    1  
  [2,]    2  
  [3,]    3  
  [4,]    4  
  [5,]    5  
  [6,]    6 

This produces a matrix with all the supplied values in a single column. We can make a similar matrix with two columns by supplying ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required