您如何用简单的英语阅读 %in% 运算符?

How do you read the %in% operator in plain English?

我正在为如何用“简单的英语”术语阅读 R 中的 %in% 运算符而苦恼。我看过多个使用它的代码示例,但没有清楚地解释如何阅读它。

例如,我找到了管道运算符 %>% 的术语,建议将其读作“然后”。我正在寻找 %in% 运算符的类似翻译。

R for Data Science 一书第 5 章标题为“数据转换”,其中有一个来自航班数据集的示例,内容如下:

The following code finds all flights that departed in November or December:

filter(flights, month == 11 | month == 12)

A useful short-hand for this problem is x %in% y. This will select every row where x is one of the values in y. We could use it to rewrite the code above:

nov_dec <- filter(flights, month %in% c(11, 12))

当我读到“一个对这个问题有用的short-hand是x %in% y”,然后再看nov_dec的例子,好像这个要理解为“select 每一行,其中月份 (x) 是 c(11,12) (y) 中的值之一,”这对我来说没有意义。

但是我的大脑想要将其解读为“在月份列中查找 1112”。在此示例中,x 似乎应该是 1112 的值,并且 %in% 运算符正在检查这些值是否在 y 中,这将成为月份列。我的大脑正在从 从右到左阅读这个例子。

但是,我发现的所有代码示例似乎都表明这个 x %in% y 应该从左到右而不是从右到左阅读。

任何人都可以帮我阅读外行术语中的%in%运算符吗?示例将不胜感激。

这真是个好问题。想想这里的字面性质:

  • 答案是肯定的吗?
  • 答案是否定的?
  • 是还是不是?
  • 答案是否定的?

当您使用 %in% 时,它代替了 'or' 语句—— any 这些 in 这里?

answers = data.frame(ans = sample(rep(c("yes","no","maybe"),
                                      each = 3, times = 2)), 
                     ind = 1:9)

# yes or no?
answers[answers$ans == "yes"|answers$ans == "no",]
#    ans ind
# 1  yes   1
# 2  yes   2
# 4   no   4
# 5  yes   5
# 6   no   6
# 8   no   8
# 10 yes   1
# 12  no   3
# 13  no   4
# 16 yes   7
# 17  no   8
# 18 yes   9 

# now about %in%
answers[answers$ans %in% c("yes","no"),]
#    ans ind
# 1  yes   1
# 2  yes   2
# 4   no   4
# 5  yes   5
# 6   no   6
# 8   no   8
# 10 yes   1
# 12  no   3
# 13  no   4
# 16 yes   7
# 17  no   8
# 18 yes   9 


# yes and no?
answers[answers$ans == c("yes","no"),]
#    ans ind
# 1  yes   1
# 4   no   4
# 5  yes   5
# 6   no   6
# 8   no   8
# 12  no   3 

# what happened here? were you expecting that? 
# this checked the first row for yes, 
 # the second row for no, 
  # the third row for yes,
   # the fourth row for no and so on...

如果我真的想“拼写出来”,我会将 x %in% y 读作“对于每个 x 值,它是否在 y 中”?

nov_dec <- filter(flights, month %in% c(11, 12))"

When I read "A useful short-hand for this problem is x %in% y," and then look at the nov_dec example, it seems like this is to be understood as "select every row where month ('x') is one of the values in c(11,12) ('y'), which doesn't make sense to me.

However my brain wants to read it as something like, "Look for 11 and 12 in the month column." In this example, it seems like 'x' should be the values of 11 and 12 and the %in% operator is checking if those values are in 'y' which would be the month column. My brain is reading this example from right to left.

左与右的关系就是您要问的问题。 x %in% y 是在问(使用我上面的冗长措辞),“ 对于每个 x,它在 y 中吗?”通过这种措辞,我们知道 x.

中的每个项目都需要一个答案(TRUEFALSE

如果我们再扩展一点,这实际上可能会变得更清楚 - 两个常见的相关问题是“y 中是否有任何 x 值?”和“y 中的所有 x 值”?这些可以自然地编码为

any(x %in% y)  # Are any x values in y?  
all(x %in% y)  # Are all x values in y?

至少对我来说,那些看起来很自然,而且他们使用从左到右的阅读方式。尝试在此处使用从右到左的阅读方式会令人费解,例如 “在 x 中查找 y 值,您是否涵盖了每个 x你的匹配值?"

我认为您的脱节是理解如何将“in”应用于矢量。您写道,您希望将其解读为“在月份列中查找 11 和 12”。你确实可以这么想。你的例子是:

nov_dec <- filter(flights, month %in% c(11, 12))

这可以用简单的英语表达为:

Give me all the flights where one of the values in c(11, 12) is in the month column

但我们也可以说 11 和 12 在向量 c(11, 12) 中。这就是从左到右的阅读方式:

Give me all the flights whose month is in the vector c(11, 12).

或者,表达方式略有不同且更冗长:

Give me all the flights whose month is equal to one of the values in the vector c(11, 12)

这在概念上类似于连续使用一堆 | 运算符 (month == 11 | month == 12),但最好不要将它们视为完全等同。您没有明确地将 xy 中的每个值进行比较,而是在问“x 是否等于 y 中的值之一?”这就像说“请关灯”和说“请走到墙上的那个盘子前,把上面的小棍子向下拉”是不同的。它表达了你想要什么而不是如何弄清楚,这让你的代码更具可读性,而且代码被阅读的次数比写的次数多,所以这很重要!!!

现在我要离开我的领域了——同样,我不知道 R 在这里实际做了什么——但回答问题的基本方法也可能不同。它可能会使用 binary search 算法来找出 x 是否在 y.