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
- 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.
R has included rainbow parentheses to help with keeping count on the brackets.
To enable the feature, go to
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
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
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
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 theAuthor
field author name. Keep theDefault 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.
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.
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
- sub-item 1
- 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.
- list item 1
- list item 2
- 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:
- list item 3
- 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…
- list item 3
- list item 1
- sub-item labelled “a)”
- sub-item labelled “i)”
- sub-item labelled “C)”
- sub-item labelled “Z)”
- 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:
- list item 7
- list item “1)” with 4 spaces
- list item “1)” with 8 spaces
- this is an item labelled “6)” (magically corrected to “2.”)
- list item “1)” with 4 spaces
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 theSource pane
). - Select
Code > Insert Chunk from the menu. - Using the shortcut
Ctrl + Alt + I
for Windows orCmd + Option + I
on MacOSX. - Type
```{r}
and```
manually
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 |
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.
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
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 theunderstanding
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.
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.
── 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).
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.
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.
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.
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.
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
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
.
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
).
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.
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?
- How many different questionnaires did the participants answer?
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: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?
Question 3
Code chunks start and end with: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?
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.
However, R gives you an error message: could not find function "read_csv"
. What could be the reason?
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:
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?
Question 7
You want to load tidyverse
into the library. The code is as follows:
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?
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?