Introduction to R for Researchers

James Bartlett, University of Glasgow

@ York St John

Preparation steps

  • R and RStudio downloaded to your laptop? ✔️
  • Workshop and Quant Fundamentals books open? ✔️
  • Workshop files downloaded? ✔️

Intended learning outcomes

By the end of the workshop, you will be able to:

  1. Write reproducible reports using R Markdown / Quarto.

  2. Clean and wrangle data into appropriate forms for visualisation and analysis.

  3. Use the ggplot2 package for creating a range of plots.

  4. Share/receive code to conduct a brief code review.

Rough schedule

Time Task
11:00 Introduction and introduction to R
12:00 File management and reproducible documents
13:00-13:30 Lunch
13:30-14:30 Data visualisation
14:30-15:30 Data wrangling
15:30-16:30 Independent task and code review

Why R?

Perfect for researchers

  • Well-integrated tool popular in psychology researchers.

  • Ecosystem of well-maintained packages (e.g., tidyverse).

  • Develop a complete reproducible pipeline from raw data to your analysis.

Perfect for educators

R workflow

Image: Wickham et al. (2023)

Key messages

  • No one is going to be an expert R user after today; you will learn by doing.

  • There is nothing wrong with copying and adapting code; you need to know enough to know how to find solutions.

  • Your code does not need to be pretty, just annotated and reliable.

Introduction to R/RStudio

Intended learning outcomes:

By the end of this part, you will be able to:

  • Navigate and interact with RStudio.

  • Use an R function and find help documentation.

  • Install and load R packages.

  • Assign content to an object.

Writing reproducible documents

Intended learning outcomes:

By the end of this part, you will be able to:

  • Understand and set your working directory, either manually or through creating an R project.

  • Create and render a Quarto file to create a reproducible document.

  • Use inline code to combine text and code output in your reproducible documents.

  • Identify and fix common errors in rendering Quarto files.

Data visualisation

Target dataset: Lopez et al. (2023) on visual cues and food intake.

Intended learning outcomes:

By the end of this part, you will be able to:

  • Understand the ggplot2 layering system of creating plots.

  • Create and edit histograms to visualise the frequency of observations collated into bins.

  • Create and edit barplots to visualise the frequency of different categories.

  • Create and edit a scatterplot to visualise the relationship between two continuous variables.

  • Create and edit a violin-boxplot to visualise the density of data points in a continuous outcome.

  • Save plots as an image to use in reports or presentations.

Data wrangling

Target dataset: Alter et al. (2024) on assessing the perceived value of statistics software.

Intended learning outcomes:

By the end of this part, you will be able to:

  • Join two data sets by matching one or more identifying columns in common.

  • Select a range and reorder variables in your data set.

  • Modify or create variables, such as recoding values or creating new groups.

  • Filter observations to retain a subset of your data.

  • Summarise your data to calculate summary statistics.

  • Restructure data into different formats, such as long and wide form.

  • String together multiple functions using pipes.

Code review

Inspired by Ivimey-Cook et al. (2023), we will explore the four Rs:

  1. Is the code as reported?

  2. Does the code run?

  3. Is the code reliable?

  4. Are the results reproducible?

Independent task and code review

Target dataset: Farias et al. (2019) on motivations to go on a pilgrimage.

Intended learning outcomes:

By the end of this part, you will be able to:

  • Apply your wrangling and visualisation skills to a new dataset.

  • Understand the basic principles of code review for quality control.

  • Conduct a code review of your partner’s code.

Feedback

To finish, I would really appreciate feedback on the workshop: