1 Workshop 1: Data Handling Skills

Aims:

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

  • Practice inspecting your data in RStudio.

  • Use different data wrangling functions to develop your data handling skills.

  • Check basic summary statistics.

1.1 Exercise 1: Import the Data

Import the Data File guess_who.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("guess_who.csv")

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

1.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.

Answer Question 2.1 - 2.2 on your worksheet.

1.3 Exercise 3: Change a Variable Name

One of the variable names is quite long. This can be annoying if we have to keep typing it out

Change the variable name do_you_own_a_pet to pet. The rename() function will let you rename a variable.

dataset <- dataset %>%
  rename(pet = do_you_own_a_pet)

Check it has worked:

names(dataset) # ask for the variable names again

1.4 Exercise 4: Remove a Variable

We do not really care about the age variable for the next few exercises.

Let’s remove it.

The code below will create a new object (once we start removing things, it is best to keep the original data file called dataset in the environment)

mydata <- dataset %>%
  select(-age)

This code will:

  • Create a new object called mydata.

  • Take our original data called dataset

  • “And then” %>%

  • Use the select() function to remove age by placing a minus symbol - in front of it.

From now on, we will use the object called mydata and not the original data set.

1.5 Exercise 5: Filter Cases

We can select particular cases in our data set

For example, I could ask: how many people were from the city of Birmingham using the code below:

mydata %>%
  filter(city == "birmingham") %>%
  count()

Check the console (bottom left panel) for the answer.

This code will:

  • Take mydata and then…

  • Filter it by the city variable.

  • We use a double equals symbol == to specify an exact match.

  • I’ve added “birmingham” in speech marks. Note it is lowercase as to match the data set and then…

  • count() the number of data points.

Adapt the code above to answer question 5.1 on the worksheet.

1.6 Exercise 6: Guess Who?

We can filter based on multiple criteria.

The code below will show us someone who is from Brighton, has a dog, and does not drink coffee.

mydata %>%
  filter(city == "brighton", pet == "dog", coffee == 0)

We can also use less than/more than symbols to filter data, For example, this will show all people who have a art enjoyment score of less than 20:

mydata %>%
  filter(maths < 20)

Use what you have learned above and adapt your code to play GUESS WHO? and complete questions 6.1-6.2 on the worksheet.

1.7 Exercise 7: Create a New Variable

Sometimes we might want to compute new scores or variables

Add up the three enjoyment scores for maths, science, and art to create an overall score called total_score.

mydata <- mydata %>%
  mutate(total_score = maths + science + art)

This code will:

  • Take mydata to overwrite it (ready to add the new variable) and then…

  • Use the mutate() function to create a new variable named total_score which should equal = maths + science + art.

View the data set and look for the new column to see it has worked.

view(mydata)

Now, we can look at who had the highest and lowest total enjoyment score.

slice_min will find the row which has the lowest score:

mydata %>%
  slice_min(total_score)

slice_max will find the row which has the highest score:

mydata %>%
  slice_max(total_score)

Answer questions 7.1-7.2 on the worksheet.

1.8 Exercise 8: Counting and Removing Missing Data

Real data sets often are missing data points

Different people have differing views on how to treat missing data points. For today, we will just identify and remove any. If you view the data, you might notice there are some blanks for degree as not everyone is studying for one.

sum(is.na(mydata$degree))

This code will:

  • Calculate the total number using sum() of…

  • Any missing data points (R calls these is.na)

  • We can then direct to a particular column using mydata$degree. This essentially means “look in mydata and then the column called degree. We use the dollar sign $ to specify the column.

If we want to remove them, we can use filter() again!

mydata <- mydata %>% 
  filter(!is.na(degree))

Note: this will overwrite mydata and remove the cases.

Answer question 8.1 on the worksheet.

1.9 Exercise 9: Summary Statistics

We might want to know What was the average enjoyment score?

We can use this code to look across the data set as a whole:

summary(mydata)

Look through the output in the console (bottom left panel) and answer questions 9.1-9.3 on the worksheet.

1.10 Exercise 10: Fixing Luke’s Broken Code

Help!! My code below just is not working. I need your help to get it working.

Fix the code below to work out how many coffees were drunk by the person from canterbury and is studying medicine. Try running the code first and then work out why it doesn’t work!

mydata %>%
  filter(city = "canterberry", degree == medicine)

Fix the code below to work out the name of the person who is from London, has a hamster, and did not drink coffee. Try running the code first and then work out why it doesn’t work!

mydata =
  filter(city == "London", pet == "hamsta", coffee >1)

Answer questions 10.1-10.2 on the worksheet.

If you get stuck, use the hints below.


👀 Click for a hint
  • Check for spelling errors: there are two of them.

  • Make sure to use double equals when specifying a label ==.

  • Use quote marks when necessary. Some are missing.

  • Code is case sensitive. There is a capital letter where there shouldn’t be one.

  • Use symbols correctly. We want to use the pipe %>% before we filter.

  • Use symbols correctly. More than > is not the same as <.



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