Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Data Analysis with R

You're reading from   Data Analysis with R Click here to enter text.

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781785288142
Length 388 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (15) Chapters Close

Preface 1. RefresheR FREE CHAPTER 2. The Shape of Data 3. Describing Relationships 4. Probability 5. Using Data to Reason About the World 6. Testing Hypotheses 7. Bayesian Methods 8. Predicting Continuous Variables 9. Predicting Categorical Variables 10. Sources of Data 11. Dealing with Messy Data 12. Dealing with Large Data 13. Reproducibility and Best Practices Index

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 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 function matrix().

  > 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 matrix() with an optional argument, ncol, that specifies the number of columns.

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

We could have produced the same matrix by binding two vectors, c(1, 2, 3) and c(4, 5, 6) by columns using the cbind() function as follows:

  > a2.matrix <- cbind(c(1, 2, 3), c(4, 5, 6))

We could create the transposition of this matrix (where rows and columns are switched) by binding those vectors by row instead:

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

or by just using the matrix transposition function in R, t().

  > t(a2.matrix)

Some other functions that operate on whole vectors are rowSums()/colSums() and rowMeans()/colMeans().

  > a2.matrix
       [,1] [,2]
  [1,]    1    4
  [2,]    2    5
  [3,]    3    6
  > colSums(a2.matrix)
  [1]  6 15
  > rowMeans(a2.matrix)
  [1] 2.5 3.5 4.5

If vectors have sapply(), then matrices have apply(). The preceding two functions could have been written, more verbosely, as:

  > apply(a2.matrix, 2, sum)
  [1]  6 15
  > apply(a2.matrix, 1, mean)
  [1] 2.5 3.5 4.5

where 1 instructs R to perform the supplied function over its rows, and 2, over its columns.

The matrix multiplication operator in R is %*%

  > a2.matrix %*% a2.matrix
  Error in a2.matrix %*% a2.matrix : non-conformable arguments

Remember, matrix multiplication is only defined for matrices where the number of columns in the first matrix is equal to the number of rows in the second.

  > a2.matrix
       [,1] [,2]
  [1,]    1    4
  [2,]    2    5
  [3,]    3    6
  > a3.matrix
       [,1] [,2] [,3]
  [1,]    1    2    3
  [2,]    4    5    6
  > a2.matrix %*% a3.matrix
       [,1] [,2] [,3]
  [1,]   17   22   27
  [2,]   22   29   36
  [3,]   27   36   45
  >
  > # dim() tells us how many rows and columns
  > # (respectively) there are in the given matrix
  > dim(a2.matrix)
  [1] 3 2

To index the element of a matrix at the second row and first column, you need to supply both of these numbers into the subscripting operator.

  > a2.matrix[2,1]
  [1] 2

Many useRs get confused and forget the order in which the indices must appear; remember—it's row first, then columns!

If you leave one of the spaces empty, R will assume you want that whole dimension:

  > # returns the whole second column
  > a2.matrix[,2]
  [1] 4 5 6
  > # returns the first row
  > a2.matrix[1,]
  [1] 1 4

And, as always, we can use vectors in our subscript operator:

  > # give me element in column 2 at the first and third row
  > a2.matrix[c(1, 3), 2]
  [1] 4 6
You have been reading a chapter from
Data Analysis with R
Published in: Dec 2015
Publisher:
ISBN-13: 9781785288142
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime