Programming in R

In this unit, you will learn how to create functions and learn some programming tricks.

Follow the links to each lecture, lab, and reading.

Scroll down to download the SWIRL lessons.

Lesson 1. How to write less code

Lecture: Writing your own functions

Learning Goals:

SWIRL: Writing functions

Lab: Unit 6: Lab 1

Reading: Best practice for writing R code

Functions: function()

Lesson 2. How to automate your work

Lecture: Control Structures slides

Learning Goals:

SWIRL: Loops

Lab: Unit 6: Lab 2

Reading: Crawley, M. The R Book Ch 2. Essentials of the R Language. 2.10 Loops and repeats

Functions: if(), else(), for() {}, while() {}, repeat {}, break, next

Lesson 3: How to sample and generate random values

Lecture: Sampling.

Learning Goals:

SWIRL: Sampling and simulation

Lab: Unit 6: Lab 3

Reading: Crawley, M. The R Book Ch 2. Essentials of the R Language. 2.9 Random numbers, shuffling, and sampling

Functions: sample(), rbinom(), rnorm(),