Exercise 6: Basic programming in R

 

Read Chapter 6 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?

 

  1. Write a function to convert Fahrenheit to Centigrade (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.”

 

  1. 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.

 

  1. 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.

 

  1. You are a population ecologist for the day and wish to investigate the properties of the Ricker model. The Ricker model is defined as:

 

\[ N_{t+1} = N_{t} exp\left[r\left(1- \frac{N_{t}}{K} \right) \right] \]

 

  1. (cont) Where Nt 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.


  1. One of the things computers can do much better than humans is to do repetitive things very fast. For example, LODES website has data on commuting patterns (simulated) from each Census tract to another for each state in the US. For most states in the US, it has annual data from 2002 to 2019. Unfortunately to downlaod this data through a browser means that we will have to click individual links in each of these state subdirectories and save them.

This can be made very easy with loops in R.

As an example, look at a the data for Connecticut. It is located at https://lehd-ces-census-gov/data/lodes/LODES7/ct/od/

and has files named ct_od_{main|aux}_JT0{0-5}_yyyy.csv.gz

where {main|aux} represent main or aux; 0-5 is a sequence of numbers from 0 to 5; yyyy is from 2002 to 2019.

  1. Using download.file() function to download individual files from the web and nested for loops, download all the files for Connecticut.

  2. replace one or more of the nested for loop with walk() from purrr package.

  3. Pick any 5 states in the US and using common two letter abbreviations, download all the files. Try different combinations of states and see if your assumptions about the file names are generalisable (Hint: Use MA)

 

End of Exercise 6