Associated Material

Module: Module 03 - Subsetting

Reading:


Subsetting of vectors

  • Using [] for extracting elements
  • Subsetting by index
  • Subsetting by boolean vector through conditional statements
  • Use of %in%

Missing Data

  • Defining missing data with NA
  • Testing for missing data with is.na()
  • Removal of missing data
    • [] and is.na()
    • functions with an na.rm parameter

Subsetting in 2 dimentions

  • Using [] with 2 arguments for [rows, columns]
  • Introducing select for columns, and filter for rows from dplyr package
    • Choosing and reordering columns with select
    • Use of between with filter

Piping data between functions

  • Use of the %>% “pipe” from the magrittr package

Data manipulations

  • Creating new columns
    • Using $
    • Using mutate
  • Summarising data
    • summarise from dplyr
    • using group_by to summarise by a categorical variable
  • Sorting data
    • arrange
    • Use of desc to arrange in descending order

Workflows

Data subsetting and manipulations combined together with pipes to then go into ggplot.

Exercises

  1. Create the following vector and remove the missing values from it
missing_vec <- c(5, 32, NA, 94, NA, 1)
  1. Load the Palmer Penguins dataset as below and remove all the missing values, save to a variable called penguins_complete.
# if you don't have it installed
# install.packages('palmerpenguins')

library(tidyverse)
library(palmerpenguins)
penguins
  1. How many penguins in pengiuns_complete have a bill_length_mm larger than 38 mm (the n() from dplyr might prove useful). Does this number change by

    1. species?
    2. island?
  2. with penguins_complete, make a plot comparing body mass (in kg) versus flipper length for only the Chinstrap penguins. Create nice labels.

  3. With the same data as 4), create a new column long_flippers that specifies TRUE/FALSE if the flipper length is longer than 200 mm, and use this column to colour your plot so that the long flipper penguins get highlighted

Example solutions

missing_vec <- c(5, 32, NA, 94, NA, 1)

missing_vec[!is.na(missing_vec)]
#> [1]  5 32 94  1

penguins_complete <- penguins %>% 
  filter(!is.na(bill_length_mm) | 
           !is.na(bill_depth_mm) | 
           !is.na(flipper_length_mm) |
           !is.na(body_mass_g)| 
           !is.na(sex))

penguins_complete %>% 
  filter(bill_length_mm > 38) %>% 
  summarise(n = n())
#> # A tibble: 1 × 1
#>       n
#>   <int>
#> 1   280

# a)
penguins_complete %>% 
  filter(bill_length_mm > 38) %>% 
  group_by(species) %>% 
  summarise(n = n())
#> # A tibble: 3 × 2
#>   species       n
#>   <fct>     <int>
#> 1 Adelie       89
#> 2 Chinstrap    68
#> 3 Gentoo      123

# b)
penguins_complete %>% 
  filter(bill_length_mm > 38) %>% 
  group_by(island) %>% 
  summarise(n = n())
#> # A tibble: 3 × 2
#>   island        n
#>   <fct>     <int>
#> 1 Biscoe      150
#> 2 Dream        99
#> 3 Torgersen    31

penguins_complete %>% 
  mutate(body_mass_kg = body_mass_g/1000) %>% 
  filter(species == "Chinstrap") %>% 
  ggplot(mapping = aes(x = body_mass_kg, y = flipper_length_mm)) +
  geom_point() +
  labs(x = "Body Mass (Kg)",
       y = "Flipper Length (mm)",
       title = "Body Mass vs Flipper Length",
       subtitle = "in Chinstrap Penguins")


penguins_complete %>% 
  mutate(body_mass_kg = body_mass_g/1000,
         long_flippers = flipper_length_mm > 200) %>% 
  filter(species == "Chinstrap") %>% 
  ggplot(mapping = aes(x = body_mass_kg, y = flipper_length_mm, colour = long_flippers)) +
  geom_point() +
  labs(x = "Body Mass (Kg)",
       y = "Flipper Length (mm)",
       title = "Body Mass vs Flipper Length",
       subtitle = "in Chinstrap Penguins",
       colour = "Flippers > 200 mm")

LS0tCnRpdGxlOiAiWm9vbSBOb3RlcyAwMzogU2VsZWN0aW5nIGFuZCBGaWx0ZXJpbmcgRGF0YSIKZGF0ZTogIlNlbWVzdGVyIDIsIDIwMjIiCm91dHB1dDoKICBodG1sX2RvY3VtZW50OgogICAgdG9jOiB0cnVlCiAgICB0b2NfZmxvYXQ6IHRydWUKICAgIHRvY19kZXB0aDogMwogICAgY29kZV9kb3dubG9hZDogdHJ1ZQogICAgY29kZV9mb2xkaW5nOiBzaG93Ci0tLQoKYGBge3Igc2V0dXAsIGluY2x1ZGU9RkFMU0V9CmxpYnJhcnkoa25pdHIpCmxpYnJhcnkodGlkeXZlcnNlKQpsaWJyYXJ5KHBhbG1lcnBlbmd1aW5zKQoKa25pdHI6Om9wdHNfY2h1bmskc2V0KAogIGNvbW1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