# 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.

## 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
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.

There are several ways to do this.

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

You might want to use `paste()`.

``````http://www.intro2r.info/data/blackoak.txt
http://www.intro2r.info/data/hickory.txt
http://www.intro2r.info/data/maple.txt
http://www.intro2r.info/data/misc.txt
http://www.intro2r.info/data/redoak.txt
http://www.intro2r.info/data/whiteoak.txt
``````

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

• Use your custom plotting function from Lab 3.

• Make sure to use a loop!

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

• Use `sample()`.

• Create a list, and have each element of this list be a community.

• Plot the six communities. Ensure that point size is proportional to DBH.

• Calculate the mean DBH of each community.

# How to submit your labs

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

• Copy and paste each question, commented out. This ensures that we know which answer corresponds to which question.

It should look something like this:

``````# LAB: Unit 1. Lab 1

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

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

• Go to the Assignments page.

• Under ‘Labs’, you should find the correct assignment.

• Copy and paste your R code into the text box.

• Click ‘Submit Assignment’.

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.