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.
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.
Source with echo
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
Create an assignment script, put it in your class folder and name it according to the convention above.
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
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.
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.
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:
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.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.
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):
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)
length
function)[]
)min
function)max
function)mean
function)[]
)sum
function)NULLS
in VectorsCut 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)
numbers_2
vectornumbers_2
vectornumbers_2
numbers_2
vectorYou 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:
sum
function).[]
).[]
).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).
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.