An Introduction to R Draft 2

An Introduction to R Draft 2

An Introduction to R Draft 2 The Berkeley R Project Department of Statistics, UC Berkeley VIGRE John Jimenez Devon Shurick The Berkeley R Project The Berkeley R Project [email protected] [email protected] December 27, 2008 1 CONTENTS CONTENTS Contents 1 Introduction 2 1.1 What is R? . 2 1.2 What can I do with R? . 2 1.3 How Can I Get a Copy? . 4 1.4 The Focus of this Tutorial, and other References . 4 2 Objects 6 2.1 Introduction . 6 2.2 Numbers . 7 2.3 Vectors . 8 2.3.1 Creating Vectors . 8 2.3.2 Logical and Comparison Operators . 9 2.3.3 Indexing Vectors . 10 2.3.4 Vector Arithmetic . 11 2.4 Matrices . 11 2.5 More on indexing . 12 2.5.1 Random Sampling, with sample and more on Matrices . 13 2.5.2 Introduction to apply ............................... 16 2.6 Strings . 16 2.6.1 Manipulating Strings . 17 2.7 Factors . 18 2.8 List . 19 2.9 Data Frames . 21 2.9.1 attach and detach ................................ 23 2.10 Tables . 23 2.11 Operators . 24 2.12 Other Things You Must Know About R . 24 2.13 Functions . 26 2.13.1 Examples . 26 3 Plotting 32 3.1 Histograms . 32 3.1.1 colors . 34 3.2 Box-Plots : . 34 3.3 Scatter-Plots : . 35 3.4 Putting Multiple Graphs in One Figure . 38 3.4.1 par . 38 3.4.2 split.screen . 39 3.4.3 Replacing a Screen . 39 3.4.4 Split Screens within Split Screens . 41 3.5 abline . 42 3.6 lines . 43 3.7 Putting it all together . 44 3.8 QQ-Plots : . 45 3.8.1 qqnorm : . 45 3.8.2 qqmath (requires installation of lattice package) : . 46 3.8.3 qqplot : . 47 3.8.4 Adjusting the qqline . 47 3.9 Linear Regression . 47 2 CONTENTS CONTENTS 3.9.1 Additional Information . 52 3.10 Saving Plots . 52 3.11 Adding Text : . 52 3.11.1 text ........................................ 52 3.12 Fine Tuning Plots . 55 3.12.1 mtext ........................................ 55 3.12.2 par revisited . 56 4 Packages 57 4.1 Introduction . 57 4.2 Listing Loaded and Installed Packages . 57 4.2.1 .packages ..................................... 58 4.2.2 library ....................................... 59 4.3 Listing all R packages . 59 4.4 Installing Packages . 60 4.4.1 From the Command Line : 60 4.4.2 Using a Graphical User Interface (GUI) 60 4.5 Removing Packages . 60 5 Setting up your working environment and profile 61 6 Help R help you 61 6.1 help.start . 61 6.2 help . 62 6.2.1 Graphical User Interface (GUI) Specific Tip . 63 6.2.2 Mac OS X . 63 6.2.3 Windows OS . 63 6.3 apropos and find . 63 6.3.1 searching for functions and their packages . 63 6.3.2 searching for lost objects : 64 6.4 help.search . 64 6.5 RSiteSearch . 66 6.6 Summary . 66 7 Reading Data Into R 66 7.1 read.table . 66 7.1.1 setwd . 69 7.1.2 An Example of Cleaning Data . 69 7.2 The Rest of the read Family: . 71 7.3 scan . 72 7.4 Missing Values . 72 8 Probability 72 8.1 Binomial . 73 8.2 Normal Distribution . 74 3 LIST OF FIGURES LIST OF FIGURES 9 Scripting 78 9.0.1 From the Command Line . 78 9.0.2 Mac : From the GUI . 80 9.0.3 Windows : From the GUI . 80 9.0.4 source . 80 10 Functions 80 10.1 Introduction . 80 10.2 Functional Form . 81 10.3 Naming Functions . 81 10.4 Functions and Loops . 82 10.4.1 for loops . 82 10.4.2 On the Myth of loops being bad form in R ................... 84 10.4.3 repeat loops . 84 10.4.4 replicate . 85 10.5 Examples of Homemade Functions : . 86 10.5.1 Uploading a set of Packages . 86 10.5.2 Adjusting the QQ-line for Comparisons with Theoretical Distributions . 86 10.5.3 adjusting qqline for two sample comparison . 87 10.5.4 A More Adjustable QQ-line : . ..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    95 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us