%in%
If rather than an index we want a logical that tells us whether or not each element of a first vector is in a second, we can use the function %in%
.
For example:
x <- c(2, 3, 5)
y <- c(1, 2, 3, 4)
x%in%y
Gives us two TRUE
followed by a FALSE
because 2 and 3 are in y
but 5 is not.
This exercise is part of the course
Data Science R Basics
Exercise instructions
Which of the following are actual abbreviations: MA, ME, MI, MO, MU?
- Define a character vector with the abbreviations MA, ME, MI, MO, MU.
- Use the
%in%
operator to create a logical vector that is TRUE when the abbreviation is inmurders$abb
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Store the 5 abbreviations in `abbs`. (remember that they are character vectors)
# Use the %in% command to check if the entries of abbs are abbreviations in the the murders data frame