Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
R Data Visualization Cookbook

You're reading from  R Data Visualization Cookbook

Product type Book
Published in Jan 2015
Publisher
ISBN-13 9781783989508
Pages 236 pages
Edition 1st Edition
Languages
Toc

Table of Contents (17) Chapters close

R Data Visualization Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. A Simple Guide to R 2. Basic and Interactive Plots 3. Heat Maps and Dendrograms 4. Maps 5. The Pie Chart and Its Alternatives 6. Adding the Third Dimension 7. Data in Higher Dimensions 8. Visualizing Continuous Data 9. Visualizing Text and XKCD-style Plots 10. Creating Applications in R Index

Matrices in R


In this recipe, we will dive into R's capability with regard to matrices.

How to do it…

A vector in R is defined using the c() notation as follows:

vec = c(1:10)

A vector is a one-dimensional array. A matrix is a multidimensional array. We can define a matrix in R using the matrix() function. Alternatively, we can also coerce a set of values to be a matrix using the as.matrix() function:

mat = matrix(c(1,2,3,4,5,6,7,8,9,10),nrow = 2, ncol = 5)
mat

To generate a transpose of a matrix, we can use the t() function:

t(mat) # transpose a matrix

In R, we can also generate an identity matrix using the diag() function:

d = diag(3) # generate an identity matrix

We can nest the rep () function within matrix() to generate a matrix with all zeroes as follows:

zro = matrix(rep(0,6),ncol = 2,nrow = 3 )# generate a matrix of Zeros
zro

How it works…

We can define our data in the matrix () function by specifying our data as its first argument. The nrow and ncol arguments are used to specify the number of rows and column in a matrix. The matrix function in R comes with other useful arguments and can be studied by typing ?matrix in the R command window.

The rep() function nested in the matrix() function is used to repeat a particular value or character string a certain number of times.

The diag() function can be used to generate an identity matrix as well as extract the diagonal elements of a matrix. More uses of the diag() function can be explored by typing ?diag in the R console window.

The code file provides a lot more functions that can used along with matrices—for example, functions related to finding a determinant or inverse of a matrix and matrix multiplication.

You have been reading a chapter from
R Data Visualization Cookbook
Published in: Jan 2015 Publisher: ISBN-13: 9781783989508
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 €14.99/month. Cancel anytime}