1  Projects and R Markdown

Intended Learning Outcomes

By the end of this chapter, you should be able to:

  • Re-familiarise yourself with setting up projects
  • Re-familiarise yourself with RMarkdown documents
  • Recap and apply data wrangling procedures to analyse data

Individual Walkthrough

1.1 R and R Studio

Remember, R is a programming language that you will write code in and RStudio is an Integrated Development Environment (IDE) which makes working with R easier as it’s more user friendly. You need both components for this course.

If this is not ringing any bells yet, have a quick browse through the materials from year 1 to refresh your memory.

1.1.1 R server

Use the server only if you are unable to install R and RStudio on your computer (e.g., if you are using a Chromebook) or if you encounter issues while installing R on your own machine. Otherwise, you should install R and RStudio directly on your own computer. R and RStudio are already installed on the R server.

You will find the link to the server on Moodle.

1.1.2 Installing R and RStudio on your computer

The RSetGo book provides detailed instructions on how to install R and RStudio on your computer. It also includes links to walkthroughs for installing R on different types of computers and operating systems.

If you had R and RStudio installed on your computer last year, we recommend updating to the latest versions. In fact, it’s a good practice to update them at the start of each academic year. Detailed guidance can be found in Appendix B.

Once you have installed or updated R and RStudio, return to this chapter.

1.1.3 Settings for Reproducibility

By now, you should be aware that the Psychology department at the University of Glasgow places a strong emphasis on reproducibility, open science, and raising awareness about questionable research practices (QRPs) and how to avoid them. Therefore, it’s important that you work in a reproducible manner so that others (and your future self) can understand and check your work. This also makes it easier for you to reuse your work in the future.

Always start with a clear workspace. If your Global Environment contains anything from a previous session, you can’t be certain whether your current code is working as intended or if it’s using objects created earlier.

To ensure a clean and reproducible workflow, there are a few settings you should adjust immediately after installing or updating RStudio. In Tools > Global Options… General tab

  • Uncheck the box labelled Restore .RData into workspace at startup to make sure no data from a previous session is loaded into the environment
  • set Save workspace to .RData on exit to Never to prevent your workspace from being saved when you exit RStudio.

Reproducibility settings in Global Options

R has included rainbow parentheses to help with keeping count on the brackets.

To enable the feature, go to Tools > Global Options… Code tab > Display tab and tick the last checkbox “Use rainbow parentheses”

Enable Rainbow parenthesis

1.1.4 RStudio panes

RStudio has four main panes each in a quadrant of your screen:

  • Source pane
  • Environment pane
  • Console pane
  • Output pane
Your Turn

Are you ready for a quick quiz to see what you remember about the RStudio panes from last year? Click on Quiz to see the questions.

What is their purpose?

The Source pane…
The Environment pane…
The Console pane…
The Output pane…

Where are these panes located by default?

  • The Source pane is located?
  • The Environment pane is located?
  • The Console pane is located?
  • The Output pane is located?

If you were not quite sure about one/any of the panes, check out the materials from Level 1. If you want to know more about them, there is the RStudio guide on posit

1.2 Activity 1: Creating a new project

It’s important to create a new RStudio project whenever you start a new project. This practice makes it easier to work in multiple contexts, such as when analysing different datasets simultaneously. Each RStudio project has its own folder location, workspace, and working directories, which keeps all your data and RMarkdown documents organised in one place.

Last year, you learnt how to create projects on the server, so you already know the steps. If cannot quite recall how that was done, go back to the Level 1 materials.

On your own computer, open RStudio, and complete the following steps in this order:

  • Click on File > New Project…
  • Then, click on “New Directory”
  • Then, click on “New Project”
  • Name the directory something meaningful (e.g., “2A_chapter1”), and save it in a location that makes sense, for example, a dedicated folder you have for your level 2 Psychology labs - you can either select a folder you have already in place or create a new one (e.g., I named my new folder “Level 2 labs”)
  • Click “Create Project”. RStudio will restart itself and open with this new project directory as the working directory. If you accidentally close it, you can open it by double-clicking on the project icon in your folder
  • You can also check in your folder structure that everything was created as intended

Creating a new project

In case anyone is wondering why my colour scheme in the gif above looks different to yours, I’ve set mine to “Pastel On Dark” in Tools > Global Options… > Appearances. And my computer lives in “dark mode”.

Don’t nest projects

Don’t ever save a new project inside another project directory. This can cause some hard-to-resolve problems.

1.3 Activity 2: Create a new R Markdown file

  • Open a new R Markdown document: click File > New File > R Markdown or click on the little page icon with a green plus sign (top left).
  • Give it a meaningful Title (e.g., Level 2 chapter 1) - you can also change the title later. Feel free to add your name or GUID in the Author field author name. Keep the Default Output Format as HTML.
  • Once the .Rmd opened, you need to save the file.
  • To save it, click File > Save As… or click on the little disc icon. Name it something meaningful (e.g., “chapter_01.Rmd”, “01_intro.Rmd”). Make sure there are no spaces in the name - R is not very fond of spaces… This file will automatically be saved in your project folder (i.e., your working directory) so you should now see this file appear in your file viewer pane.

Creating a new .Rmd file

Remember, an R Markdown document or .Rmd has “white space” (i.e., the markdown for formatted text) and “grey parts” (i.e., code chunks) in the default colour scheme (see Figure 1.1). R Markdown is a powerful tool for creating dynamic documents because it allows you to integrate code and regular text seamlessly. You can then knit your .Rmd using the knitr package to create a final document as either a webpage (HTML), a PDF, or a Word document (.docx). We’ll only knit to HTML documents in this course.

R markdown anatomy (image from https://intro2r.com/r-markdown-anatomy.html)

1.3.1 Markdown

The markdown space in an .Rmd is ideal for writing notes that explain your code and document your thought process. Use this space to clarify what your code is doing, why certain decisions were made, and any insights or conclusions you have drawn along the way. These notes are invaluable when revisiting your work later, helping you (or others) understand the rationale behind key decisions, such as setting inclusion/exclusion criteria or interpreting the results of assumption tests. Effectively documenting your work in the markdown space enhances both the clarity and reproducibility of your analysis.

The markdown space offers a variety of formatting options to help you organise and present your notes effectively. Here are a few of them that can enhance your documentation:

Heading levels

There is a variety of heading levels to make use of, using the # symbol.

You would incorporate this into your text as:

# Heading level 1

## Heading level 2

### Heading level 3

#### Heading level 4

##### Heading level 5

###### Heading level 6

And it will be displayed in your knitted html file as:

You need a space between the # and the first letter. If the space is missing, the heading will be displayed in the HTML file as …

#Heading 1

Unordered and ordered lists

You can also include unordered lists and ordered lists. Click on the tabs below to see how they are incorporated

You can add bullet points using either *, - or + and they will turn into:

  • bullet point (created with *)
  • bullet point (created with -)
  • bullet point (created with +)

or use bullet points of different levels using 1 tab key press or 2 spaces (for sub-item 1) or 2 tabs/4 spaces (for sub-sub-item 1):

  • bullet point item 1
    • sub-item 1
      • sub-sub-item 1
      • sub-sub-item 2
  • bullet point item 2

You need an empty row before your bullet points start. If I delete the empty row before the bullet points, they will be displayed in the HTML as …

Text without the empty row: * bullet point created with * - bullet point created with - + bullet point created with +

Start the line with 1., 2., etc. When you want to include sub-items, either use the tab key twice or add 4 spaces. Same goes for the sub-sub-item: include either 2 tabs (or 4 manual spaces) from the last item or 4 tabs/ 8 spaces from the start of the line.

  1. list item 1
  2. list item 2
    1. sub-item 1 (with 4 spaces) A. sub-sub-item 1 (with an additional 4 spaces from the last indent)

If you don’t leave enough spaces, the list won’t be recognised, and your output looks like this:

  1. list item 3
  1. sub-item 1 (with only 2 spaces) A. sub-sub-item 1 (with an additional 2 spaces from the last indent)

The great thing though is that you don’t need to know your alphabet or number sequences. R markdown will fix that for you

If I type into my .Rmd

…it will be rendered in the knitted HTML output as…

  1. list item 3
  2. list item 1
    1. sub-item labelled “a)”
    2. sub-item labelled “i)”
      1. sub-item labelled “C)”
      2. sub-item labelled “Z)”
  3. list item 7

Yes, they do but you need to label your sub-item lists accordingly. The first label you list in each level is set as the baseline. If they are labelled 1) instead of i) or A., the output will show as follows, but the automatic-item-fixing still works:

  1. list item 7
    1. list item “1)” with 4 spaces
      1. list item “1)” with 8 spaces
      2. this is an item labelled “6)” (magically corrected to “2.”)

Emphasis

Include emphasis to draw attention to keywords in your text:

R markdown syntax Displayed in the knitted HTML file
**bold text** bold text
*italic text* italic text
***bold and italic*** bold and italic

Other examples can be found in the R Markdown Cheat Sheet

1.3.2 Code chunks

Everything you write inside the code chunks will be interpreted as code and executed by R. Code chunks start with ``` followed by an {r} which specifies the coding language R, some space for code, and ends with ```. If you accidentally delete one of those backticks, your code won’t run and/or your text parts will be interpreted as part of the code chunks or vice versa. This should be evident from the colour change - more white than expected typically indicates missing starting backticks, whilst too much grey/not enough white suggests missing ending backticks. But no need to fret if that happens - just add the missing backticks manually.

You can insert a new code chunk in several ways:

  • Click the Insert a new code chunk button in the RStudio Toolbar (green icon at the top right corner of the Source pane).
  • Select Code > Insert Chunk from the menu.
  • Using the shortcut Ctrl + Alt + I for Windows or Cmd + Option + I on MacOSX.
  • Type ```{r} and ``` manually

Figure 1.1: Default .Rmd with highlighting - names in pink and knitr display options in purple

Within the curly brackets of a code chunk, you can specify a name for the code chunk (see pink highlighting in Figure 1.1). The chunk name is not necessarily required; however, it is good practice to give each chunk a unique name to support more advanced knitting approaches. It also makes it easier to reference and manage chunks.

Within the curly brackets, you can also place rules and arguments (see purple highlighting in Figure 1.1) to control how your code is executed and what is displayed in your final HTML output. The most common knitr display options include:

Code Does code run Does code show Do results show
eval=FALSE NO YES NO
echo=TRUE (default) YES YES YES
echo=FALSE YES NO YES
results=‘hide’ YES YES NO
include=FALSE YES NO NO
Important

The table above will be incredibly important for the data skills homework II. When solving error mode items you will need to pay attention to the first one eval = FALSE.

One last thing: In your newly created .Rmd file, delete everything below line 12 (keep the set-up code chunk) and save your .Rmd by clicking on the disc symbol.

Delete everything below line 12
Your Turn

That was quite a long section about what Markdown can do. I promise, we’ll practice that more later. For the minute, we want you to create a new level 2 heading on line 12 and give it a meaningful heading title (something like “Loading packages and reading in data” or “Chapter 1”).

On line 12, you should have typed ## Loading packages and reading in data (or whatever meaningful title you chose). This will create level 2 heading once we knit the .Rmd.

1.4 Activity 3: Download the data

The data for chapters 1-3. Download it here: data_ch1.zip. There are 2 csv files contained in a zip folder. One is the data file we are going to use today prp_data_reduced.csv and the other is an Excel file prp_codebook that explains the variables in the data.

The first step is to unzip the zip folder so that the files are placed within the same folder as your project.

  • Place the zip folder within your 2A_chapter1 folder
  • Right mouse click –> Extract All...
  • Check the folder location is the one to extract the files to
  • Check the extracted files are placed next to the project icon
  • Files and project should be visible in the Output pane in RStudio
Screenshots for “unzipping a zip folder”

Unzipping a zip folder

The paper by Pownall et al. was a registered report published in 2023, and the original data can be found on OSF (https://osf.io/5qshg/).

Citation

Pownall, M., Pennington, C. R., Norris, E., Juanchich, M., Smailes, D., Russell, S., Gooch, D., Evans, T. R., Persson, S., Mak, M. H. C., Tzavella, L., Monk, R., Gough, T., Benwell, C. S. Y., Elsherif, M., Farran, E., Gallagher-Mitchell, T., Kendrick, L. T., Bahnmueller, J., . . . Clark, K. (2023). Evaluating the Pedagogical Effectiveness of Study Preregistration in the Undergraduate Dissertation. Advances in Methods and Practices in Psychological Science, 6(4). https://doi.org/10.1177/25152459231202724

Abstract

Research shows that questionable research practices (QRPs) are present in undergraduate final-year dissertation projects. One entry-level Open Science practice proposed to mitigate QRPs is “study preregistration,” through which researchers outline their research questions, design, method, and analysis plans before data collection and/or analysis. In this study, we aimed to empirically test the effectiveness of preregistration as a pedagogic tool in undergraduate dissertations using a quasi-experimental design. A total of 89 UK psychology students were recruited, including students who preregistered their empirical quantitative dissertation (n = 52; experimental group) and students who did not (n = 37; control group). Attitudes toward statistics, acceptance of QRPs, and perceived understanding of Open Science were measured both before and after dissertation completion. Exploratory measures included capability, opportunity, and motivation to engage with preregistration, measured at Time 1 only. This study was conducted as a Registered Report; Stage 1 protocol: https://osf.io/9hjbw (date of in-principle acceptance: September 21, 2021). Study preregistration did not significantly affect attitudes toward statistics or acceptance of QRPs. However, students who preregistered reported greater perceived understanding of Open Science concepts from Time 1 to Time 2 compared with students who did not preregister. Exploratory analyses indicated that students who preregistered reported significantly greater capability, opportunity, and motivation to preregister. Qualitative responses revealed that preregistration was perceived to improve clarity and organization of the dissertation, prevent QRPs, and promote rigor. Disadvantages and barriers included time, perceived rigidity, and need for training. These results contribute to discussions surrounding embedding Open Science principles into research training.

Changes made to the dataset

We made some changes to the dataset for the purpose of increasing difficulty for data wrangling (Chapter 2 and Chapter 3) and data visualisation (Chapter 4 and Chapter 5). This will ensure some “teachable moments”. The changes are as follows:

  • We removed some of the variables to make the data more manageable for teaching purposes.
  • We recoded some values from numeric responses to labels (e.g., understanding).
  • We added the word “years” to one of the Age entries.
  • We tidied a messy column Ethnicity but introduced a similar but easier-to-solve “messiness pattern” when recoding the understanding data.
  • The scores in the original file were already corrected from reverse-coded responses. We reversed that process to present raw data here.

1.5 Activity 4: Installing packages, loading packages, and reading in data

1.5.1 Installing packages

When you install R and RStudio for the first time (or after an update), most of the packages we will be using won’t be pre-installed. Before you can load new packages like tidyverse, you will need to install them.

If you try to load a package that has not been installed yet, you will receive an error message that looks something like this: Error in library(tidyverse) : there is no package called 'tidyverse'.

To fix this, simply install the package first. In the console, type the command install.packages("tidyverse"). This only needs to be done once after a fresh installation. After that, you will be able to load the tidyverse package into your library whenever you open RStudio.

Install packages from the console only

Never include install.packages() in the Rmd. Only install packages from the console pane or the packages tab of the lower right pane!!!

Note, there will be other packages used in later chapters that will also need to be installed before their first use, so this error is not limited to tidyverse.

1.5.2 Loading packages and reading in data

The first step is to load in the packages we need and read in the data. Today, we’ll only be using tidyverse, and read_csv() will help us store the data from prp_data_reduced.csv in an object called data_prp.

Copy the code into a code chunk in your .Rmd file and run it. You can either click the green error to run the entire code chunk, or use the shortcut Ctrl + Enter (Windows) or Cmd + Enter (Mac) to run a line of code/ pipe from the Rmd.

library(tidyverse)
data_prp <- read_csv("prp_data_reduced.csv")
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Rows: 89 Columns: 91
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (17): Code, Age, Ethnicity, Opptional_mod_1_TEXT, Research_exp_1_TEXT, U...
dbl (74): Gender, Secondyeargrade, Opptional_mod, Research_exp, Plan_prereg,...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

1.6 Activity 5: Familiarise yourself with the data

  • Look at the Codebook to get a feel of the variables in the dataset and how they have been measured. Note that some of the columns were deleted in the dataset you have been given.
  • You’ll notice that some questionnaire data was collected at 2 different time points (i.e., SATS28, QRPs, Understanding_OS)
  • some of the data was only collected at one time point (i.e., supervisor judgements, OS_behav items, and Included_prereg variables are t2-only variables)

1.6.1 First glimpse at the data

Before you start wrangling your data, it is important to understand what kind of data you’re working with and what the format of your dataframe looks like.

As you may have noticed, read_csv() provides a message listing the data types in your dataset and how many columns are of each type. Plus, it shows a few examples columns for each data type.

To obtain more detailed information about your data, you have several options. Click on the individual tabs to see the different options available. Test them out in your own .Rmd file and use whichever method you prefer (but do it).

Warning

Some of the output is a bit long because we do have quite a few variables in the data file.

In the Global Environment, click the blue arrow icon next to the object name data_prp. This action will expand the object, revealing details about its columns. The $ symbol is commonly used in Base R to access a specific column within your dataframe.

Visual inspection of the data

Con: When you have quite a few variables, not all of them are shown.

Use glimpse() if you want a more detailed overview you can see on your screen. The output will display rows and column numbers, and some examples of the first couple of observations for each variable.

glimpse(data_prp)
Rows: 89
Columns: 91
$ Code                                  <chr> "Tr10", "Bi07", "SK03", "SM95", …
$ Gender                                <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2,…
$ Age                                   <chr> "22", "20", "22", "26", "22", "2…
$ Ethnicity                             <chr> "White European", "White British…
$ Secondyeargrade                       <dbl> 2, 3, 1, 2, 2, 2, 2, 2, 1, 1, 1,…
$ Opptional_mod                         <dbl> 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2,…
$ Opptional_mod_1_TEXT                  <chr> "Research methods in first year"…
$ Research_exp                          <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,…
$ Research_exp_1_TEXT                   <chr> NA, NA, NA, NA, NA, NA, NA, NA, …
$ Plan_prereg                           <dbl> 1, 3, 1, 2, 1, 1, 3, 3, 2, 2, 2,…
$ SATS28_1_Affect_Time1                 <dbl> 4, 5, 5, 6, 2, 1, 6, 3, 2, 5, 2,…
$ SATS28_2_Affect_Time1                 <dbl> 5, 6, 3, 3, 6, 1, 2, 2, 7, 3, 4,…
$ SATS28_3_Affect_Time1                 <dbl> 3, 2, 5, 2, 6, 7, 2, 6, 6, 5, 2,…
$ SATS28_4_Affect_Time1                 <dbl> 4, 5, 2, 2, 6, 6, 5, 5, 5, 5, 2,…
$ SATS28_5_Affect_Time1                 <dbl> 5, 5, 5, 6, 1, 1, 5, 1, 2, 5, 2,…
$ SATS28_6_Affect_Time1                 <dbl> 5, 6, 2, 5, 6, 7, 4, 5, 5, 3, 5,…
$ SATS28_7_CognitiveCompetence_Time1    <dbl> 4, 2, 2, 5, 6, 7, 2, 5, 5, 2, 2,…
$ SATS28_8_CognitiveCompetence_Time1    <dbl> 2, 2, 2, 1, 6, 7, 2, 5, 3, 2, 3,…
$ SATS28_9_CognitiveCompetence_Time1    <dbl> 2, 2, 2, 3, 3, 7, 2, 6, 3, 3, 1,…
$ SATS28_10_CognitiveCompetence_Time1   <dbl> 6, 7, 6, 6, 4, 2, 6, 4, 5, 6, 5,…
$ SATS28_11_CognitiveCompetence_Time1   <dbl> 4, 3, 5, 5, 3, 1, 6, 2, 5, 6, 5,…
$ SATS28_12_CognitiveCompetence_Time1   <dbl> 3, 5, 3, 5, 5, 7, 3, 4, 7, 2, 3,…
$ SATS28_13_Value_Time1                 <dbl> 1, 1, 2, 1, 3, 7, 1, 2, 1, 2, 4,…
$ SATS28_14_Value_Time1                 <dbl> 7, 7, 6, 6, 5, 1, 6, 5, 7, 6, 2,…
$ SATS28_15_Value_Time1                 <dbl> 7, 7, 6, 6, 3, 5, 6, 6, 6, 5, 5,…
$ SATS28_16_Value_Time1                 <dbl> 2, 1, 3, 2, 6, 5, 3, 7, 2, 2, 2,…
$ SATS28_17_Value_Time1                 <dbl> 1, 1, 3, 3, 7, 7, 2, 7, 2, 2, 5,…
$ SATS28_18_Value_Time1                 <dbl> 3, 6, 5, 3, 1, 1, 5, 1, 5, 2, 2,…
$ SATS28_19_Value_Time1                 <dbl> 3, 3, 3, 3, 7, 7, 4, 5, 3, 5, 6,…
$ SATS28_20_Value_Time1                 <dbl> 2, 1, 4, 2, 7, 7, 2, 4, 2, 2, 7,…
$ SATS28_21_Value_Time1                 <dbl> 2, 1, 3, 2, 6, 7, 2, 5, 1, 3, 5,…
$ SATS28_22_Difficulty_Time1            <dbl> 3, 2, 5, 3, 2, 1, 4, 2, 2, 5, 3,…
$ SATS28_23_Difficulty_Time1            <dbl> 5, 6, 5, 6, 6, 7, 4, 6, 7, 5, 6,…
$ SATS28_24_Difficulty_Time1            <dbl> 2, 2, 2, 3, 1, 4, 4, 2, 2, 2, 2,…
$ SATS28_25_Difficulty_Time1            <dbl> 6, 7, 5, 5, 6, 7, 5, 6, 5, 5, 5,…
$ SATS28_26_Difficulty_Time1            <dbl> 4, 2, 2, 2, 6, 7, 4, 5, 3, 5, 3,…
$ SATS28_27_Difficulty_Time1            <dbl> 4, 5, 5, 3, 6, 7, 4, 3, 5, 3, 6,…
$ SATS28_28_Difficulty_Time1            <dbl> 1, 7, 5, 5, 6, 6, 5, 4, 4, 4, 2,…
$ QRPs_1_Time1                          <dbl> 7, 7, 7, 7, 7, 7, 6, 2, 7, 6, 7,…
$ QRPs_2_Time1                          <dbl> 7, 7, 7, 7, 7, 7, 6, 7, 7, 7, 5,…
$ QRPs_3_Time1                          <dbl> 5, 2, 6, 2, 6, 4, 6, 3, 7, 3, 3,…
$ QRPs_4_Time1                          <dbl> 7, 7, 6, 6, 7, 4, 6, 7, 7, 7, 6,…
$ QRPs_5_Time1                          <dbl> 3, 3, 7, 7, 2, 7, 4, 6, 7, 3, 2,…
$ QRPs_6_Time1                          <dbl> 4, 7, 6, 5, 7, 4, 4, 5, 7, 6, 5,…
$ QRPs_7_Time1                          <dbl> 5, 7, 7, 7, 7, 4, 5, 6, 7, 7, 5,…
$ QRPs_8_Time1                          <dbl> 7, 7, 7, 7, 7, 7, 7, 7, 7, 2, 7,…
$ QRPs_9_Time1                          <dbl> 6, 7, 7, 4, 7, 7, 3, 7, 6, 6, 2,…
$ QRPs_10_Time1                         <dbl> 7, 6, 5, 2, 5, 4, 2, 6, 7, 7, 2,…
$ QRPs_11_Time1                         <dbl> 7, 7, 7, 4, 7, 7, 4, 6, 7, 7, 5,…
$ QRPs_12NotQRP_Time1                   <dbl> 2, 2, 1, 4, 1, 4, 2, 4, 2, 2, 1,…
$ QRPs_13NotQRP_Time1                   <dbl> 1, 1, 1, 1, 1, 4, 2, 4, 1, 1, 1,…
$ QRPs_14NotQRP_Time1                   <dbl> 1, 4, 3, 4, 1, 4, 2, 3, 3, 4, 3,…
$ QRPs_15NotQRP_Time1                   <dbl> 2, 4, 2, 2, 1, 4, 2, 1, 4, 4, 2,…
$ Understanding_OS_1_Time1              <chr> "2", "2", "6", "2", "6", "Not at…
$ Understanding_OS_2_Time1              <chr> "2", "Not at all confident", "2"…
$ Understanding_OS_3_Time1              <chr> "2", "Not at all confident", "3"…
$ Understanding_OS_4_Time1              <chr> "6", "Not at all confident", "6"…
$ Understanding_OS_5_Time1              <chr> "Entirely confident", "6", "6", …
$ Understanding_OS_6_Time1              <chr> "Entirely confident", "Entirely …
$ Understanding_OS_7_Time1              <chr> "6", "Not at all confident", "2"…
$ Understanding_OS_8_Time1              <chr> "6", "3", "5", "3", "5", "Not at…
$ Understanding_OS_9_Time1              <chr> "Entirely confident", "6", "5", …
$ Understanding_OS_10_Time1             <chr> "Entirely confident", "6", "5", …
$ Understanding_OS_11_Time1             <chr> "Entirely confident", "2", "4", …
$ Understanding_OS_12_Time1             <chr> "Entirely confident", "2", "5", …
$ Pre_reg_group                         <dbl> 1, 1, 1, 2, 1, 1, 1, 2, 2, 1, 2,…
$ Other_OS_behav_2                      <dbl> 1, NA, NA, NA, 1, NA, NA, 1, NA,…
$ Other_OS_behav_4                      <dbl> 1, NA, NA, NA, NA, NA, NA, NA, N…
$ Other_OS_behav_5                      <dbl> NA, NA, NA, NA, 1, 1, NA, NA, NA…
$ Closely_follow                        <dbl> 2, 2, 2, NA, 3, 3, 3, NA, NA, 2,…
$ SATS28_Affect_Time2_mean              <dbl> 3.500000, 3.166667, 4.833333, 4.…
$ SATS28_CognitiveCompetence_Time2_mean <dbl> 4.166667, 4.666667, 6.166667, 5.…
$ SATS28_Value_Time2_mean               <dbl> 3.000000, 6.222222, 6.000000, 4.…
$ SATS28_Difficulty_Time2_mean          <dbl> 2.857143, 2.857143, 4.000000, 2.…
$ QRPs_Acceptance_Time2_mean            <dbl> 5.636364, 5.454545, 6.272727, 5.…
$ Time2_Understanding_OS                <dbl> 5.583333, 3.333333, 5.416667, 4.…
$ Supervisor_1                          <dbl> 5, 7, 7, 1, 7, 1, 7, 6, 7, 5, 6,…
$ Supervisor_2                          <dbl> 5, 6, 7, 4, 6, 2, 7, 5, 6, 5, 5,…
$ Supervisor_3                          <dbl> 6, 7, 7, 1, 7, 1, 7, 5, 6, 6, 7,…
$ Supervisor_4                          <dbl> 6, 7, 7, 1, 7, 1, 7, 6, 7, 6, 6,…
$ Supervisor_5                          <dbl> 5, 7, 7, 4, 7, 3, 7, 7, 6, 6, 6,…
$ Supervisor_6                          <dbl> 5, 7, 7, 4, 6, 3, 7, 6, 7, 6, 6,…
$ Supervisor_7                          <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
$ Supervisor_8                          <dbl> 5, 5, 7, 1, 7, 1, 7, 5, 7, 5, 6,…
$ Supervisor_9                          <dbl> 6, 7, 7, 4, 7, 3, 7, 5, 7, 6, 7,…
$ Supervisor_10                         <dbl> 5, 7, 7, 1, 7, 1, 7, 6, 7, 6, 6,…
$ Supervisor_11                         <dbl> NA, 7, 7, NA, 7, 1, 7, 5, 7, 6, …
$ Supervisor_12                         <dbl> 4, 5, 7, 1, 4, 1, 7, 3, 6, 6, 5,…
$ Supervisor_13                         <dbl> 4, 2, 5, 1, 2, 1, 6, 3, 5, 6, 5,…
$ Supervisor_14                         <dbl> 5, 7, 7, 1, 7, 1, 7, 5, 7, 6, 6,…
$ Supervisor_15_R                       <dbl> 1, 1, 1, 4, 1, 7, 1, 2, 1, 2, 1,…

You can also use spec() as suggested in the message above and then it shows you a list of the data type in every single column. But it doesn’t show you the number of rows and columns.

spec(data_prp)
cols(
  Code = col_character(),
  Gender = col_double(),
  Age = col_character(),
  Ethnicity = col_character(),
  Secondyeargrade = col_double(),
  Opptional_mod = col_double(),
  Opptional_mod_1_TEXT = col_character(),
  Research_exp = col_double(),
  Research_exp_1_TEXT = col_character(),
  Plan_prereg = col_double(),
  SATS28_1_Affect_Time1 = col_double(),
  SATS28_2_Affect_Time1 = col_double(),
  SATS28_3_Affect_Time1 = col_double(),
  SATS28_4_Affect_Time1 = col_double(),
  SATS28_5_Affect_Time1 = col_double(),
  SATS28_6_Affect_Time1 = col_double(),
  SATS28_7_CognitiveCompetence_Time1 = col_double(),
  SATS28_8_CognitiveCompetence_Time1 = col_double(),
  SATS28_9_CognitiveCompetence_Time1 = col_double(),
  SATS28_10_CognitiveCompetence_Time1 = col_double(),
  SATS28_11_CognitiveCompetence_Time1 = col_double(),
  SATS28_12_CognitiveCompetence_Time1 = col_double(),
  SATS28_13_Value_Time1 = col_double(),
  SATS28_14_Value_Time1 = col_double(),
  SATS28_15_Value_Time1 = col_double(),
  SATS28_16_Value_Time1 = col_double(),
  SATS28_17_Value_Time1 = col_double(),
  SATS28_18_Value_Time1 = col_double(),
  SATS28_19_Value_Time1 = col_double(),
  SATS28_20_Value_Time1 = col_double(),
  SATS28_21_Value_Time1 = col_double(),
  SATS28_22_Difficulty_Time1 = col_double(),
  SATS28_23_Difficulty_Time1 = col_double(),
  SATS28_24_Difficulty_Time1 = col_double(),
  SATS28_25_Difficulty_Time1 = col_double(),
  SATS28_26_Difficulty_Time1 = col_double(),
  SATS28_27_Difficulty_Time1 = col_double(),
  SATS28_28_Difficulty_Time1 = col_double(),
  QRPs_1_Time1 = col_double(),
  QRPs_2_Time1 = col_double(),
  QRPs_3_Time1 = col_double(),
  QRPs_4_Time1 = col_double(),
  QRPs_5_Time1 = col_double(),
  QRPs_6_Time1 = col_double(),
  QRPs_7_Time1 = col_double(),
  QRPs_8_Time1 = col_double(),
  QRPs_9_Time1 = col_double(),
  QRPs_10_Time1 = col_double(),
  QRPs_11_Time1 = col_double(),
  QRPs_12NotQRP_Time1 = col_double(),
  QRPs_13NotQRP_Time1 = col_double(),
  QRPs_14NotQRP_Time1 = col_double(),
  QRPs_15NotQRP_Time1 = col_double(),
  Understanding_OS_1_Time1 = col_character(),
  Understanding_OS_2_Time1 = col_character(),
  Understanding_OS_3_Time1 = col_character(),
  Understanding_OS_4_Time1 = col_character(),
  Understanding_OS_5_Time1 = col_character(),
  Understanding_OS_6_Time1 = col_character(),
  Understanding_OS_7_Time1 = col_character(),
  Understanding_OS_8_Time1 = col_character(),
  Understanding_OS_9_Time1 = col_character(),
  Understanding_OS_10_Time1 = col_character(),
  Understanding_OS_11_Time1 = col_character(),
  Understanding_OS_12_Time1 = col_character(),
  Pre_reg_group = col_double(),
  Other_OS_behav_2 = col_double(),
  Other_OS_behav_4 = col_double(),
  Other_OS_behav_5 = col_double(),
  Closely_follow = col_double(),
  SATS28_Affect_Time2_mean = col_double(),
  SATS28_CognitiveCompetence_Time2_mean = col_double(),
  SATS28_Value_Time2_mean = col_double(),
  SATS28_Difficulty_Time2_mean = col_double(),
  QRPs_Acceptance_Time2_mean = col_double(),
  Time2_Understanding_OS = col_double(),
  Supervisor_1 = col_double(),
  Supervisor_2 = col_double(),
  Supervisor_3 = col_double(),
  Supervisor_4 = col_double(),
  Supervisor_5 = col_double(),
  Supervisor_6 = col_double(),
  Supervisor_7 = col_double(),
  Supervisor_8 = col_double(),
  Supervisor_9 = col_double(),
  Supervisor_10 = col_double(),
  Supervisor_11 = col_double(),
  Supervisor_12 = col_double(),
  Supervisor_13 = col_double(),
  Supervisor_14 = col_double(),
  Supervisor_15_R = col_double()
)

In the Global Environment, click on the object name data_prp. This action will open the data in a new tab. Hovering over the column headings with your mouse will also reveal their data type. However, it seems to be a fairly tedious process when you have loads of columns.

One common source of confusion is not seeing all your columns when you open up a data object as a tab. This is because RStudio shows you a maximum of 50 columns at a time. If you have more than 50 columns, navigate with the arrows to see the remaining columns.

Showing 50 columns at a time
Your Turn

Now that you have tested out all the options in your own .Rmd file, you can probably answer the following questions:

  • How many observations?
  • How many variables?
  • How many columns are col_character or chr data type?
  • How many columns are col_double or dbl data type?

The visual inspections shows you the number of observations and variables. glimpse() also gives you that information but calls them rows and columns respectively.

The data type information actually comes from the output when using the read_csv() function. Did you notice the information on Column specification (see screenshot below)?

message from read_csv() when reading in the data

Whilst spec() is quite useful for data type information per individual column, it doesn’t give you the total count of each data type. So it doesn’t really help with answering the questions here - unless you want to count manually from its extremely long output.

In your .Rmd, include a new heading level 2 called “Information about the data” (or something equally meaningful) and jot down some notes about data_prp. You could include the citation and/or the abstract, and whatever information you think you should note about this dataset (e.g., any observations from looking at the codebook?). You could also include some notes on the functions used so far and what they do. Try to incorporate some bold, italic or bold and italic emphasis and perhaps a bullet point or two.

## Information about the data

The data is from Pownall et al. (2023), and I can find the paper here: https://doi.org/10.1177/25152459231202724.

I’ve noticed in the prp codebook that the SATS-28 questionnaire has quite a few *reverse-coded items*, and the supervisor support questionnaire also has a reverse-coded item.

So far, I think I prefer **glimpse()** to show me some more detail about the data. Specs() is too text-heavy for me which makes it hard to read.

Things to keep in mind:

  • **don’t forget to load in tidyverse first!!!**
  • always read in the data with **read_csv**, ***never ever use read.csv***!!!

The output rendered in a knitted html file

1.6.2 Data types

Each variable has a data type, such as numeric (numbers), character (text), and logical (TRUE/FALSE values), or a special class of factor. As you have just seen, our data_prp only has character and numeric columns (so far).

Numeric data can be double (dbl) or integer (int). Doubles can have decimal places (e.g., 1.1). Integers are the whole numbers (e.g., 1, 2, -1) and are displayed with the suffix L (e.g., 1L). This is not overly important but might leave you less puzzled the next time you see an L after a number.

Characters (also called “strings”) is anything written between quotation marks. This is usually text, but in special circumstances, a number can be a character if it placed within quotation marks. This can happen when you are recoding variables. It might not be too obvious at the time, but you won’t be able to calculate anything if the number is a character

typeof(1)
typeof(1L)
typeof("1")
typeof("text")
[1] "double"
[1] "integer"
[1] "character"
[1] "character"

No problems here…

1+1
[1] 2

When the data type is incorrect, you won’t be able to compute anything, despite your numbers being shown as numeric values in the dataframe. The error message tells you exactly what’s wrong with it, i.e., that you have non-numeric arguments.

"1"+"1" # ERROR
Error in "1" + "1": non-numeric argument to binary operator

Logical data (also sometimes called “Boolean” values) are one of two values: TRUE or FALSE (written in uppercase). They become really important when we use filter() or mutate() with conditional statements such as case_when(). More about those in Chapter 3.

Some commonly used logical operators:

operator description
> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal to
!= not equal to
%in% TRUE if any element is in the following vector

A factor is a specific type of integer or character that lets you assign the order of the categories. This becomes useful when you want to display certain categories in “the correct order” either in a dataframe (see arrange) or when plotting (see Chapter 4/ Chapter 5).

1.6.3 Variable types

You’ve already encountered them in Level 1 but let’s refresh. Variables can be classified as continuous (numbers) or categorical (labels).

Categorical variables are properties you can count. They can be nominal, where the categories don’t have an order (e.g., gender) or ordinal (e.g., Likert scales either with numeric values 1-7 or with character labels such as “agree”, “neither agree nor disagree”, “disagree”). Categorical data may also be factors rather than characters.

Continuous variables are properties you can measure and calculate sums/ means/ etc. They may be rounded to the nearest whole number, but it should make sense to have a value between them. Continuous variables always have a numeric data type (i.e. integer or double).

Why is this important you may ask?

Knowing your variable and data types will help later on when deciding on an appropriate plot (see Chapter 4 and Chapter 5) or which inferential test to run (Chapter 6 to ?sec-factorial).

Your Turn

As we’ve seen earlier, data_prp only had character and numeric variables which hardly tests your understanding to see if you can identify a variety of data types and variable types. So, for this little quiz, we’ve spiced it up a bit. We’ve selected a few columns, shortened some of the column names, and modified some of the data types. Here you can see the first few rows of the new object data_quiz. You can find the code with explanations at the end of this section.

Code Age Gender Ethnicity Secondyeargrade QRP_item QRPs_mean Understanding_item QRP_item > 4
Tr10 22 2 White European 60-69% (2:1 grade) 5 5.636364 2 TRUE
Bi07 20 2 White British 50-59% (2:2 grade) 2 5.454546 2 FALSE
SK03 22 2 White British ≥ 70% (1st class grade) 6 6.272727 6 TRUE
SM95 26 2 White British 60-69% (2:1 grade) 2 5.000000 2 FALSE
St01 22 2 White British 60-69% (2:1 grade) 6 5.545454 6 TRUE
glimpse(data_quiz)
Rows: 89
Columns: 9
$ Code               <chr> "Tr10", "Bi07", "SK03", "SM95", "St01", "St10", "Wa…
$ Age                <chr> "22", "20", "22", "26", "22", "20", "21", "21", "22…
$ Gender             <fct> 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, …
$ Ethnicity          <chr> "White European", "White British", "White British",…
$ Secondyeargrade    <fct> 60-69% (2:1 grade), 50-59% (2:2 grade), ≥ 70% (1st …
$ QRP_item           <dbl> 5, 2, 6, 2, 6, 4, 6, 3, 7, 3, 3, 4, 4, 4, 4, 6, 3, …
$ QRPs_mean          <dbl> 5.636364, 5.454545, 6.272727, 5.000000, 5.545455, 6…
$ Understanding_item <chr> "2", "2", "6", "2", "6", "Not at all confident", "4…
$ `QRP_item > 4`     <lgl> TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE,…

Select from the dropdown menu the variable type and their data types for each of the columns.

Column Variable type Data type
Age
Gender
Ethinicity
Secondyeargrade
QRP_item
QRPs_mean
Understanding_item
QRP_item > 4

The code might look a bit complex for the minute despite the line-by-line explanations below. Come back to it after completing chapter 2.

data_quiz <- data_prp %>% 
  select(Code, Age, Gender, Ethnicity, Secondyeargrade, QRP_item = QRPs_3_Time1, QRPs_mean = QRPs_Acceptance_Time2_mean, Understanding_item = Understanding_OS_1_Time1) %>% 
  mutate(Gender = factor(Gender),
         Secondyeargrade = factor(Secondyeargrade,
                                  levels = c(1, 2, 3, 4, 5),
                                  labels = c("≥ 70% (1st class grade)", "60-69% (2:1 grade)", "50-59% (2:2 grade)", "40-49% (3rd class)", "< 40%")),
         `QRP_item > 4` = case_when(
           QRP_item > 4 ~ TRUE, 
           .default = FALSE))

Lets go through this line by line:

  • line 1: creates a new object called data_quiz and it is based on the already existing data object data_prp
  • line 2: we are selecting a few variables of interest, such as Code, Age etc. Some of those variables were renamed in the process according to the structure new_name = old_name, for example QRP item 3 at time point 1 got renamed as QRP_item.
  • line 3: The function mutate() is used to create a new column called Gender that turns the existing column Gender from a numeric value into a factor. R simply overwrites the existing column of the same name. If we had named the new column Gender_factor, we would have been able to retain the original Gender column and Gender_factor would have been added as the last column.
  • line 4-6: See how the line starts with an indent which indicates we are still within the mutate() function. You can also see this by counting brackets - in line 3 there are 2 opening brackets but only 1 closes.
    • Similar to Gender, we are replacing the “old” Secondyeargrade with the new Secondyeargrade column that is now a factor.
    • Turning our variable Secondyeargrade into a factor, spot the difference between this attempt and the one we used for Gender? Here we are using a lot more arguments in that factor function, namely levels and labels. Levels describes the unique values we have for that column, and in labels we want to define how these levels will be shown in the data object. If you don’t add the levels and labels argument, the labels will be the labels (as you can see in the Gender column in which we kept the numbers).
  • line 7: Doesn’t start with a function name and has an indent, which means we are still within the mutate() function - count the opening and closing brackets to confirm.
    • Here, we are creating a new column called QRP_item > 4. Notice the two backticks we have to use to make this weird column name work? This is because it has spaces (and we did mention that R doesn’t like spaces). So the backticks help R to group it as a unit/ a single name.
    • Next we have a case_when() function which helps executing conditional statements. We are using it to check whether a statement is TRUE or FALSE. Here, we ask whether the QRP item (column QRP_item) is larger than 4 (midpoint of the scale) using the Boolean operator >. If the statement is TRUE, the label TRUE should appear in column QRP_item > 4. Otherwise, if the value is equal to 4 or smaller, the label should read FALSE. We will come back to conditional statements in Chapter 2. But long story short, this Boolean expression created the only logical data type in data_quiz.

And with this, we are done with the individual walkthrough. Well done :)

Pair-coding

The data we will be using in the upcoming lab activities is a randomised controlled trials experiment by Binfet et al. (2021) that was conducted in Canada.

Citation

Binfet, J. T., Green, F. L. L., & Draper, Z. A. (2021). The Importance of Client–Canine Contact in Canine-Assisted Interventions: A Randomized Controlled Trial. Anthrozoös, 35(1), 1–22. https://doi.org/10.1080/08927936.2021.1944558

Abstract

Researchers have claimed that canine-assisted interventions (CAIs) contribute significantly to bolstering participants’ wellbeing, yet the mechanisms within interactions have received little empirical attention. The aim of this study was to assess the impact of client–canine contact on wellbeing outcomes in a sample of 284 undergraduate college students (77% female; 21% male, 2% non-binary). Participants self-selected to participate and were randomly assigned to one of two canine interaction treatment conditions (touch or no touch) or to a handler-only condition with no therapy dog present. To assess self-reports of wellbeing, measures of flourishing, positive and negative affect, social connectedness, happiness, integration into the campus community, stress, homesickness, and loneliness were administered. Exploratory analyses were conducted to assess whether these wellbeing measures could be considered as measuring a unidimensional construct. This included both reliability analysis and exploratory factor analysis. Based on the results of these analyses we created a composite measure using participant scores on a latent factor. We then conducted the tests of the four hypotheses using these factor scores. Results indicate that participants across all conditions experienced enhanced wellbeing on several measures; however, only those in the direct contact condition reported significant improvements on all measures of wellbeing. Additionally, direct interactions with therapy dogs through touch elicited greater wellbeing benefits than did no touch/indirect interactions or interactions with only a dog handler. Similarly, analyses using scores on the wellbeing factor indicated significant improvement in wellbeing across all conditions (handler-only, d = 0.18, p = 0.041; indirect, d = 0.38, p < 0.001; direct, d = 0.78, p < 0.001), with more benefit when a dog was present (d = 0.20, p < 0.001), and the most benefit coming from physical contact with the dog (d = 0.13, p = 0.002). The findings hold implications for post-secondary wellbeing programs as well as the organization and delivery of CAIs.

However, we accessed the data via Ciaran Evans’ github (https://github.com/ciaran-evans/dog-data-analysis). Evans et al. (2023) published a paper that reused the Binfet data for teaching statistics and research methods. If anyone is interested, the accompanying paper is:

Evans, C., Cipolli, W., Draper, Z. A., & Binfet, J. T. (2023). Repurposing a Peer-Reviewed Publication to Engage Students in Statistics: An Illustration of Study Design, Data Collection, and Analysis. Journal of Statistics and Data Science Education, 31(3), 236–247. https://doi.org/10.1080/26939169.2023.2238018

There are a few changes that Evans and we made to the data:

  • Evans removed the demographics ethnicity and gender to make the study data available while protecting participant privacy. Which means we’ll have limited demographic variables available, but we will make do with what we’ve got.
  • We modified some of the responses in the raw data csv - for example, we took out impossible response values and replaced them with NA.
  • We replaced some of the numbers with labels to increase the difficulty in the dataset for Chapter 2 and Chapter 3.

Task 1: Create a project folder for the lab activities

Since we will be working with the same data throughout semester 1, create a separate project for the lab data. Name it something useful, like lab_data or dogs_in_the_lab. Make sure you are not placing it within the project you have already created today. If you need guidance, see Section 1.2 above.

Task 2: Create a new .Rmd file

… and name it something useful. If you need help, have a look at Section 1.3.

Task 3: Download the data

Download the data here: data_pair_coding. The zip folder contains the raw data file with responses to individual questions, a cleaned version of the same data in long format and wide format, and the codebook describing the variables in the raw data file and the long format.

Unzip the folder and place the data files in the same folder as your project.

Task 4: Familiarise yourself with the data

Open the data files, look at the codebook, and perhaps skim over the original Binfet article (methods in particular) to see what kind of measures they used.

Read in the raw data file as dog_data_raw and the cleaned-up data (long format) as dog_data_long. See if you can answer the following questions.

library(tidyverse)

dog_data_raw <- read_csv("dog_data_raw.csv")
dog_data_long <- read_csv("dog_data_clean_long.csv")
Rows: 284 Columns: 136
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (41): GroupAssignment, L2_1, L2_2, L2_3, L2_4, L2_5, L2_6, L2_7, L2_8, L...
dbl (95): RID, Age_Yrs, Year_of_Study, Live_Pets, Consumer_BARK, S1_1, HO1_1...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 568 Columns: 16
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (4): GroupAssignment, Year_of_Study, Live_Pets, Stage
dbl (12): RID, Age_Yrs, Consumer_BARK, Flourishing, PANAS_PA, PANAS_NA, SHS,...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
  • How many participants took part in the study?

You can see this from dog_data_raw. Each participant ID is on a single row meaning the number of observations is the number of participants.

If you look at dog_data_long, there are 568 observations. Each participant answered the questionnaires pre and post intervention, resulting in 2 rows per participant ID. This means you’d have to divide the number of observations by 2 to get to the number of participants.

  • How many different questionnaires did the participants answer?

The Binfet paper (e.g., Methods section and/or abstract) and the codebook show it’s 9 questionnaires - Flourishing scale (variable Flourishing), the UCLS Loneliness scale Version 3 (Loneliness), Positive and Negative affect scale (PANAS_PA and PANAS_NA), the Subjective Happiness scale (SHS), the Social connectedness scale (SCS), and 3 scales with 1 question each, i.e., perception of stress levels (Stress), self-reported level of homesickness (Homesick), and integration into the campus community (Engagement).

However, if you thought PANAS_PA and PANAS_NA are a single questionnaire, 8 was also acceptable as an answer here.

Test your knowledge

Are you ready for some knowledge check questions to test your understanding of the chapter? We also have some faulty codes. See if you can spot what’s wrong with them.

Knowledge check

Question 1

One of the key first steps when we open RStudio is to:

Opening an existing project (e.g., when coming back to the same dataset) or creating a new project (e.g., for a new task or new dataset) ensures that subsequent .Rmd files, any output, figures, etc are saved within the same folder on your computer (i.e., the working directory). If the.Rmd files or data is not in the same folder as “the project icon”, things can get messy and code might not run.

Question 2

When using the default environment colour settings for RStudio, what colour would the background of a code chunk be in R Markdown?

When using the default environment colour settings for RStudio, what colour would the background of normal text be in R Markdown?

Assuming you have not changed any of the settings in RStudio, code chunks will tend to have a grey background and normal text will tend to have a white background. This is a good way to check that you have closed and opened code chunks correctly.

Question 3

Code chunks start and end with:

Code chunks always take the same general format of three backticks followed by curly parentheses and a lower case r inside the parentheses ({r}). People often mistake these backticks for single quotes but that will not work. If you have set your code chunk correctly using backticks, the background colour should change to grey from white.

Question 4

What is the correct way to include a code chunk in RMarkdown that will be executed but neither the code nor its output will be shown in the final HTML document?

Check the table of knitr display options in Section 1.3.2.

  • {r, echo=FALSE} also executes the code and does not show the code, but it does display the result in the knitted html file. (matches 2/3 criteria)
  • {r, eval=FALSE} does not show the results but does not execute the code and it does show it in the knitted file. (matches 1/3 criteria)
  • {r, results=“hide”} executes the code and does not show results, however, it does include the code in the knitted html document. (matches 2/3 criteria)

Error mode

Some of these codes have mistakes in them, other code chunks are not quite producing what was aimed for. Your task is to spot anything faulty, explain why the things happened, and perhaps try to fix them.

Question 5

You want to read in data with the read_csv() function. You have just stated R, created a new .Rmd file, and typed the following code into your code chunk.

data <- read_csv("data.csv")

However, R gives you an error message: could not find function "read_csv". What could be the reason?

“Could not find function” is an indication that you have forgotten to load in tidyverse. Because read_csv() is a function in the tidyverse collection, R cannot find it.

FIX: Add library(tidyverse) prior to reading in the data and run the code chunk again.

Question 6

You want to read in data with the read_csv() function. This time, you are certain you have loaded in tidyverse first. The code is as follows:

library(tidyverse)
data <- read_csv("data.csv")

The error message shows 'data.csv' does not exist in current working directory. You check your folder and it looks like this:

Why is there an error message?

R is looking for a csv file that is called data which is currently not in the working directory. We may assume it’s in the data folder. Perhaps that happened when unzipping the zip file. So instead of placing the csv file on the same level as the project icon, it was unzipped into a folder named data.

FIX - option 1: Take the data.csv out of the data folder and place it next to the project icon and the .Rmd file.

FIX - option 2: Modify your R code to tell R that the data is in a separate folder called data, e.g., …

library(tidyverse)
data <- read_csv("data/data.csv")

Question 7

You want to load tidyverse into the library. The code is as follows:

library(tidyverse)

The error message says: Error in library(tidyverse) : there is no package called ‘tidyverse’

Why is there an error message and how can we fix this?

If R says there is no package called tidyverse, means you haven’t installed the package yet. This could be an error message you receive either after switching computers or a fresh install of R and RStudio.

FIX: Type install.packages("tidyverse") into your Console.

Question 8

You knitted your .Rmd into a html but the output is not as expected. You see the following:

Why did the file not knit properly?

There is a backtick missing in the code chunk. If you check your .Rmd file, you can see that the code chunk does not show up in grey which means it’s one of the 3 backticks at the beginning of the chunk.

FIX: Add a single backtick manually where it’s missing.