1

"gapminder" is a dataset with variables like fertility rates and GDP for each country and each year. I want to access and compare the fertility rates of Turkey and Sri Lanka in the year 2015.

library(dslabs)
data(gapminder)

How do I know which argument comes first when I use %in%? In my R basics course, it was explained that first we write what we look for, then the pool in which we search for it. So in in my example, we look for the countries Sri Lanka and Turkey among all countries. The code should be

gapminder %>%
  filter(year == 2015 & c("Sri Lanka", "Turkey") %in% country)

But this is wrong and returns something else than the code provided:

gapminder %>%
  filter(year == 2015 & country %in% c("Sri Lanka", "Turkey"))

Yet the documentation clearly states: "x %in% table, x = the values to be matched (for us Turkey, Sri Lanka), table = the values to be matched against (list of countries)". Why is my first code wrong then? Can someone give me a simple rule of which argument to write first?

2
  • 1
    c("Sri Lanka", "Turkey") %in% country returns a boolean of two, that is, whether Sri Lanka or Turkey is in country, and this will not work for your data.frame
    – StupidWolf
    May 13, 2020 at 5:53
  • 1
    you need country %in% c("Sri Lanka", "Turkey") because this is a boolean that is as long as your data.frame
    – StupidWolf
    May 13, 2020 at 5:54

2 Answers 2

2

%in% looks up if the elements of one object are present in another element. It internally uses match and returns logical (boolean) values. Example:

a <- c("C", "A", "B")
b <- c("A", "B")

a %in% b
# [1] FALSE  TRUE  TRUE
b %in% a
# [1] TRUE TRUE
match(a, b)
# [1] NA  1  2
match(b, a)
# [1] 2 3
1
  • 1
    Thank you, this combined with what StupidWolf said answered my question.
    – E_H
    May 13, 2020 at 8:27
0

```dplyr and I believe all tidyverse family packages take an R dataframe as an argument. Make sure gapminder is an R dataframe. You can use class(gapminder) to check the object type. Other than that your code is correct and should produce the desired output. And as for the order of arguments, the first function after the %>% would take the object that preceded the %>%. In this case the first argument to filter function is gapminder. That should give you the data from year 2015 and where the country is Sri Lanka and Turkey


data(gapminder)
gapminder %>%
  filter(year == 2015 & country %in% c("Sri Lanka", "Turkey"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.