3.1 Aperitif

Palmer Penguins
Palmer Penguins

3.1.1 Counting Penguins

Consider this code to count the number of Gentoo penguins in the penguins data set. We see that there are 124 Gentoo penguins.

sum("Gentoo" == penguins$species)
# output: 124

3.1.2 In

One subtle error can arise in trying out %in% here instead.

species_vector <- penguins |> select(species)
print("Gentoo" %in% species_vector)
# output: FALSE
Where did the penguins go?
Where did the penguins go?

3.1.3 Fix: base R

species_unlist <- penguins |> select(species) |> unlist()
print("Gentoo" %in% species_unlist)
# output: TRUE

3.1.4 Fix: dplyr

species_pull <- penguins |> select(species) |> pull()
print("Gentoo" %in% species_pull)
# output: TRUE

3.1.5 Motivation

  • What are the different types of vectors?
  • How does this affect accessing vectors?
Side Quest: Looking up the %in% operator

If you want to look up the manual pages for the %in% operator with the ?, use backticks:

?`%in%`

and we find that %in% is a wrapper for the match() function.