Assignment 1: Basic R (8 marks)
Download the .Rmd file here.
*To submit this assignment, upload the full .Rmd document, including the original questions, your code, and the output. Submit your assignment as a knitted .pdf (Question 1 walks you through this part, and more details are in course notes).
1. Using R Markdown notebooks (1 mark)
- In RStudio, create a new R Markdown notebook. The file name should end in .Rmd.
- Read the guidelines provided in example text in that notebook
- Use the knit button to run the example notebook and generate a PDF file. You might be asked to install some R packages if it’s the first time you’ve done this - go ahead an let them install
- Next, download the assignment .Rmd file, and either a) open it in RStudio (ignoring the new file you created above) or b) copy the contents into example notebook you already have opened.
- Insert a code chunk below these bullets, write a simple mathematical expression to evaluate (e.g. 1+1), and then run the chunk (green arrow
playbutton in top right of chunk) - Now knit this notebook again, before proceeding with the rest of the assignment
2. Variable assignment and basic math (1 mark)
- Assign the value
5to the variable/objecta. Displaya. (0.25 marks)
- Assign the result of
10/3to the variableb. Displayb. (0.25 marks)
- Write a function that uses Pythagorean theorem to calculate the length of the longest side (hypotenuse) of a right angle triangle given the lengths of the two other sides. Use it to assign the hypotenuse of a triangle with sides
aandbto the variablehypot. Displayhypot. (0.5 marks)
3. Vectors (3 marks)
- Create a vector
vwith all integers 0-30, and a vectorwwith every third integer in the same range. (0.25 marks)
- What is the difference in lengths of the vectors
vandw? (0.25 marks)
- Create a new vector,
v_square, with the square of elements at indices 3, 6, 7, 10, 15, 22, 23, 24, and 30 from the variablev. Hint: Use indexing rather than a for loop. (0.25 marks)
- Calculate the mean and median of the first five values from
v_square. (0.25 marks)
- Create a boolean vector
v_bool, indicating which vectorvelements are bigger than 20. How many values are over 20? Hint: In R, TRUE = 1, and FALSE = 0, so you can use simple arithmetic to find this out. (0.5 marks)
- Create a new vector of character strings
v_bool_in_words, that contains the wordbigif the corresponding element ofvis bigger than 20 andsmallif not (0.5 marks)
- Write a function that calculates the median of the any values in a numeric vector greater than “n”. Test this function with the
vandv_squarevectors and n = 10. (1 marks)
4. Loops and conditional statements (3 marks)
We’re going to use some of the simple R commands we’ve used to create our first biological simulation! Imagine you are managing the cultivation of some type of organism (this could be anything from a population of cells growing in a petri dish, a crop of plants, or a colony of animals). We’ll call this organism the “crop” and assume the population is divided up into separate “plots”. The population in each plot grows over time, and when it reaches a specified size you will “harvest” it - removing most of the population - and then replant a small amount. We are going to track the population size of each plot over time.
- Assume you start with 10 plots with population sizes 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Create a vector of length 10 that contains the size of each plot. (0.25 marks)
- If the population size is greater than 5, harvest occurs. Using a single line of code and without defining any new variables, count how many plots will be harvested initially (0.25 marks)
- Write a for-loop to go through the initial plots, find the ones that will be harvested, and set their population sizes to zero after harvest. Store the new population sizes in the vector
v_post_harvest(0.5 mark)
- Do the same calculation as above, but use vector subsetting and boolean expressions to avoid having to do a for-loop (see lectures notes for reminders) (0.5 marks)
- Each year, the population size doubles due to growth. Update your for-loop to go through the initial plots and report their population sizes after 1 full year including harvesting and growth. Assume that after harvesting the population is “re-seeded” such that after a year it will have size 1. Make sure to make a not of any assumptions you are making. Store the new population sizes in the vector
v_post_growth_harvest(1 mark)
- Is there a way to avoid using a for-loop for this last calculation? (0.5 marks)