Learning R

Book description

Learn how to perform data analysis with the R language and software environment, even if you have little or no programming experience. With the tutorials in this hands-on guide, youâ??ll learn how to use the essential R tools you need to know to analyze data, including data types and programming concepts.

The second half of Learning R shows you real data analysis in action by covering everything from importing data to publishing your results. Each chapter in the book includes a quiz on what youâ??ve learned, and concludes with exercises, most of which involve writing R code.

  • Write a simple R program, and discover what the language can do
  • Use data types such as vectors, arrays, lists, data frames, and strings
  • Execute code conditionally or repeatedly with branches and loops
  • Apply R add-on packages, and package your own work for others
  • Learn how to clean data you import from a variety of sources
  • Understand data through visualization and summary statistics
  • Use statistical models to pass quantitative judgments about data and make predictions
  • Learn what to do when things go wrong while writing data analysis code

Publisher resources

View/Submit Errata

Table of contents

  1. Learning R
  2. Preface
    1. About This Book
    2. What Is in This Book
    3. Which Chapters Should I Read?
    4. Conventions Used in This Book
    5. Goals, Summaries, Quizzes, and Exercises
    6. Using Code Examples
    7. Safari® Books Online
    8. How to Contact Us
    9. Acknowledgments
  3. I. The R Language
    1. 1. Introduction
      1. Chapter Goals
      2. What Is R?
      3. Installing R
      4. Choosing an IDE
        1. Emacs + ESS
        2. Eclipse/Architect
        3. RStudio
        4. Revolution-R
        5. Live-R
        6. Other IDEs and Editors
      5. Your First Program
      6. How to Get Help in R
      7. Installing Extra Related Software
      8. Summary
      9. Test Your Knowledge: Quiz
      10. Test Your Knowledge: Exercises
    2. 2. A Scientific Calculator
      1. Chapter Goals
      2. Mathematical Operations and Vectors
      3. Assigning Variables
      4. Special Numbers
      5. Logical Vectors
      6. Summary
      7. Test Your Knowledge: Quiz
      8. Test Your Knowledge: Exercises
    3. 3. Inspecting Variables and Your Workspace
      1. Chapter Goals
      2. Classes
      3. Different Types of Numbers
      4. Other Common Classes
      5. Checking and Changing Classes
      6. Examining Variables
      7. The Workspace
      8. Summary
      9. Test Your Knowledge: Quiz
      10. Test Your Knowledge: Exercises
    4. 4. Vectors, Matrices, and Arrays
      1. Chapter Goals
      2. Vectors
        1. Sequences
        2. Lengths
        3. Names
        4. Indexing Vectors
        5. Vector Recycling and Repetition
      3. Matrices and Arrays
        1. Creating Arrays and Matrices
        2. Rows, Columns, and Dimensions
        3. Row, Column, and Dimension Names
        4. Indexing Arrays
        5. Combining Matrices
        6. Array Arithmetic
      4. Summary
      5. Test Your Knowledge: Quiz
      6. Test Your Knowledge: Exercises
    5. 5. Lists and Data Frames
      1. Chapter Goals
      2. Lists
        1. Creating Lists
        2. Atomic and Recursive Variables
        3. List Dimensions and Arithmetic
        4. Indexing Lists
        5. Converting Between Vectors and Lists
        6. Combining Lists
      3. NULL
      4. Pairlists
      5. Data Frames
        1. Creating Data Frames
        2. Indexing Data Frames
        3. Basic Data Frame Manipulation
      6. Summary
      7. Test Your Knowledge: Quiz
      8. Test Your Knowledge: Exercises
    6. 6. Environments and Functions
      1. Chapter Goals
      2. Environments
      3. Functions
        1. Creating and Calling Functions
        2. Passing Functions to and from Other Functions
        3. Variable Scope
      4. Summary
      5. Test Your Knowledge: Quiz
      6. Test Your Knowledge: Exercises
    7. 7. Strings and Factors
      1. Chapter Goals
      2. Strings
        1. Constructing and Printing Strings
        2. Formatting Numbers
        3. Special Characters
        4. Changing Case
        5. Extracting Substrings
        6. Splitting Strings
        7. File Paths
      3. Factors
        1. Creating Factors
        2. Changing Factor Levels
        3. Dropping Factor Levels
        4. Ordered Factors
        5. Converting Continuous Variables to Categorical
        6. Converting Categorical Variables to Continuous
        7. Generating Factor Levels
        8. Combining Factors
      4. Summary
      5. Test Your Knowledge: Quiz
      6. Test Your Knowledge: Exercises
    8. 8. Flow Control and Loops
      1. Chapter Goals
      2. Flow Control
        1. if and else
        2. Vectorized if
        3. Multiple Selection
      3. Loops
        1. repeat Loops
        2. while Loops
        3. for Loops
      4. Summary
      5. Test Your Knowledge: Quiz
      6. Test Your Knowledge: Exercises
    9. 9. Advanced Looping
      1. Chapter Goals
      2. Replication
      3. Looping Over Lists
      4. Looping Over Arrays
      5. Multiple-Input Apply
        1. Instant Vectorization
      6. Split-Apply-Combine
      7. The plyr Package
      8. Summary
      9. Test Your Knowledge: Quiz
      10. Test Your Knowledge: Exercises
    10. 10. Packages
      1. Chapter Goals
      2. Loading Packages
        1. The Search Path
        2. Libraries and Installed Packages
      3. Installing Packages
      4. Maintaining Packages
      5. Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Exercises
    11. 11. Dates and Times
      1. Chapter Goals
      2. Date and Time Classes
        1. POSIX Dates and Times
        2. The Date Class
        3. Other Date Classes
      3. Conversion to and from Strings
        1. Parsing Dates
        2. Formatting Dates
      4. Time Zones
      5. Arithmetic with Dates and Times
      6. Lubridate
      7. Summary
      8. Test Your Knowledge: Quiz
      9. Test Your Knowledge: Exercises
  4. II. The Data Analysis Workflow
    1. 12. Getting Data
      1. Chapter Goals
      2. Built-in Datasets
      3. Reading Text Files
        1. CSV and Tab-Delimited Files
        2. Unstructured Text Files
        3. XML and HTML Files
        4. JSON and YAML Files
      4. Reading Binary Files
        1. Reading Excel Files
        2. Reading SAS, Stata, SPSS, and MATLAB Files
        3. Reading Other File Types
      5. Web Data
        1. Sites with an API
        2. Scraping Web Pages
      6. Accessing Databases
      7. Summary
      8. Test Your Knowledge: Quiz
      9. Test Your Knowledge: Exercises
    2. 13. Cleaning and Transforming
      1. Chapter Goals
      2. Cleaning Strings
      3. Manipulating Data Frames
        1. Adding and Replacing Columns
        2. Dealing with Missing Values
        3. Converting Between Wide and Long Form
        4. Using SQL
      4. Sorting
      5. Functional Programming
      6. Summary
      7. Test Your Knowledge: Quiz
      8. Test Your Knowledge: Exercises
    3. 14. Exploring and Visualizing
      1. Chapter Goals
      2. Summary Statistics
      3. The Three Plotting Systems
      4. Scatterplots
        1. Take 1: base Graphics
        2. Take 2: lattice Graphics
        3. Take 3: ggplot2 Graphics
      5. Line Plots
      6. Histograms
      7. Box Plots
      8. Bar Charts
      9. Other Plotting Packages and Systems
      10. Summary
      11. Test Your Knowledge: Quiz
      12. Test Your Knowledge: Exercises
    4. 15. Distributions and Modeling
      1. Chapter Goals
      2. Random Numbers
        1. The sample Function
        2. Sampling from Distributions
      3. Distributions
      4. Formulae
      5. A First Model: Linear Regressions
        1. Comparing and Updating Models
        2. Plotting and Inspecting Models
      6. Other Model Types
      7. Summary
      8. Test Your Knowledge: Quiz
      9. Test Your Knowledge: Exercises
    5. 16. Programming
      1. Chapter Goals
      2. Messages, Warnings, and Errors
      3. Error Handling
      4. Debugging
      5. Testing
        1. RUnit
        2. testthat
      6. Magic
        1. Turning Strings into Code
        2. Turning Code into Strings
      7. Object-Oriented Programming
        1. S3 Classes
        2. Reference Classes
      8. Summary
      9. Test Your Knowledge: Quiz
      10. Test Your Knowledge: Exercises
    6. 17. Making Packages
      1. Chapter Goals
      2. Why Create Packages?
      3. Prerequisites
      4. The Package Directory Structure
      5. Your First Package
      6. Documenting Packages
      7. Checking and Building Packages
      8. Maintaining Packages
      9. Summary
      10. Test Your Knowledge: Quiz
      11. Test Your Knowledge: Exercises
  5. III. Appendixes
    1. A. Properties of Variables
    2. B. Other Things to Do in R
    3. C. Answers to Quizzes
    4. D. Solutions to Exercises
  6. E. Bibliography
  7. Index
  8. About the Author
  9. Colophon
  10. Copyright

Product information

  • Title: Learning R
  • Author(s): Richard Cotton
  • Release date: September 2013
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449357108