Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Data Analysis with R, Second Edition
  • Table Of Contents Toc
  • Feedback & Rating feedback
Data Analysis with R, Second Edition

Data Analysis with R, Second Edition - Second Edition

3.5 (2)
close
close
Data Analysis with R, Second Edition

Data Analysis with R, Second Edition

3.5 (2)

Overview of this book

Frequently the tool of choice for academics, R has spread deep into the private sector and can be found in the production pipelines at some of the most advanced and successful enterprises. The power and domain-specificity of R allows the user to express complex analytics easily, quickly, and succinctly. Starting with the basics of R and statistical reasoning, this book dives into advanced predictive analytics, showing how to apply those techniques to real-world data though with real-world examples. Packed with engaging problems and exercises, this book begins with a review of R and its syntax with packages like Rcpp, ggplot2, and dplyr. From there, get to grips with the fundamentals of applied statistics and build on this knowledge to perform sophisticated and powerful analytics. Solve the difficulties relating to performing data analysis in practice and find solutions to working with messy data, large data, communicating results, and facilitating reproducibility. This book is engineered to be an invaluable resource through many stages of anyone’s career as a data analyst.
Table of Contents (19 chapters)
close
close

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 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 these 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 

We can do this 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 follows:

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

Here, 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 that 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 

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
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Data Analysis with R, Second Edition
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon