Unit 6. Programming: Labs

These labs test and build on the material presented in the SWIRL lessons.

Scroll down or click here to to check how to submit them.

Lab 1

Tree plot in Michigan

Trees in a 1-ha plot on East Lansing, MI, USA. Colours indicate different species; size of symbol indicates DBH.

# Read in the data
dat_tree <- read.table("http://www.intro2r.info/data/treespecies_cleandata.txt", 
                       header = TRUE, sep = '\t')

  1. Write a function to calculate basal area (remember that BA = radius^2 * pi).

  2. Calculate the basal area of each tree.

  3. Calculate the total BA of all trees.

  4. Using tapply(), calculate the mean BA of each species

  5. Using one call to tapply() what is the total BA of (i) alive trees, and (ii) dead trees?

  6. Write a function to generate a map (an x-y plot) of the trees. Set some nice defaults. Establish a 2 x 3 grid of panels in a figure, and use this function to make a map of each species.

Lab 2: More Trees!

1. Write a loop to read in each of the following six files, one for each species.

You can either read them in from the URLs, or download them to your hard drive and read them in from there.

There are several ways to do this.

Think about what data object/s you want to create.

You might want to use paste().


2. Write a loop to make the same 2 x 3 panel figure of maps as for Lab 3 Q6.

3. Create six random tree communities of 100 trees from the full trees dataset

How to submit your labs

How to write up your lab answers

You will need to write R code to answer each of the questions.

Please format your answers as follows:

It should look something like this:

# LAB: Unit 1. Lab 1
# Your Name #  Put your name here

# 1. Add 7 and 3,456.
7 + 3456

# 2. Assign this value to an object
x <- 7 + 3456

How to submit your lab answers on Canvas

You are permitted to submit your answers as many times as you like within each Unit.

Answers will be graded two or three times a week and re-opened if you submit early.

Each lab will close at its respective deadline (see Canvas).

Final grades for each lab will be computed and entered into the Canvas gradebook at the end of each Unit.