在子集函数和逻辑运算符中使用多个条件
如果我想在R中选择数据的子集,我可以使用subset函数。我想对符合几个标准之一的数据进行分析,例如某个变量是 1、2 或 3。 我尝试过,
myNewDataFrame <- subset(bigfive, subset = (bigfive$bf11==(1||2||3)))
它总是只选择与第一个条件相匹配的值,这里是 1。我的假设是,它将从 1 开始,如果它确实评估为“假”,它将继续到 2,而不是 3,如果没有一个与 == 后面的语句匹配,则为“假”,如果其中一个匹配,则为“真”。
得到了正确的结果,那么:为什么第一种方法不起作用?
newDataFrame <- subset(bigfive, subset = (bigfive$bf11==c(1,2,3)))
我使用但我希望能够通过逻辑运算符选择数据
If I want to select a subset of data in R, I can use the subset function. I wanted to base an analysis on data that that was matching one of a few criteria, e.g. that a certain variable was either 1, 2 or 3.
I tried
myNewDataFrame <- subset(bigfive, subset = (bigfive$bf11==(1||2||3)))
It did always just select values that matched the first of the criteria, here 1. My assumption was that it would start with 1 and if it does evaluate to "false" it would go on to 2 and than to 3, and if none matches the statement after == is "false" and if one of them matches, it is "true".
I got the right result using
newDataFrame <- subset(bigfive, subset = (bigfive$bf11==c(1,2,3)))
But I would like to be able to select data via logical operators, so: why did the first approach not work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里正确的运算符是
%in%
。下面是一个虚拟数据的示例:给出:
dat
的子集,其中bf11
等于集合1,2,3
中的任何一个被视为使用%in%
如下:至于为什么你的原始版本不起作用,请分解它以查看问题。看看
1||2||3
的计算结果:使用
|
会得到相同的结果。因此,subset()
调用只会返回bf11
为TRUE
的行(或计算结果为TRUE
的行)代码>)。您可以编写如下内容:
这给出了与我之前的
subset()
调用相同的结果。关键是你需要一系列的单一比较,而不是一系列选项的比较。但正如您所看到的,在这种情况下,%in%
更有用且更简洁。另请注意,我必须使用|
,因为我想将bf11
的每个元素与1
、2
进行比较,和3
,依次。比较:The correct operator is
%in%
here. Here is an example with dummy data:giving:
The subset of
dat
wherebf11
equals any of the set1,2,3
is taken as follows using%in%
:As to why your original didn't work, break it down to see the problem. Look at what
1||2||3
evaluates to:and you'd get the same using
|
instead. As a result, thesubset()
call would only return rows wherebf11
wasTRUE
(or something that evaluated toTRUE
).What you could have written would have been something like:
Which gives the same result as my earlier
subset()
call. The point is that you need a series of single comparisons, not a comparison of a series of options. But as you can see,%in%
is far more useful and less verbose in such circumstances. Notice also that I have to use|
as I want to compare each element ofbf11
against1
,2
, and3
, in turn. Compare:对于您的示例,我相信以下内容应该有效:
请参阅
?subset
中的示例了解更多信息。只是为了演示,一个更复杂的逻辑子集将是:正如 Chase 指出的那样,
%in%
在您的示例中会更有效:正如 Chase 还指出的那样,请确保您了解 < 之间的区别代码>|和<代码>||。要查看运算符的帮助页面,请使用
?'||'
,其中运算符被引用。For your example, I believe the following should work:
See the examples in
?subset
for more. Just to demonstrate, a more complicated logical subset would be:And as Chase points out,
%in%
would be more efficient in your example:As Chase also points out, make sure you understand the difference between
|
and||
. To see help pages for operators, use?'||'
, where the operator is quoted.