FES 720 Introduction to R

LAB: Functions

This is the lab associated with the lecture and lesson on Functions in R.

Please upload your final completed lab on the Assignments page in Canvas, as per the instructions below.

You are welcome and expected to ask for help from the instructors if you get stuck: Please also come to the R Bootcamp on Friday—there is coffee and snacks!

Note: Half the marks in this lab are for good Style. Make sure that you document and format your functions well!


A. Harry Potter Movies (again!)

Read in the data from the website again (but this time with no ‘$’ sign in the budgets).

# Read in the Harry Potter Movies Table 1
dat_hp <- read.table("http://www.simonqueenborough.info/R/data/harry-potter-movies-numeric.txt", 
                     sep = '\t', header = TRUE)

Table 1. Box office history for all Harry Potter movies.

Release Date Movie Production Budget Domestic Opening Weekend Domestic Box Office Worldwide Box Office
Nov 16, 2001 Harry Potter and the Sorcerer’s Stone $125,000,000 $90,294,621 $317,575,550 $974,755,371
Nov 15, 2002 Harry Potter and the Chamber of Secrets $100,000,000 $88,357,488 $261,987,880 $878,979,634
Jun 4, 2004 Harry Potter and the Prisoner of Azkaban $130,000,000 $93,687,367 $249,538,952 $796,688,549
Nov 18, 2005 Harry Potter and the Goblet of Fire $150,000,000 $102,685,961 $290,013,036 $896,911,078
Jul 11, 2007 Harry Potter and the Order of the Phoenix $150,000,000 $77,108,414 $292,004,738 $942,943,935
Jul 15, 2009 Harry Potter and the Half-Blood Prince $250,000,000 $77,835,727 $301,959,197 $935,083,686
Nov 19, 2010 Harry Potter and the Deathly Hallows: Part I $125,000,000 $125,017,372 $295,983,305 $960,283,305
Jul 15, 2011 Harry Potter and the Deathly Hallows: Part II $125,000,000 $169,189,427 $381,011,219 $1,341,511,219
Nov 18, 2016 Fantastic Beasts and Where to Find Them $180,000,000 $74,403,387 $234,037,575 $803,798,342
  1. Write a (admittedly, very simple) function to add the total Domestic and Worldwide box offices, called totalBoxOffice(). Make sure that you document it well and that it is an abstraction of this dataset.

  2. Calculate the total box office for all nine films using this function.

  3. Write another (simple) function to calculate the Return on Investment, based on the total domestic and worldwide box office. Use your function totalBoxOffice() within this new function. Again, make sure that you document it well and that it is an abstraction of this dataset.

  4. Calculate the RoI for all nine films using this new function.

  5. Write a simple function to calculate the ratio of worldwide to domestic box office.

  6. Calculate this ratio for all nine films using your function.

B. Tree plot in Michigan

Here, we introduce a new dataset, of trees in a plot in East Lansing, Michigan, USA.

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.simonqueenborough.info/R/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. How many different species of tree are in the data?

  4. How many dead and alive trees are there (the $status column; 1 = alive, 0 = dead)?

  5. What is the mean BA of trees that are alive?


How to write up your answers

Please check the help page for a reminder, if you need to.