Assignment Formats: R Scripts

For your assignments, you will be writing R scripts with comments and code which solve problems and answer questions. You will submit your R scripts on D2L. I will download all the scripts and run them in RStudio on my computer to check your work. Therefore, you will need to follow strict assignment formatting rules.

File Names

Each assignment should follow this naming convention:

full_name_hw##.R

For example: justin_pomeranz_hw01.R

You can capitalize words in the file name if you choose, but it should follow the general convention above.

File format

  • Comment before each problem, and each sub-problem
  • Make sure your results print out on Source with echo
    • If you’re answer is saved in an object, make sure you print out that object afterwards
    • for example, see that volume is on the last line of the problem 2 example below
# Problem 1

# 1.1
2 - 10

# 1.2
3 * 5

# problem 2

width = 2
height = 3
length = 1.5
volume = width * height * length
volume

Exercises Homework 01

Create an assignment script, put it in your class folder and name it according to the convention above.

1. Basic expressions

Write the following calculations in your script.

2 - 10

3 * 5

9 / 2

5 - 3 * 2

(5 - 3) * 2

4^2

8 / 2^2

Run them by either clicking the Run button in the top-right corner of the editor or press Ctrl+Enter (Windows & Linux) or Command+Enter (Mac) to run code and print the results in the console.

If no code is highlighted/selected this will run the line the cursor is on. If you highlighted/selected a block of code it will run that entire group of lines.

You can also run the entire script by clicking the arrow next to Source and selecting Source with Echo or by using Ctrl+Shift+Enter (Windows & Linux) or Command+Shift+Enter (Mac).

To tell someone reading the code what this section of the code is about, add a comment line that says ‘Exercise 1’ before the code that answers the exercise. Comments in R are added by adding the # sign. Anything after a# sign on the same line is ignored when the program is run. So, the start of your program should look something like:

# Exercise 1

# 1.1
2 - 10

# 1.2
3*5

2. Basic Objects: Scalars

Here is a small program that converts a mass in kilograms to a mass in grams and then prints out the resulting value.

mass_kg <- 2.62
mass_g <- mass_kg * 1000
mass_g

Create similar code to convert a mass in pounds to a mass kilograms.

  • Create a variable to store a body mass in pounds. Assign this variable a value of 3.5 (an appropriate mass for a Sylvilagus audubonii.

  • Convert the variable from body mass in pounds to body mass in kilograms (by dividing it by 2.2046), and assign it to a new variable.

  • Print the value of the new variable to the screen.

3. More Objects

Calculate a total biomass in grams for 3 white-throated woodrats Neotoma albigula and then convert it to kilograms. The total biomass is three times the average size of a single individual. An average individual weighs 250 grams.

  • Add a new section to your R script starting with a comment.

  • Create a variable grams and assign it the mass of a single Neotoma albigula (250).

  • Create a variable number and assign it the number of individuals (3).

  • Create a variable biomass and assign it a value by multiplying the grams and number variables together.

  • Convert the value of biomass into kilograms (there are 1000 grams in a kilogram so divide by 1000) and assign this value to a new variable.

  • Print the final answer to the screen.

4. Built-in Functions

A built-in function is one that you don’t need to install and load a package to use. Some examples include:

  • abs() returns the absolute value of a number (e.g., abs(-2))
  • round() rounds a number (the first argument) to a given number of decimal places (the second argument digits =) (e.g., round(12.1123, digits = 2))
  • sqrt() takes the square root of a number (e.g., sqrt(4))
  • tolower() makes a string all lower case (e.g., tolower("HELLO"))
  • toupper() makes a string all upper case (e.g., toupper("hello"))

Use these built-in functions to print the following items:

  1. The absolute value of -15.5.
  2. 4.483847 rounded to one decimal place.
  3. 3.8 rounded to the nearest integer. You don’t have to specify the number of decimal places in this case if you don’t want to, because round() will default to using 0 if the digits argument is not provided. Look at help(round) or ?round to see how this is indicated.
  4. “species” in all capital letters.
  5. “SPECIES” in all lower case letters.
  6. Assign the value of the square root of 2.6 to a variable. Then round the variable you’ve created to 2 decimal places and assign it to another variable. Print out the rounded value.

Optional Challenge: Do the same thing as task 6 (immediately above), but instead of creating the intermediate variable, perform both the square root and the round on a single line by putting the sqrt() call inside the round() call.

5. Modify the code

The following code estimates the total net primary productivity (NPP) per day for two sites. It does this by multiplying the grams of carbon produced in a single square meter per day by the total area of the site. It then prints the daily NPP for each site.

site1_g_carbon_m2_day <- 5
site2_g_carbon_m2_day <- 2.3
site1_area_m2 <- 200
site2_area_m2 <- 450
site1_npp_day <- site1_g_carbon_m2_day * site1_area_m2
site2_npp_day <- site2_g_carbon_m2_day * site2_area_m2
site1_npp_day
site2_npp_day

Copy this code into your assignment and then add additional lines of code to do the following steps and print them out after the daily NPP values (the ones currently printed by the code):

  1. The sum of the total daily NPP for the two sites.
  2. The difference between the daily NPP for the two sites. We only want an absolute difference, so use abs() function to make sure the number is positive.
  3. The total NPP over a year for the two sites combined (the sum of the total daily NPP values from part (1) multiplied by 365).

6. Basic Vectors

Cut and paste the following vector into your assignment and then use code to print the requested values related to the vector.

numbers <- c(5, 2, 26, 8, 16)
  1. The number of items in the numbers vector (using the length function)
  2. The third item in the numbers vector (using [])
  3. The smallest number in the numbers vector (using the min function)
  4. The largest number in the numbers vector (using the max function)
  5. The average of the numbers in the numbers vector (using the mean function)
  6. The first, second and third numbers in the numbers vector (using [])
  7. The sum of the values in the numbers vector (using the sum function)

7. NULLS in Vectors

Cut and paste the following vector into your assignment. Then use code to print the requested values related to the vector. You’ll need to use na.rm = TRUE argument within each function to ignore the null values. for example: range(numbers_2, na.rm = TRUE)

numbers_2 <- c(7, 6, 22, 5, NA, 42)
  1. The smallest number in the numbers_2 vector
  2. The largest number in the numbers_2 vector
  3. The average of the numbers in the numbers_2
  4. The sum of the values in the numbers_2 vector

8. Shrub Volume Vectors

You have data on the length, width, and height of 10 individuals of the yew Taxus baccata stored in the following vectors:

length <- c(2.2, 2.1, 2.7, 3.0, 3.1, 2.5, 1.9, 1.1, 3.5, 2.9)
width <- c(1.3, 2.2, 1.5, 4.5, 3.1, NA, 1.8, 0.5, 2.0, 2.7)
height <- c(9.6, 7.6, 2.2, 1.5, 4.0, 3.0, 4.5, 2.3, 7.5, 3.2)

Copy these vectors into an R script and then determine the following:

  1. The volume of each shrub (length × width × height). Storing this in a variable will make some of the next problems easier.
  2. The sum of the volume of all of the shrubs (using the sum function).
  3. A vector of the height of shrubs with lengths > 2.5.
  4. A vector of the height of shrubs with heights > 5.
  5. A vector of the heights of the first 5 shrubs (using []).
  6. A vector of the volumes of the first 3 shrubs (using []).

Optional Challenge: A vector of the volumes of the last 5 shrubs with the code written so that it will return the last 5 values regardless of the length of the vector (i.e., it will give the last 5 values if there are 10, 20, or 50 individuals).

9. Variable Names (optional)

In the More Variables exercise (number 3 above) we used the variable names grams, number, and biomass to describe the individual mass, number of individuals, and total biomass of some white-throated woodrats. But are these variable names the best choice? If we came back to the code for this assignment in two weeks would we be able to remember what these variables were referring to and therefore what was going on in the code? The variable name biomass is also kind of long. If we had to type it many times it would be faster just to type b. We could also use really descriptive alternatives like individual_mass_in_grams. Or we would compromise and abbreviate this or leave out some of the words to make it shorter (e.g., indiv_mass_g).

Think about good variable names and then create a new version of this code with the variables renamed to be most useful. Make sure your code still runs properly with the name changes.