Norman Matloff University of California, Davis c 2007-8, N. Matloff December 4, 2008
IMPORTANT NOTICE: This document is no longer being maintained. My book on R programming, The Art of R Programming, is due out in August 2011. You are welcome to use an early draft at http://heather.cs.ucdavis.edu/˜matloff/132/NSPpart.pdf; it was about 50% complete and contains bugs, but should be useful. Licensing: This work, dated December 4, 2008, is licensed under a Creative Commons Attribution-No Derivative Works 3.0 United States License. Subsequent works by the author that make use of part or all of this material will not be covered by the license. Copyright is retained by N. Matloff in all non-U.S. jurisdictions.
1
Contents
Prerequisites 1 What Is R? 2 Why Use R for Your Statistical Work? 3 How to Run R 3.1 3.2 10 11 11 12
Interactive Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Running R in Batch Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 13 16 16
4 A First R Example Session (5 Minutes) 5 Functions: a Short Programming Example 6 Scalars, Vectors, Arrays and Matrices 6.1 6.2 6.3 6.4 6.5 6.6 6.7
“Declarations” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Generating Useful Vectors with “:”, seq() and rep() . . . . . . . . . . . . . . . . . . . . . . 17 Vector Arithmetic and Logical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Recycling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Vector Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Vector Element Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 6.7.1 6.7.2 6.7.3 General Operations . . . . . . . . . . . . . . . . . . . . .