Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何用简单的英语阅读%in%运算符?_R_Operators_Terminology - Fatal编程技术网

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

R 如何用简单的英语阅读%in%运算符?,r,operators,terminology,R,Operators,Terminology,我正在努力学习如何用“普通英语”的术语阅读R中的%操作符中的%in%。我已经看到了许多代码的使用示例,但没有清楚地解释如何阅读它 例如,我发现管道操作符%%>%%的术语建议将其理解为“然后”。我正在为%操作符中的%寻找类似的翻译 在第5章题为“数据转换”的《数据科学R》一书中,有一个来自flights数据集的示例,内容如下: 以下代码查找11月或12月起飞的所有航班: filter(flights, month == 11 | month == 12) 这个问题的一个有用的简写是%y中的x%。

我正在努力学习如何用“普通英语”的术语阅读R中的%操作符中的
%in%。我已经看到了许多代码的使用示例,但没有清楚地解释如何阅读它

例如,我发现管道操作符
%%>%%
的术语建议将其理解为“然后”。我正在为%
操作符中的
%寻找类似的翻译

在第5章题为“数据转换”的《数据科学R》一书中,有一个来自flights数据集的示例,内容如下:

以下代码查找11月或12月起飞的所有航班:

filter(flights, month == 11 | month == 12)
这个问题的一个有用的简写是%y中的
x%
。这将选择x是y中的一个值的每一行。我们可以用它重写上面的代码:

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

这是一个非常好的问题。考虑一下这里的字面性质:

  • 答案是肯定的吗
  • 答案是否定的吗
  • 答案是肯定还是否定
  • 答案是肯定的还是否定的
当您在%
中使用
%时,它代替了“or”语句——这里有这些语句吗

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


至少对我来说,这些看起来很自然,他们使用从左到右的阅读方式。在这里尝试使用从右到左的读数会让人费解,比如“在
x
中查找
y
值,您是否用匹配项覆盖了每个
x
值?”

%in%
将为回答问题的
x
的每个元素返回一个逻辑向量,是否在
y
中找到
x
?一次考虑一个元素:
x
的第一个元素是在
y
中找到的,是
x
的第二个元素是在
y
中找到的,依此类推。
x
的值是在向量
y
中找到的吗在filter()函数中对其实现的描述可以改进一点,即“选择变量x中的值存在于列表y的值中的行”。具体来说,可以说
x=12
y=c(11,12)
。在这种情况下,
x%y
询问
12
是否在向量
c(11,12)
中。因为它是,
x%in%y
返回
TRUE
。改为说
z=13
,你在%y中问
z%,然后你得到
FALSE
,因为
13
不是向量
c(11,12)
中的一个元素。换句话说,过滤器示例保留数据集中的行,其中
flights
,其中
month
11
12
,并删除所有其他行。它可能是month的数字表示形式。例如,如果月份为6月且
month=6
,因此
x=month=6
则%y
中的
x%可能返回
FALSE
,因为
6
不等于
11
12
。如果月份是12月的数字表示,那么这句话应该是
TRUE
。我认为在读数中使用“any”是令人困惑的。“x中有x吗?”只有一个是/否答案@GregorThomas的“对于每个x,是否在y中?”的答案与x元素的答案一样多,这与%
中的
%匹配。
flights
是来自
nycflights13
包的数据集。
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...
nov_dec <- filter(flights, month %in% c(11, 12))"
any(x %in% y)  # Are any x values in y?  
all(x %in% y)  # Are all x values in y?