简体   繁体   中英

Can someone explain the order of arguments for %in% in R software?

"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?

%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

```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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM