R tutorial

Introduction

Most of the pracs that you will be working on during this semetester will be conducted using R. R is a computer programming language which allows for amazing statistical analyses and graphical outputs. One of the best things about R is that it is “opensource” - that means that anyone can access the underlying code that the language is based on to understand how it works. This is great for development because those who know how to code can then learn the best ways to write code to take full advantage of the language’s capabilities. You can think of R as the Android of the statistics world. Like R, Android is also freely available to explore and develop further. Anyone can go and look at how the Android operating system works and anyone can develop an app if they know how. It’s much trickier to do that for Apple’s iOS, for example.
R is great but it requires a bit of a learning curve. You are most probably used to interacting with a computer through what is called a graphical user interface (A.K.A GUI) but R does not have a GUI. R users interact with R via the console or through scripts - both of which require you to write commands out as text. These are then compiled and run by R and an output is produced. This may seem a little tricky at first as the only thing you are presented with is a blank screen - no menu options and no buttons to click on that can help you remember where you were going but let me assure you that doing statistics by writing out what you want to do will make you a better biologist and statician because you really need to think about what you are going to do. The other down side to R is that everything you do matters - there is no room for error. If you make a spelling mistake your code will not work. If you forget a comma your code will not work. If you forget to capitalise something your code will not work.
Those are just about all the down sides to R. Now on to the upsides:

  1. There is a massive online community that is willing to help you solve your problems
  2. R is free - you can continue using it when you leave UKZN
  3. It is a rare skill and knowing how to use R will make you more employable
  4. It is well on its way to becoming the industry standard
  1. You can do more than just statistics. You can also
  2. You will always have your code (provided you save your work) so later you can come back to your analyses and run them again. This beats having to click through menu options in the hopes of matching what you might have done six months ago

There are a whole load of other reasons to use R - some of which you will hopefully come across yourselves. But for now we will go through some of the basics. This course is designed to teach you population and community ecology and not R. However, an important skill which can help you learn and do population and community ecology better is the R programming language. Unfortunately we do not have enough time together for you to learn R completely but we hope that this course’s practical component will spark an interest in you to try learning R to help you with your future studies.

Where to get help?

There are plenty of places to get help with how to use R. I will try give you a broad suite of resources (all of which are free) to cater for people who enjoy learning in different ways.

More formal manuals

Accessible and easy textbooks

YouTube Channels

All of these resources (and many more which you will easily find if you look online) are freely available - forever, no strings attached!
Apart from textbook and video resources there is also a very vibrant community of scientists and programmers on a website called Stack Overflow willing to help solve problems relating to R - provided your describe your problem adequately enough. More often than not your particular question would likely have been answered and so a simple search for your question should do the trick. Other more specific forums include mailing lists such as R-sig-ecology and R-nabble.
Besides these online and email based resources are the statistical analyses sections of scientific papers where researchers describe the ways they carried out there analyses. And then there is the help documentation from within R. We will discuss how to use this documentation during our tutorial from Hands-On Programming with R.
Something important to note regarding all these resources is that you might come across several different ways of doing the same thing. It might seem strange but please don’t let it worry you. This just means that you can find one way that works for you out of a range of possible options.

First introductory tutorial

Now that we have had a bit of an overview of what R is and where we can get help, we can get onto installing R and something else we will need called RStudio. Head over to the first section of the “RStudio tutorial” page within the “Useful tutorials” section and see how to install R and RStudio.
Once you’ve installed R and RStudio we can go out of this website and open up Part 1 in Hands-On Programming with R. Once we are done there we will go through Part 3.11.3. These two resources have been specifically selected to help you get to grips with the basic basics of R needed for BIOL 305 practicals. We will do these two parts together in today’s practical to get to grips with the language.
During the time that you would normally be working on your prac report submissions we ask that you go through two other sections in another book called R for Data Science. Specifically parts 1, 2, 3, and 5. You are not required to submit anything from reading and attempting these exercises but rather they are there to help prepare you for the pracs that follow. You aren’t expected to know each and every thing from those exercises, rather we make them available for you to become familiar with how R works and several resources which you can return to throughout the semester.

Final note

There will be some aspects of the code that you may not fully grasp and that is okay. These practicals are designed to expose you to both population and community ecology and the R programming language. It does not aim to make you a fully fledged expert on R although I hope that your experience with using R during this course will encourage you to want to learn it more. I understand that some of this code may be quite challenging. If at any time you are really struggling with a particular aspect of the code or are particularly interested in how something works please feel free to contact one of your demonstrators or myself and we will be happy to assist you as best we can and within reason (i.e. we will not give you answers to the problems but we will certainly give you suggestions on how to address the problem).

Packages

If you would like to download all of the packages we will be using during these practicals you can use the code below:

install.packages(c("tidyverse", "tidyr", "rmarkdown", "shiny", "deSolve", "viridis", "codyn", "gridExtra", "tibble", "ggthemes", "mvtnorm", "latex2exp", "png", "rmarkdown", "knitr", "kableExtra", "reshape2", "plyr", "tibble"))