DAPR1 Workshops
  • 1: Exploratory Data Analysis
    • W1: Introductions
    • W2: Data & Variables
    • W3: Visualising Distributions
    • W4: Summarising Distributions I – Central Tendency
    • W5: Summarising Distributions II – Variability
  • 2: Introduction to Inference
    • W7: Z-scores & the Normal Distribution
  • 3: Common Statistical Tests
  • 4: The Move to Modelling

W2: Data & Variables

This week’s workshop we’re going to start working together in groups.

Pick one of you to be the ‘driver’. They will type the code. The rest of you are navigators, you need to tell the driver where to go (i.e., what to type!).

Driver instructions:

  1. Log in to the bigger screen at your table
  2. Open RStudio Server. See the first drop-down in the FAQs page for instructions
  3. Listen to your navigators

Navigator instructions:

  1. Tell your driver where to go (i.e., what to type)!
Question 1

Open a new R script for this week.

The first thing we’re going to do is to get some practice with packages.

  1. load the “tidyverse” (it should be already installed on the server)
  2. install and load the “skimr” package using the code below:
install.packages("write_package_name_here", type="source")

In general, you would only write install.packages("write_package_name_here") to install packages. However, because we are using RStudio on the browser via an online server, we also need the additional argument type="source", but don’t worry too much about what it means.

🗂️ See Use R packages flash card.

Solution 1. To load the tidyverse, we just need to do:

library(tidyverse)

To install the “skimr” package, we need to use install.packages("skimr"). BUT, we probably don’t want to keep this in our script, because then it would re-install the package every time we ran the whole script.

Installing packages is one of the few things we might type specifically in the console

install.packages("skimr", type="source")

And then back in our script we will want:

library(skimr)

Question 2

Now that we have loaded the packages we need, we want to read in some data!
Last week, we put a little survey up for you to fill out, so that we could have data for you to play with.

The data are available at: https://uoepsy.github.io/data/dapr1_2627_survey.csv

Read it in to your R session now. Be sure to assign it a name!

🗂️ See Read in and inspect data flash card.

Solution 2.

sdata <- read_csv("https://uoepsy.github.io/data/dapr1_2627_survey.csv")

Question 3

How many people do we have data for? And on how many variables?

🗂️ See Read in and inspect data flash card.

TipHints

Functions like dim(data), nrow(data), ncol(data) will come in handy here.

You can also try using the skim(data) function (from the “skimr” package we installed a minute ago) — it will give you loads of information!

Solution 3.

nrow(sdata)
[1] 134
ncol(sdata)
[1] 15
dim(sdata)
[1] 134  15

We have data for 134 people, and on 15 variables.

skim(sdata)
Data summary
Name sdata
Number of rows 134
Number of columns 15
_______________________
Column type frequency:
character 7
numeric 8
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
pseudonym 31 0.77 1 18 0 102 0
birthmonth 0 1.00 3 9 0 12 0
spicy 0 1.00 3 10 0 6 0
eye_colour 0 1.00 4 5 0 6 0
fav_color 0 1.00 3 6 0 9 0
catdog 0 1.00 3 7 0 4 0
threewords 25 0.81 2 45 0 109 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
n_sibling 1 0.99 1.75 2.28 0 1.0 1.00 2.00 21 ▇▁▁▁▁
spicy_num 0 1.00 2.89 1.11 1 2.0 3.00 3.00 6 ▇▆▂▁▁
distance_born 1 0.99 3111.16 3938.52 0 74.0 657.00 6733.00 17000 ▇▁▂▁▁
outlook 0 1.00 21.83 45.86 -100 -10.0 23.50 52.25 100 ▁▅▇▇▆
ampm 0 1.00 3.47 2.82 0 1.1 2.75 5.00 10 ▇▅▃▂▂
sleepqual 5 0.96 55.37 20.29 5 46.0 57.00 68.00 100 ▂▂▇▅▂
procrast 1 0.99 60.61 25.49 7 40.0 65.00 80.00 100 ▃▅▅▇▅
multitask 2 0.99 56.50 21.21 3 47.0 56.00 72.00 100 ▂▃▇▇▂

Question 4

Go through each variable and write down what type of variable it is (e.g., is it continuous, categorical, or ordinal?)

When you’re done, you can go through your answers with a tutor to check if you are correct.

TipHints

Revisit the slides about different data types.

Solution 4.

  • pseudonym (Name) is categorical (nominal), and it is the identifier variable.
  • birthmonth (Birthday month) could be thought of as categorical (ordinal) or categorical (nominal). If you thought ordinal, it’s because you thought that “January” comes before “February” in the calendar. If you thought “nominal”, it’s because it’s not really the case that “February” is better than “January”.
  • n_sibling (Number of siblings) is numerical (discrete).
  • spicy (Spicy food preference, in words) is categorical (ordinal).
  • spicy_num (Spicy food preference, as a number 1-6) is stored as numerical (discrete) data. However, this is a numerical encoding of a categorical (ordinal) variable, spicy. In other words, it’s the same information as spicy, just recoded as a number and the numbers are not really meaningful in themselves as they could also be recorded as 0-5 for example.
  • eye_colour is categorical (nominal).
  • distance_born (Distance between Edinburgh and birthplace) is numerical (continuous).
  • outlook (Optimism scale, -100 to 100) is numerical (continuous).
  • ampm (Morningness scale, -100 to 100) is numerical (continuous).
  • sleepqual (Sleep quality, 0-100) is numerical (continuous).
  • procrast (Procrastination scale, 0-100) is numerical (continuous).
  • multitask (Multi-tasking scale, 0-100) is numerical (continuous).
  • fav_color (Favourite colour) is categorical (nominal).
  • catdog (Preferred pet) is categorical (nominal).
  • threewords (Description of self) is categorical (nominal), and is a free text response so it’s rare for two different people to have the same answer.

Question 5

R will by default treat a variable with only numbers in it as a numeric variable, and anything else as a “character” variable (“character” = any letters/numbers/symbols).

Change the eye_colour variable to a factor in R, that only has the options “brown”, “blue”, “green” or “hazel”.

🗂️ See Mutate columns and Manage factor variables flash cards.

What happens when you now do skim(data)? How does it treat this variable differently?

TipHints

Here is the basic code structure for taking a dataframe named mydata and adding a new variable (or changing an existing one).

  • Line 2 takes the data, and passes it to line 3.
  • Lines 3-5 “mutates” the data, adding a variable called new_variable. The new_variable is going to contain the values of an existing variable (old_variable) but with some function applied to them. For example, if we had new_variable = old_variable/2 then it would contain the same values but halved.
  • It feels odd, but it helps to think about the first line of code once we’ve thought of the others. This line re-assigns the name mydata to the mutated mydata, thereby making the change save to the object in R’s memory.
mydata <-
  mydata |> 
  mutate(
    new_variable = function(old_variable)
  )

To make a variable a factor, the function we want is factor(). This will consider every different value that we have to be a possible category:

factor(variable)

If we want to have a specific set of possible values, we can do so with the levels part of the function, replacing the "..." with the options we consider valid:

factor(variable, levels = c("...","...","...", ....... ))

Solution 5.

sdata <-
  sdata |>
  mutate(
    eye_colour = factor(eye_colour, levels = c("brown","blue","green","hazel"))
  )


skim(sdata)
Data summary
Name sdata
Number of rows 134
Number of columns 15
_______________________
Column type frequency:
character 6
factor 1
numeric 8
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
pseudonym 31 0.77 1 18 0 102 0
birthmonth 0 1.00 3 9 0 12 0
spicy 0 1.00 3 10 0 6 0
fav_color 0 1.00 3 6 0 9 0
catdog 0 1.00 3 7 0 4 0
threewords 25 0.81 2 45 0 109 0

Variable type: factor

skim_variable n_missing complete_rate ordered n_unique top_counts
eye_colour 12 0.91 FALSE 4 bro: 64, blu: 39, gre: 13, haz: 6

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
n_sibling 1 0.99 1.75 2.28 0 1.0 1.00 2.00 21 ▇▁▁▁▁
spicy_num 0 1.00 2.89 1.11 1 2.0 3.00 3.00 6 ▇▆▂▁▁
distance_born 1 0.99 3111.16 3938.52 0 74.0 657.00 6733.00 17000 ▇▁▂▁▁
outlook 0 1.00 21.83 45.86 -100 -10.0 23.50 52.25 100 ▁▅▇▇▆
ampm 0 1.00 3.47 2.82 0 1.1 2.75 5.00 10 ▇▅▃▂▂
sleepqual 5 0.96 55.37 20.29 5 46.0 57.00 68.00 100 ▂▂▇▅▂
procrast 1 0.99 60.61 25.49 7 40.0 65.00 80.00 100 ▃▅▅▇▅
multitask 2 0.99 56.50 21.21 3 47.0 56.00 72.00 100 ▂▃▇▇▂

Finishing up

Once you’ve finished, don’t forget to:

  1. Save your R script
  2. Post the R script to the group discussion space on Learn, to ensure that everyone has access to it! (For step-by-step instructions on how to do this, click here)