library(learnr)
library(testwhat)
knitr::opts_chunk$set(echo = FALSE)
tutorial_options(exercise.timelimit = 60, exercise.checker=testwhat::testwhat_learnr)

require(tidyverse)
colour <- tibble(Name=c("Sophie","Lauren","Greg","Callum","Ainsley","Alistair"), colour=sample(c("Pink","Blue","Red","Green","Purple","Yellow")))

pet <- tibble(Name=c("Stacey","Lauren","Ainsley","Graham","Chris","Greg"), Pet=c("Dog","Cat","Dog","Fish","Hamster","Cat"))

I have two data sets, colour and pet, which are shown below:

knitr::kable(colour)

knitr::kable(pet)

Exercise 1

I want to keep all rows in both data sets while merging rows which have a match.

question("What type of join could you use here?",
  answer("left"),
  answer("right"),
  answer("full", correct=TRUE),
  answer("inner"),
  answer("anti"),
  answer("semi")
)

What would the code be to do this in R?


colour %>% full_join(pet)
# this is really unsatisfying - come back to this at some point
ex() %>% check_function("full_join") %>% check_result()

Exercise 2

I want to merge the two data sets but only keep the rows in both data sets which have a match.

question("What type of join could you use here?",
  answer("left"),
  answer("right"),
  answer("full"),
  answer("inner", correct=TRUE),
  answer("anti"),
  answer("semi")
)

What would the code be to do this in R?


colour %>% inner_join(pet)
# this is really unsatisfying - come back to this at some point
ex() %>% check_function("inner_join") %>% check_result()

Exercise 3

I want to keep all the rows in the pets data set that have a match in the colour data set - I do not want to merge the data sets.

question("What type of join could you use here?",
  answer("left"),
  answer("right"),
  answer("full"),
  answer("inner"),
  answer("anti"),
  answer("semi", correct=TRUE)
)

What would the code be to do this in R?


pet %>% semi_join(colour)
# this is really unsatisfying - come back to this at some point
ex() %>% check_function("semi_join") %>% check_result()

Exercise 4

I want to keep all the rows in the colour data set that have a match in the pet data set - I do not want to merge the data sets.

question("What type of join could you use here?",
  answer("left"),
  answer("right"),
  answer("full"),
  answer("inner"),
  answer("anti", correct=TRUE),
  answer("semi")
)

What would the code be to do this in R?


colour %>% anti_join(pet)
# this is really unsatisfying - come back to this at some point
ex() %>% check_function("anti_join") %>% check_result()


kate-pyper/MM916ProgrammingExercises documentation built on Oct. 15, 2020, 10:40 p.m.