Since the release of version 1.0 in 2000, R's popularity as an environment for statistical computing, data analytics, and graphing has grown exponentially. People who have been using spreadsheets and need to perform things that spreadsheet packages cannot readily do, or need to handle larger data volumes than what a spreadsheet program can comfortably handle, are looking to R. Analogously, people using powerful commercial analytics packages are also intrigued by this free and powerful option. As a result, a large number of people are now looking to quickly get things done in R.
Being an extensible system, R's functionality is divided across numerous packages with each one exposing large numbers of functions. Even experienced users cannot expect to remember all the details off the top of their head. This cookbook, aimed at users who are already exposed to the fundamentals of R, provides ready recipes to perform many important data analytics tasks. Instead of having to search the Web or delve into numerous books when faced with a specific task, people can find the appropriate recipe and get going in a matter of minutes.
Chapter 1, Acquire and Prepare the Ingredients – Your Data, covers the activities that precede the actual data analysis task. It provides recipes to read data from different input file formats. Furthermore, prior to actually analyzing the data, we perform several preparatory and data cleansing steps and the chapter also provides recipes for these: handling missing values and duplicates, scaling or standardizing values, converting between numerical and categorical variables, and creating dummy variables.
Chapter 2, What's in There? – Exploratory Data Analysis, talks about several activities that analysts typically use to understand their data before zeroing in on specific techniques to apply. The chapter presents recipes to summarize data, split data, extract subsets, and create random data partitions, as well as several recipes to plot data to reveal underlying patters using standard plots as well as the lattice and ggplot2 packages.
Chapter 3, Where Does It Belong? – Classification, covers recipes for applying classification techniques. It includes classification trees, random forests, support vector machines, Naïve Bayes, K-nearest neighbors, neural networks, linear and quadratic discriminant analysis, and logistic regression.
Chapter 4, Give Me a Number – Regression, is about recipes for regression techniques. It includes K-nearest neighbors, linear regression, regression trees, random forests, and neural networks.
Chapter 5, Can You Simplify That? – Data Reduction Techniques, covers recipes for data reduction. It presents cluster analysis through K-means and hierarchical clustering. It also covers principal component analysis.
Chapter 6, Lessons from History – Time Series Analysis, covers recipes to work with date and date/time objects, create and plot time-series objects, decompose, filter and smooth time series, and perform ARIMA analysis.
Chapter 7, It's All About Your Connections – Social Network Analysis, is about social networks. It includes recipes to acquire social network data using public APIs, create and plot social networks, and compute important network metrics.
Chapter 8, Put Your Best Foot Forward – Document and Present Your Analysis, considers techniques to disseminate your analysis. It includes recipes to use R markdown and KnitR to generate reports, to use shiny to create interactive applications that enable your audience to directly interact with the data, and to create presentations with RPres.
Chapter 9, Work Smarter, Not Harder – Efficient and Elegant R Code, addresses the issue of writing efficient and elegant R code in the context of handling large data. It covers recipes to use the apply
family of functions, to use the plyr package, and to use data tables to slice and dice data.
Chapter 10, Where in the World? – Geospatial Analysis, covers the topic of exploiting R's powerful features to handle spatial data. It covers recipes to use RGoogleMaps to get GoogleMaps and to superimpose our own data on them, to import ESRI shape files into R and plot them, to import maps from the maps package, and to use the sp package to create and plot spatial data frame objects.
Chapter 11, Playing Nice – Connecting to Other Systems, covers the topic of interconnecting R to other systems. It includes recipes for interconnecting R with Java, Excel and with relational and NoSQL databases (MySQL and MongoDB respectively).
We have tested all the code in this book for R versions 3.0.2 (Frisbee Sailing) and 3.1.0 (Spring Dance). When you install or load some of the packages, you may get a warning message to the effect that the code was compiled for a different version, but this will not impact any of the code in this book.
This book is ideal for those who are already exposed to R, but have not yet used it extensively for data analytics and are seeking to get up and running quickly for analytics tasks. This book will help people who aspire to enhance their skills in any of the following ways:
perform advanced analyses and create informative and professional charts
become proficient in acquiring data from many sources
apply supervised and unsupervised data mining techniques
use R's features to present analyses professionally
In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows:
This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The read.csv()
function creates a data frame from the data in the .csv
file."
A block of code is set as follows:
> names(auto) [1] "No" "mpg" "cylinders" [4] "displacement" "horsepower" "weight" [7] "acceleration" "model_year" "car_name"
Any command-line input or output is written as follows:
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/server export MAKEFLAGS="LDFLAGS=-Wl,-rpath $JAVA_HOME/lib/server"
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>
, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
We have generated many of the data files used in this book. We have also used some publicly available data sets. The table below lists the sources of these public data sets. We downloaded most of the public data sets from the University of California at Irvine (UCI) Machine Learning Repository at http://archive.ics.uci.edu/ml/. In the table below we have indicated this as "Downloaded from UCI-MLR."
Data file name |
Source |
---|---|
|
Quinlan, R. Combining Instance-Based and Model-Based Learning, Machine Learning Proceedings on the Tenth International Conference 1993, 236-243, held at University of Massachusetts, Amherst published by Morgan Kaufmann. (Downloaded from UCI-MLR). |
|
D. Harrison and D.L. Rubinfeld, Hedonic prices and the demand for clean air, Journal for Environmental Economics a Management, pages 81–102, 1978. (Downloaded from UCI-MLR) |
|
Fanaee-T, Hadi, and Gama, Joao, Event labeling combining ensemble detectors and background knowledge, Progress in Artificial Intelligence (2013): pp. 1-15, Springer Berlin Heidelberg. (Downloaded from UCI-MLR) |
|
(Downloaded from UCI-MLR) |
|
Robust Regression and Outlier Detection, P. J. Rouseeuw and A. M. Leroy, Wiley, 1987. (Downloaded from UCI-MLR) |
|
Downloaded from Yahoo! Finance |
|
Downloaded from the US Bureau of Labor Statistics. |
|
Downloaded from Yahoo! Finance. |
|
NJ Department of Education's website and |
|
Adapted from Wikipedia: |
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/9065OS_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>
, and we will do our best to address the problem.