Workshops‎ > ‎

Programming in R

Monday, February 6 & 13, 2012  14h-16h
Ben Haller

Break out of  writing simple linear scripts (do this, then do this, then do this...) and learn to exploit the full power of R as a programming language, using control structures such as if statements, loops, and functions.  Learn how to use R's most powerful features, including subsetting and vectorization, to easily and efficiently slice your data.  Avoid pitfalls that will make your code buggy and your analyses incorrect.  Find bugs in your code, track down errors, and understand and fix problems that cause warnings.  Ultimately, learn how to write better code in less time with fewer bugs — code that you can reuse in future analyses and even share with others. Become an R programmer!

NOTE: It would be a good idea, although not required, to print out a copy of Tom Short's R reference card and bring it to the workshop.  It's very helpful for finding commands and figuring out what they do.


February 6th:
  • Flow control (if/else and loops)
  • Subsetting and sorting (indices and logical vectors, which, order)
  • Writing functions (including multiple arguments, defaults, and ...)
February 13th:
  • Coding for speed (profiling your code, vectorization and other methods)
  • Debugging (various methods, plus defensive coding techniques)
  • Caveats of R (some very common problems that can bite you)
The first workshop contains mostly introductory topics, aimed at those learning to program for the first time.  The second workshop covers more advanced topics, but it is still paced for new programmers.  The intention is that those new to programming in R will find both workshops useful, while those who have already done some programming in R and other languages may wish to skip the first workshop.

Learning Objectives

  • Write R code that is reusable, shareable, and bug-free
  • Make slow analyses run faster
  • Be able to understand (and even modify!) the code in R's packages
  • Avoid copy/paste errors in repetitive analyses using loops and functions
  • Use subsetting and sorting to slice and dice your data efficiently
  • Avoid common pitfalls of R that lurk in the shadows
  • Find out where to look for more in-depth information on programming in R
This material is divided into two workshops to allow sufficient time; we hope that this will allow the material to be covered at a pace that is not too fast for the typical beginning R user.


These workshops are for the R user with at least a small amount of experience.  You should have:
  • gone to some introductory workshops, or learned from a book such as Crawley's The R Book
  • used an R environment such as R-Studio
  • loaded a dataset and done a simple analysis
  • made a plot or two
The level of knowledge required is not high, but a minimal amount of experience is needed.  This is not an introductory workshop, and if you have never used R before, you will be lost. To get started, try going through the slides and script here.

If you would like to bring your own laptop to work on, please install R and R-Studio on your own before the workshop to speed things up.  If you are having difficulty with this step, come to the workshop at least 20 minutes before it starts and we will help you out.


Slides (in PDF) and R script for the Feb. 6th workshop are posted below.  Sample solutions for the exercises have also been posted.

Slides (in PDF) and R script for the Feb. 13th workshop are posted below.  Sample solutions for the exercises will be posted here at the end of the Feb. 13th workshop.


This workshop was designed with help from Jonathan Whiteley, Kiyoko Gotanda, Etienne Low-Décarie, Zofia Taranu, Corey Chivers, and Morpheus.


Take one minute to fill out a feedback form and tell us what you thought about the workshop.
Programming in R part 2 solutions.R
Ben Haller,
Feb 13, 2012, 12:36 PM
Programming in R part 2.R
Ben Haller,
Feb 12, 2012, 6:38 PM
Ben Haller,
Feb 12, 2012, 6:38 PM
Ben Haller,
Feb 5, 2012, 3:51 AM
Ben Haller,
Feb 6, 2012, 8:23 AM
Ben Haller,
Feb 6, 2012, 12:57 PM