Search icon CANCEL
Subscription
0
Cart icon
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
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

Table of Contents (17) Chapters

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

The apply, lapply, sapply, and tapply functions


R has some very handy functions such as apply, sapply, tapply, and mapply, that can be used to reduce the task of writing complicated statements. Also, using them makes our code look cleaner. The apply() function is similar to writing a loop statement.

The lapply() function is very similar to the apply() function but can be used on lists; this will return a list. The sapply() function is very similar to lapply() but returns a vector and not a list.

How to do it…

The apply() function can be used as follows:

mat= matrix(1:25, 5,5)
apply(mat,1,sd)

The lapply() function can be used in the following way:

j = list(x = 1:4, b = rnorm(100,1,2))
lapply(j,mean)

The tapply() function is useful when we have broken a vector into factors, groups, or categories:

tapply(mtcars$mpg,mtcars$gear,mean)

How it works…

The first argument in the apply() function is the data. The second argument takes two values: 1 and 2; if we state 1, R will perform a row-wise computation; if we mention 2, R will perform a column-wise computation. The third argument is the function. We would like to calculate the standard deviation of each row in R; hence we use the sd function as the third argument. Note that we can define our own function and replace it with the sd function.

With regard to the lapply() function, we have defined J as a list and would like to calculate the mean. The first argument in the lapply() function is the data and the second argument is the function used to process the data.

The first argument in the tapply() function is the data; in our case it is mpg. The second argument is the factor or the grouping; in this case it would be gears. The last argument is the function used to process the data. We would like to calculate the mean of mpg for each unique gear (3, 4, and 5 gears) in the mtcars data.

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 ₹800/month. Cancel anytime}