Applied Population Dynamics
Lab 1 – Excel and R Basics

Referencing

Referencing – Column B

Referencing – Row 3

Referencing – Cell B3

Create Sequence Using Auto-fill

To use auto-fill: begin a sequence, highlight the cells, and then drag the box at the bottom-right of the last cell.

Relative Cell References

Relative Cell References

Relative Cell References

Values of reference will change when using auto-fill

Absolute Cell References

Absolute Cell References

Dollar sign “locks” a reference so that auto-fill won’t change it

Partial Cell References

Equations

Equations

Equations

Equations also have to begin with the equals sign (=)

Equations

Equations

Formulas

Graphics

Graphics

Graphics

Graphics

Add a line for males

Customize

Add legend

Customize

Add axis labels

Customize

Change line color

R

R – Software for statistical computing


R can be downloaded here: https://www.r-project.org/


You can use the graphical user interface that comes with R, or you can run R through a system like ESS+emacs or RStudio (https://posit.co/download/rstudio-desktop/)


Most people use RStudio these days

RStudio

RStudio

To create a new script, click: File > New File > R Script

Save your script using: File > Save As

RStudio

Reproducing the Excel exercise

Create an object called year to hold the sequence of years.1

year <- 1950:1961 # A vector of integers
year              # Type the name of an object to see its values
 [1] 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961

Note that you can use seq to create sequences in a more general way. The c function combines two or more vectors.

updown <- c(seq(from=20, to=100, by=20), seq(from=100, to=20, by=-20))
updown            # Two sequences, combined
 [1]  20  40  60  80 100 100  80  60  40  20

Use the length function to determine the number of values in a vector.

nYears <- length(year)
nYears
[1] 12

A simple population model

Create an empty vector to store the data on females. Set female abundance to 100 in the first year.

females <- rep(NA, nYears)
females[1] <- 100


Use a “for loop” to compute female abundance in subsequent years.

for(t in 2:nYears) {
    females[t] <- females[t-1] + females[t-1]*0.01
}


We will use “for loops” for almost every population model that we implement in R

A simple population model

Generate the data on males using a single line of code.

males <- females*0.8

Put the objects in a data.frame

model1 <- data.frame(year, females, males)
model1
   year  females    males
1  1950 100.0000 80.00000
2  1951 101.0000 80.80000
3  1952 102.0100 81.60800
4  1953 103.0301 82.42408
5  1954 104.0604 83.24832
6  1955 105.1010 84.08080
7  1956 106.1520 84.92161
8  1957 107.2135 85.77083
9  1958 108.2857 86.62854
10 1959 109.3685 87.49482
11 1960 110.4622 88.36977
12 1961 111.5668 89.25347

Graphics

plot(females ~ year, data=model1, type="o", xlab="Year", ylab="Abundance",
     lwd=2, pch=16, ylim=c(0, 120))
lines(males ~ year, data=model1, type="o", col="blue", lwd=2, pch=16)
legend(x=1950, y=40, legend=c("Females", "Males"), col=c("black", "blue"),
       lty=1, pch=16)

Graphics

Another way to do the same thing, without putting objects in a data.frame:

plot(x=year, y=females, type="o", xlab="Year", ylab="Abundance",
     lwd=2, pch=16, ylim=c(0, 120))
lines(x=year, y=males, type="o", col="blue", lwd=2, pch=16)
legend(x=1950, y=40, legend=c("Females", "Males"), col=c("black", "blue"), lty=1, pch=16)

Assignment

  1. Create an Excel file and name it “Yourlastname_Yourfirstname”.

  2. Create the sheet shown on the next page using the techniques covered in this lab.

    • Use auto-fill to create the first two columns.

    • For the “Adults” column, use the equation shown for cells C3 through C22. Note: For cell C2, you can directly enter the value “10”.

  3. Copy “Sheet1” to a new sheet and change the color and thickness of the lines. You can pick any colors and thicknesses you want.

  4. Replicate steps 1–3 using a “for loop” in a self-contained R script. Note that you will need code to create both graphs—the original one and the one with the new line color and thickness.

    • Hint: You can make the juveniles data using seq and c, like we did with the updown example earlier.
  5. Upload the Excel workbook (with both sheets) and the R script to ELC by 5:00pm on Friday.1

Excel sheet to be created