The Essential R Reference
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright 2013 by Mark Gardener
Published by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-39141-9
ISBN: 978-1-118-39140-2 (ebk)
ISBN: 978-1-118-39138-9 (ebk)
ISBN: 978-1-118-39139-6 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions .
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or website may provide or recommendations it may make. Further, readers should be aware that Internet websites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com . For more information about Wiley products, visit www.wiley.com .
Library of Congress Control Number: 2012948918
Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.
There's only one corner of the universe you can be certain of improving, and that's your own self.
Aldous Huxley
About the Author
Mark Gardener ( http://www.gardenersown.co.uk ) is an ecologist, lecturer, and writer working in the UK. He has a passion for the natural world and for learning new things. Originally he worked in optics, but returned to education in 1996 and eventually gained his doctorate in ecology and evolutionary biology. This work involved a lot of data analysis and he became interested in R as a tool to help in research. He is currently self-employed and runs courses in ecology, data analysis, and R for a variety of organizations. Mark lives in rural Devon with his wife Christine (a biochemist) and still enjoys the natural world and learning new things.
About the Technical Editor
Richard Rowe started his professional life as a physicist, but switched fields to earn a PhD in insect behavior. He has taught data analysis courses, mainly to biologists, at Canterbury, then James Cook University, since 1982. He has worked with R since 1997 when a friend forced a very early copy onto him. The R system has exponentially improved over the past decade, and in light of the fact that Richards individual capacity is more linear, he retired in 2011 but keeps his hand in data-analysis consultancies and master-class workshops regularly (the best way to learn is to teach). Based on life, his belief is that ecological and behavioral data is usually the dirtiest and most ill-behaved, and hence the most fun to explore for pattern. His other hobby is dragonfly biology.
Credits
Executive Editor
Carol Long
Project Editor
Victoria Swider
Technical Editor
Richard Rowe
Production Editor
Kathleen Wisor
Copy Editor
Kim Cofer
Editorial Manager
Mary Beth Wakefield
Freelancer Editorial Manager
Rosemarie Graham
Associate Director of Marketing
David Mayhew
Marketing Manager
Ashley Zurcher
Business Manager
Amy Knies
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Neil Edde
Associate Publisher
Jim Minatel
Project Coordinator, Cover
Katie Crocker
Compositor
Jeff Lytle, Happenstance Type-O-Rama
Proofreader
James Saturnio, Word One
Indexer
Jack Lewis
Cover Designer
Ryan Sneed
Acknowledgments
First of all my thanks go out to the R project team and the many authors and programmers who work tirelessly to make this a peerless program. I would also like to thank my wife, Christine, who has had to put up with me during this entire process, and in many senses became an R-widow! Thanks to Wiley, for helping this book become a reality, especially Carol Long and Victoria Swider. I couldnt have done it without you. Thanks also to Richard Rowe, the technical reviewer, who first brought my attention to R and its compelling (and rather addictive) power.
Last but not least, thanks to the R community in general. I learned to use R largely by trial and error and using the vast wealth of knowledge that is in this community. I hope that this book is a worthwhile addition to the R knowledge base and that it will prove useful to all users of R.
Mark Gardener
Introduction
R is rapidly becoming the de facto standard among professionals, and is used in every conceivable discipline from science and medicine to business and engineering. R is more than just a computer program; it is a statistical programming environment and language. R is free and open source and is, therefore, available to everyone with a computer.
R is a language with its own vocabulary and grammar. To make R work for you, you communicate with the computer using the language of R and tell it what to do. You accomplish this by typing commands directly into the program. This means that you need to know some of the words of the language and how to put them together to make a sentence that R understands. This book aims to help with this task by providing a dictionary of words that R understands.
Next page