Introduction

We are using a RMarkdown file (extension .Rmd), which contains a combination of narrative (text) and code (contained in code chunks). This allows extensive documentation alongside your code. You will use this file type in all of your workshops, and for submitting your first assignment.

The files are provided for you, but if you want to create a new one, go to File > New File > R Markdown and select the type of document you want to create. You could also select R Script if you don’t want any text narrative. The code in an R script does not need to be inside a chunk. Create your script file by clicking File > New file > R Script, then File > Save As, and you should be prompted to save it. Name it something obvious like ‘BEM2031_script_week1’, and save it to a location where you know you can find and access it later on.

Packages in R and RStudio

Installation:
    To use external functions and datasets in R, you'll need to install the necessary packages.
    Use install.packages("package_name") to install a package. This usually needs to be done only once.
    Example: install.packages("tidyverse") (Uncomment this line if you haven't installed the tidyverse package, by removing the # symbol.)

Loading:
    Before using a package, you must load it into your current R session.
    Use library(package_name) to load a package. This needs to be done at the beginning of each R session where you intend to use the package.
    Example: library(tidyverse

Commenting Out Code:
    Use the # symbol to comment out any lines of code.
    Commented-out code is ignored by R when the script is run.
    This is helpful for:
        Temporarily disabling parts of your code.
        Adding explanatory notes within your script.

The tidyverse package just loads many different packages at once:

  • ggplot2 - for beautidful and informative visualisations
  • dplyr - for filtering, selecting, arranging, mutating, and summarising data
  • tibble - for effective data tables - a modern version of a dataframe
  • tidyr - helpful functions for cleaning and reshaping your data
  • readr - functions for reading and writing data
  • purrr - functional programming tools for operations on multiple values efficiently
  • stringr - for manipulating text and character strings
  • forcats - for dealing with categorical or ordinal data (factors)

The data mtcars dataset To illustrate how to use these tools, let’s explore the mtcars dataset, which is included in R by default. This dataset contains information about fuel consumption and other characteristics of 32 cars.

Examine the data

  • View the data:

    This will open a spreadsheet-like view of the data, allowing you to examine the variables and their values.

view(mtcars)

We may need to know more about the dataset. As this data is built into R, we can do that by going to the Help Tab in the bottom right quadrant, and searching for mtcars. Understanding your data is vital for understanding and interpreting it!

  • Summarise the data:

    This will provide summary statistics for each variable in the dataset, such as the mean, median, minimum, and maximum values.

summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000
  • Summarise the structure of the data:

    This will show you the data types of each variable (e.g. numeric, character)

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

By exploring the mtcars dataset and using the tools provided by tidyverse, you can start to uncover interesting patterns and insights. For example, you might investigate the relationship between engine horsepower and fuel efficiency, or compare the performance of different types of cars.

This is just a brief introduction to the tidyverse. As you continue to learn R, you’ll discover the many powerful ways in which these tools can be used to analyze and visualise data.

Including Plots

Plots are a great way of exploring data by showing patterns that are not easily visualised in examining data. Plots can be embedded in a .Rmd file. Using echo=FALSE means that the code will not display when you knit the file. I have set it to echo=TRUE, which is the default.

Here we will look at the relationship between mpg and displacement (a measure of the engine’s volume). A larger displacement indicates a more powerful engine.

ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point()

What pattern do you see?