Data science has taken the world by storm. Every field of study and area of business has been affected as people increasingly realize the value of the incredible quantities of data being generated. But to extract value from those data, one needs to be trained in the proper data science skills. The R programming language has become the de facto programming language for data science. Its flexibility, power, sophistication, and expressiveness have made it an invaluable tool for data scientists around the world. 

This book is about the fundamentals of R programming. You will get started with the basics of the language, learn how to manipulate datasets, how to write functions, and how to debug and optimize code. With the fundamentals provided in this book, you will have a solid foundation on which to build your data science toolbox.

If you are interested in a printed copy of this book, you can purchase one at Lulu.

Roger D. Peng

Roger D. Peng is an Associate Professor of Biostatistics at the Johns Hopkins Bloomberg School of Public Health. He is also a Co-Founder of the Johns Hopkins Data Science Specialization, which has enrolled over 1.5 million students, and the Simply Statistics blog where he writes about statistics for the general public. Roger can be found on Twitter and GitHub @rdpeng.

  • Preface

  • History and Overview of R

    • What is R?
    • What is S?
    • The S Philosophy
    • Back to R
    • Basic Features of R
    • Free Software
    • Design of the R System
    • Limitations of R
    • R Resources
  • Getting Started with R

    • Installation
    • Getting started with the R interface
  • R Nuts and Bolts

    • Entering Input
    • Evaluation
    • R Objects
    • Numbers
    • Attributes
    • Creating Vectors
    • Mixing Objects
    • Explicit Coercion
    • Matrices
    • Lists
    • Factors
    • Missing Values
    • Data Frames
    • Names
    • Summary
  • Getting Data In and Out of R

    • Reading and Writing Data
    • Reading Data Files with read.table()
    • Reading in Larger Datasets with read.table
    • Calculating Memory Requirements for R Objects
  • Using the readr Package

  • Using Textual and Binary Formats for Storing Data

    • Using dput() and dump()
    • Binary Formats
  • Interfaces to the Outside World

    • File Connections
    • Reading Lines of a Text File
    • Reading From a URL Connection
  • Subsetting R Objects

    • Subsetting a Vector
    • Subsetting a Matrix
    • Subsetting Lists
    • Subsetting Nested Elements of a List
    • Extracting Multiple Elements of a List
    • Partial Matching
    • Removing NA Values
  • Vectorized Operations

    • Vectorized Matrix Operations
  • Dates and Times

    • Dates in R
    • Times in R
    • Operations on Dates and Times
    • Summary
  • Managing Data Frames with the dplyr package

    • Data Frames
    • The dplyr Package
    • dplyr Grammar
    • Installing the dplyr package
    • select()
    • filter()
    • arrange()
    • rename()
    • mutate()
    • group_by()
    • %>%
    • Summary
  • Control Structures

    • if-else
    • for Loops
    • Nested for loops
    • while Loops
    • repeat Loops
    • next, break
    • Summary
  • Functions

    • Functions in R
    • Your First Function
    • Argument Matching
    • Lazy Evaluation
    • The ... Argument
    • Arguments Coming After the ... Argument
    • Summary
  • Scoping Rules of R

    • A Diversion on Binding Values to Symbol
    • Scoping Rules
    • Lexical Scoping: Why Does It Matter?
    • Lexical vs. Dynamic Scoping
    • Application: Optimization
    • Plotting the Likelihood
    • Summary
  • Coding Standards for R

  • Loop Functions

    • Looping on the Command Line
    • lapply()
    • sapply()
    • split()
    • Splitting a Data Frame
    • tapply
    • apply()
    • Col/Row Sums and Means
    • Other Ways to Apply
    • mapply()
    • Vectorizing a Function
    • Summary
  • Debugging

    • Something’s Wrong!
    • Figuring Out What’s Wrong
    • Debugging Tools in R
    • Using traceback()
    • Using debug()
    • Using recover()
    • Summary
  • Profiling R Code

    • Using system.time()
    • Timing Longer Expressions
    • The R Profiler
    • Using summaryRprof()
    • Summary
  • Simulation

    • Generating Random Numbers
    • Setting the random number seed
    • Simulating a Linear Model
    • Random Sampling
    • Summary
  • Data Analysis Case Study: Changes in Fine Particle Air Pollution in the U.S.

    • Synopsis
    • Loading and Processing the Raw Data
    • Results