-
Notifications
You must be signed in to change notification settings - Fork 6
/
exercise_6.Rmd
51 lines (29 loc) · 2.38 KB
/
exercise_6.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
---
title: "Exercise 6"
output:
html_document:
toc: false
---
```{r setup, echo=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE, eval = FALSE, cache = FALSE)
```
\
## Exercise 6: Basic programming in R
\
Read [Chapter 7](https://intro2r.com/prog-r.html) to help you complete the questions in this exercise.
\
1\. Create a function to calculate the area of a circle. Test the function by finding the area of a circle with a diameter of 3.4 cm. Can you use it on a vector of data?
\
2\. Write a function to convert farenheit to centegrade (oC = (oF - 32) x 5/9). Get your function to print out your result in the following format: "Farenheit : *value of oF* is equivalent to *value oC* centigrade."
\
3\. Create a vector of normally distributed data, of length 100, mean 35 and standard deviation of 15. Write a function to calculate the mean, median, and range of the vector, print these values out with appropriate labels. Also get the function to plot a histogram (as a proportion) of the values and add a density curve.
\
4\. Write a function to calculate the median value of a vector of numbers (yes I know there's a ```median()``` function already but this is fun!). Be careful with vectors of an even sample size, as you will have to take the average of the two central numbers (hint: use modulo %%2 to determine whether the vector is an odd or an even size). Test your function on vectors with both odd and even sample sizes.
\
5\. You are a population ecologist for the day and wish to investigate the properties of the [Ricker model](https://en.wikipedia.org/wiki/Ricker_model). The Ricker model is defined as:
\
$$ N_{t+1} = N_{t} exp\left[r\left(1- \frac{N_{t}}{K} \right) \right] $$
\
5\. (cont) Where *N~t~* is the population size at time *t*, *r* is the population growth rate and *K* is the carrying capacity. Write a function to simulate this model so you can conveniently determine the effect of changing *r* and the initial population size N0. *K* is often set to 100 by default, but you want the option of being able to change this with your function. So, you will need a function with the following arguments; nzero which sets the initial population size, *r* which will determine the population growth rate, time which sets how long the simulation will run for and *K* which we will initially set to 100 by default.
\
End of Exercise 6