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 measures—most 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.
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:
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 scratch—you don’t 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 doesn’t explain in great detail what they are. If you want a really introductory text on how to program, then Python for Kids by Jason R. Briggs 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. Garrett Grolemund’s Data Analysis with R picks up where this book leaves off, covering data analysis workflow in more detail.
A word of warning: this isn’t a reference book, and many of the topics aren’t covered in great detail. This book provides tutorials to give you ideas about what you can do in R and let you practice. There isn’t enough room to cover all 4,000 add-on packages, but by the time you’ve finished reading, you should be able to find the ones that you need, and get the help you need to start using them.
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, Chapter 4 covers vectors, matrices, and arrays) or a concept (for example, Chapter 8 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.
Here’s what you’ll find in Part I:
if
and else
), and basic looping.
apply
function and its variants.
Here are the topics covered in Part II:
Lastly, there are useful references in Part III:
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 R For Dummies. He suggested giving up and reading his book instead![1]
The following font conventions are used in this book:
Constant width
##
).
Constant width italic
There is a style guide for the code used in this book at http://4dpiecharts.com/r-code-style-guide.
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
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 you’ve learned. You also get a quiz, to make sure you’ve 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.
Supplemental material (code examples, exercises, etc.) is available for download at http://cran.r-project.org/web/packages/learningr.
This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Learning R by Richard Cotton (O’Reilly). Copyright 2013 Richard Cotton, 978-1-449-35710-8.”
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at [email protected].
Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.
Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.
Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc. |
1005 Gravenstein Highway North |
Sebastopol, CA 95472 |
800-998-9938 (in the United States or Canada) |
707-829-0515 (international or local) |
707-829-0104 (fax) |
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://oreil.ly/learningR.
To comment or ask technical questions about this book, send email to [email protected].
For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Many amazing people have helped with the making of this book, not least my excellent editor Meghan Blanchette, who is full of sensible advice.
Data was donated by several wonderful people:
Many other people sent me datasets; there wasn’t room for them all, but thank you anyway!
Bill Hogan also reviewed the book, as did Daisy Vincent of Marin Software, and JD Long. I don’t know where JD works, but he lives in Bermuda, so it probably involves triangles. Additional comments and feedback were provided by James White, Ben Hanks, Beccy Smith, and Guy Bourne of TDX Group; Alex Hogg and Adrian Kelsey of HSL; Tom Hull, Karen Vanstaen, Rachel Beckett, Georgina Rimmer, Ruth Wortham, Bernardo Garcia-Carreras, and Joana Silva of CEFAS; Tal Galili of Tel Aviv University; Garrett Grolemund of RStudio; and John Verzani of the City University of New York. David Maxwell of CEFAS wonderfully recruited more or less everyone else in CEFAS to review my book.
John Verzani also deserves much credit for helping conceive this book, and for providing advice on the structure.
Sanders Kleinfeld of O’Reilly provided great tech support when I was pulling my hair out over character encodings in the manuscript. Yihui Xie went above and beyond the call of duty helping me get knitr
to generate AsciiDoc. Rachel Head single-handedly spotted over 4,000 bugs, typos, and mistakes while copyediting.
Garib Murshudov was the lecturer who first taught me R, back in 2004.
Finally, Janette Bowler deserves a medal for her endless patience and support while I’ve been busy writing.
[1] Andrie’s book covers much the same ground as Learning R, and in many ways is almost as good as this work, so I won’t be offended if you want to read it too.