Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Beginning R pptx
Nội dung xem thử
Mô tả chi tiết
www.it-ebooks.info
www.it-ebooks.info
Beginning R
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Chapter 1 Introducing R: What It Is and How to Get It . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2 Starting Out: Becoming Familiar with R . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Chapter 3 Starting Out: Working With Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Chapter 4 Data: Descriptive Statistics and Tabulation . . . . . . . . . . . . . . . . . . . . . . . 107
Chapter 5 Data: Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Chapter 6 Simple Hypothesis Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Chapter 7 Introduction to Graphical Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Chapter 8 Formula Notation and Complex Statistics . . . . . . . . . . . . . . . . . . . . . . . 263
Chapter 9 Manipulating Data and Extracting Components . . . . . . . . . . . . . . . . . 295
Chapter 10 Regression (Linear Modeling) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Chapter 11 More About Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Chapter 12 Writing Your Own Scripts: Beginning to Program . . . . . . . . . . . . . . . . . 415
Appendix Answers to Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
www.it-ebooks.info
www.it-ebooks.info
Beginning
R
The Statistical Pr ogramming Language
www.it-ebooks.info
www.it-ebooks.info
Beginning
R
The Statistical Programming Language
Mark Gardener
www.it-ebooks.info
Beginning R: The Statistical Programming Language
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2012 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-16430-3
ISBN: 978-1-118-22616-2 (ebk)
ISBN: 978-1-118-23937-7 (ebk)
ISBN: 978-1-118-26412-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 Web site may provide or recommendations it may make.
Further, readers should be aware that Internet Web sites 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: 2012937909
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress 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.
www.it-ebooks.info
It is much easier to be critical than to be correct.
— Benjamin Disraeli
www.it-ebooks.info
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
Craig Woods, Happenstance Type-O-Rama
Proofr eader
James Saturnio, Word One
Indexer
John Sleeva
Cover Designer
LeAndra Young
Cover Image
© iStock / Mark Wragg
Credits
www.it-ebooks.info
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.
www.it-ebooks.info
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 asking me to do this book, including Paul Reese, Carol Long, and Victoria Swider. I couldn’t
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 I have managed
to distill this knowledge into a worthy package for future devotees of R.
— Mark Gardener
www.it-ebooks.info
Contents
Introduction xxi
Chapter 1: Introducing R: What It Is and How to Get It 1
Getting the Hang of R 2
The R Website 3
Downloading and Installing R from CRAN 3
Installing R on Your Windows Computer 4
Installing R on Your Macintosh Computer 7
Installing R on Your Linux Computer 7
Running the R Program 8
Finding Your Way with R 10
Getting Help via the CRAN Website and the Internet 10
The Help Command in R 10
Help for Windows Users 11
Help for Macintosh Users 11
Help for Linux Users 13
Help For All Users 13
Anatomy of a Help Item in R 14
Command Packages 16
Standard Command Packages 16
What Extra Packages Can Do for You 16
How to Get Extra Packages of R Commands 18
How to Install Extra Packages for Windows Users 18
How to Install Extra Packages for Macintosh Users 18
How to Install Extra Packages for Linux Users 19
Running and Manipulating Packages 20
Loading Packages 21
Windows-Specific Package Commands 21
Macintosh-Specific Package Commands 21
Removing or Unloading Packages 22
Summary 22
Chapter 2: Starting Out: Becoming Familiar with R 25
Some Simple Math 26
Use R Like a Calculator 26
Storing the Results of Calculations 29
Contents
Chapter 1: Credit
Chapter 2: About the Author
Chapter 3: Acknowledgment
ion
Who This Book Is For
Is Structured
Need to Use This Book
Errata
Chapter 4: Introducing R: What It Is and How to Get It
Getting the Hang of R
Chapter 5: Starting Out: Becoming Familiar with R
Getting Data into R
Named Objects
Items
Items
Examining Data Structure
www.it-ebooks.info
xii
CONTENTS Reading and
Getting Data into R 30
Using the combine Command for Making Data 30 Entering Numerical Items as Data 30 Entering Text Items as Data 31 Using the scan Command for Making Data 32 Entering Text as Data 33 Using the Clipboard to Make Data 33 Reading a File of Data from a Disk 35 Reading Bigger Data Files 37 The read.csv() Command 37 Alternative Commands for Reading Data in R 39 Missing Values in Data Files 40
Viewing
Named Objects 41
Viewing Previously Loaded Named-Objects 42 Viewing All Objects 42 Viewing Only Matching Names 42 Removing Objects from R 44
Types of Data
Items 45
Number Data 45 Text Items 45
Converting Between Number and Text Data 46
The Structure of Data
Items 47
Vector Items 48 Data Frames 48 Matrix Objects 49 List Objects 49
Examining Data Structure 49 Working with History Commands 51
Using History Files 52 Viewing the Previous Command History 52 Saving and Recalling Lists of Commands 52 Alternative History Commands in Macintosh OS 52 Editing History Files 53
Saving Your Work in R 54
Saving the Workspace on Exit 54 Saving Data Files to Disk 54 Save Named Objects 54 Save Everything 55 Reading Data Files from Disk 56 Saving Data to Disk as Text Files 57 Writing Vector Objects to Disk 58 Writing Matrix and Data Frame Objects to Disk 58
www.it-ebooks.info
xiii
CONTENTS Writing List Objects to Disk 59
Converting List Objects to Data Frames 60
Summary 61
Chapter 3: Starting Out: Working
With Ob jects 65
Manipulating Objects 65
Manipulating Vectors 66
Selecting and Displaying Parts of a Vector 66
Sorting and Rearranging a Vector 68
Returning Logical Values from a Vector 70
Manipulating Matrix and Data Frames 70
Selecting and Displaying Parts of a Matrix or Data Frame 71
Sorting and Rearranging a Matrix or Data Frame 74
Manipulating Lists 76
Viewing Objects within Objects 77
Looking Inside Complicated Data Objects 77
Opening Complicated Data Objects 78
Quick Looks at Complicated Data Objects 80
Viewing and Setting Names 82
Rotating Data Tables 86
Constructing Data Objects 86
Making Lists 87
Making Data Frames 88
Making Matrix Objects 89
Re-ordering Data Frames and Matrix Objects 92
Forms of Data Objects: Testing and Converting 96
Testing to See What Type of Object You Have 96
Converting from One Object Form to Another 97
Convert a Matrix to a Data Frame 97
Convert a Data Frame into a Matrix 98
Convert a Data Frame into a List 99
Convert a Matrix into a List 100
Convert a List to Something Else 100
Summary 104
Chapter 4: Data: Descriptive Statistics and Tabulation 107
Summary Commands 108
Summarizing Samples 110
Summary Statistics for Vectors 110
Summary Commands With Single Value Results 110
Summary Commands With Multiple Results 113
www.it-ebooks.info