Introduction to R

John M. Drake

What is R?


  • A “high level” computer language
  • A software platform for writing and executing programs in R

Value of R


  • Efficiently performs many, many numerical procedures
  • Open source and open access (free)
  • Available for all platforms
  • Readily parallelized for computationally intensive computations on high performance clusters
  • Strong user community
  • Early distribution of new methods

Statistically literate programming


The concept of literate programming

R Studio can be used to create static and dynamic reports including:

  • Documents
  • Presentations
  • Websites
  • Dashboards (R Shiny)

This presentation was made in R Studio

R and R Studio


R Studio is an integrated development environment for statistically literate programming in R

[Tour of the R Studio interface]

Project management


Simple

  • One directory for each project
  • Create a report using .Rmd (don't save multiple versions)
  • Run code in sequence or compile (don't use the command line)

More complicated

  • All of the above with version control using SVN or Github

Creating an R Markdown document


  1. Create a new R Markdown document
  2. Complete relevant information in the header
  3. Insert some narrative text to describe what this project is
  4. Insert a code chunk (note: lots of possible arguments to code chunks)
  5. Compile to html (pdf also okay, but may require latex)

It is good practice to load libraries and perform any other document setup in the first code chunk

Additional functionality with packages


Packages we will use in this course

  • deSolve
  • fields
  • lhs
  • sensitivity
  • bbmle

Packages can be installed using install.packages('package_name')

Packages can be loaded using the code snippet library(package_name)

Getting help

  • The ? command
  • Google
  • Stack Overflow

Example

?lm

Writing code


Some common tasks

  • Assigning variables
  • Reading data
  • Writing functions
  • Calling functions
  • Generating output (data, figures, statistical tables, etc.)

Control statements

Conditional execution

  • if
  • else

Grouping with curly braces {}

a<-5
if(a<1){
  b<-5
  c<-4
  } else {
    b<-10
  }

print(b)
[1] 10

Control statements

Looping

  • for
  • The apply family of functions
a<-c() #Note: use of `c` to create an empty object 
for(i in 1:10){
  a[i]<-i^3
  }
print(a)
 [1]    1    8   27   64  125  216  343  512  729 1000

Other useful tools


Recommended reading:

Wickham, H. & G. Grolemund. 2016. R for Data Science. O'Reilly.