Learning Objectives
Following this assignment students should be able to:
- execute simple math in the R console
- assign and manipulate variables
- use built-in functions for math and stats
- understand the assignment and execute flow of an R script
- understand the vector and data frame object structures
- assign, subset, and manipulate data in a vector
- execute vector algebra
- import data frames and interact with columns as vectors
Reading
Topics
- R & RStudio
- Expressions & Variables
- Types
- Errors
- Vectors & Data Frames
- Importing Data
- Readable Code
Readings
Introduction to RStudio (choose one)
- Reading: Introduction to R and RStudio (Data Carpentry Lesson)
- Video (15 min): An opinionated tour of RStudio (R-Ladies Sydney)
Introduction to R
Starting with Data
Lecture Notes
- Course Introduction
- Introduction
- Vectors
- Checking that your code runs
- Using Large Language Models to Learn
Exercises
Basic Expressions (10 pts)
Write the following calculations in the text editor.
- 2 - 10
- 3 * 5
- 9 / 2
- 5 - 3 * 2
- (5 - 3) * 2
- 4 ^ 2
- 8 / 2 ^ 2
Run them by either clicking the
Runbutton 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
Sourceand selectingSource with Echoor 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:Expected outputs for Basic Expressions# Exercise 1 #1.1 2 - 10 #1.2 3 * 5Basic Variables (10 pts)
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_gCreate 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.
More Variables (10 pts)
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
gramsand assign it the mass of a single Neotoma albigula (250). - Create a variable
numberand assign it the number of individuals (3). - Create a variable
biomassand assign it a value by multiplying thegramsandnumbervariables together. - Convert the value of
biomassinto 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.
Built-in Functions (10 pts)
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) (e.g.,round(12.1123, 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:
- The absolute value of -15.5.
- 4.483847 rounded to one decimal place.
- 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 using0if the second argument is not provided. Look athelp(round)or?roundto see how this is indicated. "species"in all capital letters."SPECIES"in all lower case letters.- 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
Expected outputs for Built-in Functionssqrt()call inside theround()call.Modify the Code (10 pts)
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_dayCopy 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):
- The sum of the total daily NPP for the two sites.
- 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.
- 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).
Basic Vectors (10 pts)
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)- The number of items in the
numbersvector (using thelengthfunction) - The third item in the
numbersvector (using[]) - The smallest number in the
numbersvector (using theminfunction) - The largest number in the
numbersvector (using themaxfunction) - The average of the numbers in the
numbersvector (using themeanfunction) - The first, second and third numbers in the
numbersvector (using[]) - The sum of the values in the
numbersvector (using thesumfunction)
- The number of items in the
Nulls in Vectors (15 pts)
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 = TRUEto ignore the null values.numbers <- c(7, 6, 22, 5, NA, 42)- The smallest number in the
numbersvector - The largest number in the
numbersvector - The average of the numbers in the
numbers - The sum of the values in the
numbersvector
- The smallest number in the
Shrub Volume Vectors (15 pts)
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:
- The volume of each shrub (length × width × height). Storing this in a variable will make some of the next problems easier.
- The sum of the volume of all of the shrubs (using the
sumfunction). - A vector of the height of shrubs with lengths > 2.5.
- A vector of the height of shrubs with heights > 5.
- A vector of the heights of the first 5 shrubs (using
[]). - 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).
Expected outputs for Shrub Volume VectorsCheck That Your Code Runs (10 pts)
Sometimes you think you’re code runs, but it only actually works because of something else you did previously. To make sure it actually runs you should save your work and then run it in a clean environment.
Follow these steps in RStudio to make sure your code really runs:
1. Restart R (see above) by clicking
Sessionin the menu bar and selectingRestart R:
2. If the
Environmenttab isn’t empty click on the broom icon to clear it:
The
Environmenttab should now say “Environment Is Empty”:
3. Rerun your entire homework assignment using “Source with Echo” to make sure it runs from start to finish and produces the expected results.

4. Make sure that you saved your code with the name
assignmentsomewhere in the file name. You should see the file in theFilestab and the name of the file should be black (not red with an*in the tab at the top of the text editor):

5. Make sure that your code will run on other computers
- No
setwd()(use RStudio Projects instead) - Use
/not\for paths
- No
Biomass vectors (Challenge - optional)
You have data on the diameter of all of the trees in three sites and each site includes a mix of managmeent stratagies. Tree masses can be estimated using diameter based on the equation
mass = 0.124 * diameter ^ 2.53. The biomass is the sum of the masses of all the trees at a site. Copy the vectors below into your R script.site <- c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3) diameter <- c(10.5, 14.2, 13.7, 15.3, 12.8, 9.8, 11.4, 10.9, 12.1, 13.5, 14.7, 8.5, 9.2, 10.1, 11.3, 12.4, 13.6, 14.8) management <- c("active", "active", "passive", "passive", "active", "passive", "active", "active", "passive", "passive", "active", "active", "passive", "active", "passive", "active", "passive", "active")Using only the tools we covered in class and the information above, answer the following questions:
- What is the mass of the largest tree, which site is it in, and what is its management status?
- Which site has the greatest total biomass and what is that biomass?
- What is the average mass of a tree in each site?
- What is the difference in total biomass for each site between the “active” management and “passive” management strategies?