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
# example code, do not include in your homework script
# 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.

1.1) 2 - 10

1.2) 3 * 5

1.3) 9 / 2

1.4) 5 - 3 * 2

1.5) (5 - 3) * 2

1.6) 4^2

1.7) 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.

# example code, do not include in your homework script
mass_kg <- 2.62
mass_g <- mass_kg * 1000
mass_g

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

Notes * 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. (Final Answer)

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))

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

4.1. The absolute value of -15.5. 4.2. 4.483847 rounded to one decimal place. 4.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.4. 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.

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)

6.1. The number of items in the numbers vector (using the length function) 6.2. The third item in the numbers vector (using []) 6.3. The smallest number in the numbers vector (using the min function) 6.4. The largest number in the numbers vector (using the max function) 6.5. The average of the numbers in the numbers vector (using the mean function) 6.6. The first, second and third numbers in the numbers vector (using []) 6.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)

7.1. The smallest number in the numbers_2 vector 7.2. The largest number in the numbers_2 vector 7.3. The average of the numbers in the numbers_2 7.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:

8.1. The volume of each shrub (length × width × height). Storing this in a variable will make some of the next problems easier. 8.2. The sum of the volume of all of the shrubs (using the sum function). 8.3. A vector of the height of shrubs with lengths > 2.5. 8.4. A vector of the height of shrubs with heights > 5. 8.5. A vector of the heights of the first 5 shrubs (using []). 8.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. Data frames

  • Copy and run the following code in your assignment, then answer the questions below.
data("PlantGrowth")

Write code in your script to answer all of the following questions for the PLantGrowth data frame
9.1 What are the dimensions?
9.2 What are the number of columns?
9.3 What are the number of rows?
9.4 What are the column names?
9.5 Print out the structure of the data frame.
* Add a comment telling me what the class of each column is.
9.6 Extract the entire 3rd row.
9.7 Extract the entire 1st column.
9.8 Extract the column named group.