This is an old revision of the document!


QCBS R Workshops

This series of 8 workshops walks participants through the steps required to use R for a wide array of statistical analyses relevant to research in biology and ecology. These open-access workshops were created by members of the QCBS both for members of the QCBS and the larger community.

Workshop 8: Programming in R

Developed by: Johanna Bradie, Sylvain Christin, Ben Haller, Guillaume Larocque

Link to associated Prezi: POST LINK HERE

Download the R script and data for this lesson: – NEED TO PUT R SCRIPT AND DATA HERE

Summary: This workshop focuses on basic programming in R. In this workshop, you will learn how to use for loops, write your own functions and run simulations in R. In addition, you will learn to use data.table to work quickly with large datasets and learn tips to program efficiently. The last part of the workshop will discuss code optimization, as well as parallel and multi-threaded computing.

  1. Flow control
  2. Writing functions in R
  3. Speeding up your code
  4. Useful packages for biologists

1. Execute statements conditionally using:

  • if
  • if/else

2. Execute statements multiple times using:

  • for loops
  • while loops
  • repeat loops

3. Modify loop execution using:

  • break statements
  • next statements

if and if/else statements

if and if/else statements are good for:

  • checking for problems or violations of assumptions
  • treating different rows of your data frame differently
  • testing for the existence of a file or variable

Syntax

if(condition) {
expression}     # The expression can be any command that you would like R to perform.
 
if(condition) {
expression 
} else {
expression }

Curly brackets are used so that R knows that your command is not complete with the first line of code.

For example, try:

if (2+2)==4 print("Arithmetic works.")
else print("Houston, we have a problem.")

The else statement doesn't work because R evaluates the first line without knowing your command is incomplete.

Instead, use:

if (2+2)==4 {
print("Arithmetic works.")
} else {
print("Houston, we have a problem.")
}

When using brackets, R waits to evaluate the command until the brackets have been closed.

Loops

Loops are good for:

  • doing something for every element of an object
  • doing something until the processed data runs out
  • doing something for every file in a folder
  • doing something that can fail, until it succeeds
  • iterating a calculation until it converges

Syntax

for(variable in sequence) {
expression }
 
while(condition) {
expression }
 
repeat(expression) 

for loops

The most common loop is the 'for loop'. Use a 'for loop' to execute a block of code a known number of times.

Syntax

for (i in 1:5) {
expression
}

The example above would cause R to evaluate the expression 5 times. In the first iteration, R would replace every instance of i with 1. In the second iteration i would be replaced with 2, and so on.

Try:

for (m in 1:10) {
print(m*2) 
}