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()andsummarise()functions.Visually inspect data using plots and describe data distributions.
2.1 Exercise 1: Import the Data
Import the Data File: sleep.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("sleep.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 Total Sleep Score
Sometimes we might want to compute new scores or variables
Add up the two sleep scores sleep_quality and drowsy scores to create an overall score called total_sleep.
This code will:
Create an object called
mydatabefore using the originaldatasetand then…Use the
mutate()function to create a new variable namedtotal_sleepwhich should equal=scale_1+scale_2.
View the data set and look for the new column to see it has worked.
view(mydata)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 total_sleep. Remember, we want to use the total score calculated in exercise 3, and not to use the two subscales.
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:
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 the data set)
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 the data set)
👀 Click for a hint
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 = energy_drink, y = total_sleep)) +
geom_boxplot(width = .4)Generate histograms:
ggplot(mydata, aes(x = total_sleep, fill = energy_drink)) +
geom_histogram(colour = "black") +
facet_wrap(~ energy_drink)Generate density plots:
ggplot(mydata, aes(x = total_sleep, fill = energy_drink)) +
geom_density(alpha = .5) +
facet_wrap(~ energy_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 = total_sleep, fill = energy_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 total_sleep column in. 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, "sleep.csv")This means the sleep.csv 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.
Well Done. You have reached the end of the workshop.