2 Workshop 2: Summarising and Describing Data

Aims:

  • Practice importing a .csv data file into RStudio using read_csv()

  • Practice inspecting your data in RStudio.

  • Calculate mean and standard deviation using the group_by() and summarise() functions.

  • Visually inspect data using plots and describe data distributions.

2.1 Exercise 1: Import the Data

Import the Data File: stroop.csv

Before you begin, you will need the tidyverse package loaded.

install.packages("tidyverse") #install tidyverse if you do not have it.
library(tidyverse) #loads tidyverse.

Next import the data file and store it as an object called dataset.

dataset <- read_csv("stroop.csv")

If you see an error saying cannot find function read_csv() this usually means you have not loaded (or installed) the tidyverse package.

2.2 Exercise 2: Inspect and Check Your Data

Take a look at your newly imported data file

Check the top right panel (the environment) and also use the code below to inspect your data set.

view(dataset) # this will open the data in a new tab.
names(dataset) # this will show the variable names.

It is really important to look at the variable names as you’ll be using them in code later on.

2.3 Exercise 3: Calculate the Stroop Inteference Score

Sometimes we might want to compute new scores or variables

Calculate the Stroop interference measure. This should be the difference between the incongruent and congruent conditions. Take the incongruent reaction times and then subtract the congruent reaction times using the code below:

mydata <- dataset %>%
  mutate(int = incongruent - congruent)

This code will:

  • Create an object called mydata before using the original dataset and then…

  • Use the mutate() function to create a new variable named int which should equal = incongruent - (minus) congruent.

View the data set and look for the new column to see it has worked. Note: check the final column to see if int has appeared.

view(mydata)

What exactly is this Stroop Interference thingy? If you want to learn more, see below.


👀 Click for more information

The interference measure in milliseconds (msecs) is the amount of extra time it took a participant to answer the incongruent (trickier trials because the colours do not match the word) compared to the congruent trials (easier trial because the colours do match the work).

In a sense, it is how many milliseconds slower you are because you need to focus your attention and engage executive functions to fight the urge to read the word rather than name the colour.

A smaller number is perhaps indicative of having better attention/executive functioning processes!


Answer questions 3.1-3.2 on the worksheet.

2.4 Exercise 4: Calculate Descriptive Statistics

Just as shown in the lecture, use the code below to calculate the mean and standard deviation for Stroop interference or int. Remember, we want to use int that was calculated in exercise 3.

desc <-  mydata %>%
  group_by(NULL1) %>%
  summarise(mean_int = mean(NULL2),
            sd_int = sd(NULL2))

You will need to change NULL to match your data set. Try and give this a go on your own first, but if you aren’t sure look below for help.

Think about:

  • For NULL1: What is the name of the variable you will split the data file by (e.g., what is the grouping variable/independent variable called in mydata)

  • For NULL2: What is the name of the score that you want to find the mean and standard deviation for (e.g., what is the dependent variable called in mydata)


👀 Click for a hint
desc <-  mydata %>%
  group_by(drink) %>%
  summarise(mean_int = mean(int),
            sd_int = sd(int))

If you look in the environment (top right panel) you will see a new object called desc. This is where your descriptive statistics are stored. I called it desc but you can call it anything you like. It is best to keep object names short and informative. We can now view that object using the view() function.

view(desc)

Answer questions 4.1-4.2 on the worksheet.

2.5 Exercise 5: Explore Data with Plots

Generate a box plot:

ggplot(mydata, aes(x = drink, y = int)) +
  geom_boxplot(width = .4)

Generate histograms:

ggplot(mydata, aes(x = int, fill = drink)) +
  geom_histogram(colour = "black") +
  facet_wrap(~ drink)

Generate density plots:

ggplot(mydata, aes(x = int, fill = drink)) +
  geom_density(alpha = .5) +
  facet_wrap(~ drink)

Answer questions 5.1-5.2 in the worksheet.

2.6 Exercise 6: What Does facet_wrap() do?

Re-run the density plot code, except this time delete the final line. This will show what facet_wrap() does. What do you notice about the plot now?

Use this code without facet_wrap().

ggplot(mydata, aes(x = int, fill = drink)) +
  geom_density(alpha = .5)

Answer question 6.1 on the worksheet.

2.7 Exercise 7: Save Your Amended Data File

Your current data file has the int column in, calculated in exercise 3. You need to save it so you can use it for next week’s workshop. You can overwrite you original .csv file using this code, which will save today’s data set.

write.csv(mydata, "stroop.csv")

This means the stroop.csv file on your computer will be updated and ready to use next week! Make sure you know where it has saved on your computer before you leave. You will need this file next week!


Well Done. You have reached the end of the workshop.