R is a programming language and a software environment for data analysis and statistics. It is a GNU project, which means that it is free, open source software. It is growing exponentially by most measuresmost estimates count over a million users, and it has over 4,000 add-on packages contributed by the community, with that number increasing by about 25% each year. The Tiobe Programming Community Index of language popularity places it at number 24 at the time of this writing, roughly on a par with SAS and MATLAB.
R is used in almost every area where statistics or data analyses are needed. Finance, marketing, pharmaceuticals, genomics, epidemiology, social sciences, and teaching are all covered, as well as dozens of other smaller domains.
About This Book
Since R is primarily designed to let you do statistical analyses, many of the books written about R focus on teaching you how to calculate statistics or model datasets. This unfortunately misses a large part of the reality of analyzing data. Unless you are doing cutting-edge research, the statistical techniques that you use will often be routine, and the modeling part of your task may not be the largest one. The complete workflow for analyzing data looks more like this:
- Retrieve some data.
- Clean the data.
- Explore and visualize the data.
- Model the data and make predictions.
- Present or publish your results.
Of course at each stage your results may generate interesting questions that lead you to look for more data, or for a different way to treat your existing data, which can send you back a step. The workflow can be iterative, but each of the steps needs to be undertaken.
The first part of this book is designed to teach you R from scratchyou dont need any experience in the language. In fact, no programming experience at all is necessary, but if you have some basic programming knowledge, it will help. For example, the book explains how to comment your code and how to write a for
loop, but doesnt explain in great detail what they are. If you want a really introductory text on how to program, then is as good a place to start as any!
The second part of the book takes you through the complete data analysis workflow in R. Here, some basic statistical knowledge is assumed. For example, you should understand terms like mean and standard deviation , and what a bar chart is.
The book finishes with some more advanced R topics, like object-oriented programming and package creation. picks up where this book leaves off, covering data analysis workflow in more detail.
A word of warning: this isnt a reference book, and many of the topics arent covered in great detail. This book provides tutorials to give you ideas about what you can do in R and let you practice. There isnt enough room to cover all 4,000 add-on packages, but by the time youve finished reading, you should be able to find the ones that you need, and get the help you need to start using them.
What Is in This Book
This is a book of two halves. The first half is designed to provide you with the technical skills you need to use R; each chapter is a short introduction to a different set of data types (for example, covers branching and looping).
The second half of the book ramps up the fun: you get to see real data analysis in action. Each chapter covers a section of the standard data analysis workflow, from importing data to publishing your results.
Heres what youll find in :
- , tells you how to install R and where to get help.
- , shows you how to use R as a scientific calculator.
- , lets you inspect variables in different ways.
- , covers vectors, matrices, and arrays.
- , covers lists and data frames (for spreadsheet-like data).
- , covers environments and functions.
- , covers strings and factors (for categorical data).
- , covers branching (
if
and else
), and basic looping. - , covers advanced looping with the
apply
function and its variants. - , explains how to install and use add-on packages.
- , covers dates and times.
Here are the topics covered in :
- , shows you how to import data into R.
- , explains cleaning and manipulating data.
- , lets you explore data by calculating statistics and plotting.
- , introduces modeling.
- , covers a variety of advanced programming techniques.
- , shows you how to package your work for others.
Lastly, there are useful references in :
- , contains tables comparing the properties of different types of variables.
- , describes some other things that you can do in R.
- , contains the answers to the end-of-chapter quizzes.
- , contains the answers to the end of chapter programming exercises.
Which Chapters Should I Read?
If you have never used R before, then start at the beginning and work through chapter by chapter. If you already have some experience with R, you may wish to skip the first chapter and skim the chapters on the R core language.
Each chapter deals with a different topic, so although there is a small amount of dependency from one chapter to the next, it is possible to pick and choose chapters that interest you.
I recently discussed this matter with Andrie de Vries, author of ]
Conventions Used in This Book
The following font conventions are used in this book:
Italic Indicates new terms, URLs, email addresses, file and pathnames, and file extensions. Constant width
Used for code samples that should be copied verbatim, as well as within paragraphs to refer to program elements such as variable or function names, data types, environment variables, statements, and keywords. Output from blocks of code is also in constant width, preceded by a double hash (##
). Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.
There is a style guide for the code used in this book at http://4dpiecharts.com/r-code-style-guide.
Tip
This icon signifies a tip, suggestion, or general note.
Warning
This icon indicates a warning or caution.
Goals, Summaries, Quizzes, and Exercises
Each chapter begins with a list of goals to let you know what to expect in the forthcoming pages, and finishes with a summary that reiterates what youve learned. You also get a quiz, to make sure youve been concentrating (and not just pretending to read while watching telly). The answers to the questions can be found within the chapter (or at the end of the book, if you want to cheat). Finally, each chapter concludes with some exercises, most of which involve you writing some R code. After each exercise description there is a number in square brackets, denoting a generous estimate of how many minutes it might take you to complete it.