使用逻辑 OR 组合列表中的逻辑向量 [英] Combine logical vectors in list using logical OR

查看:18
本文介绍了使用逻辑 OR 组合列表中的逻辑向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用与逻辑OR (|) 的元素比较来有效地组合列表中的逻辑向量.结果应该是与输入向量长度相同的逻辑向量.如果任意个输入值为TRUE,则结果为TRUE,否则结果为FALSE.

How do I effectively combine logical vectors across the list using elementwise comparisons with logical OR (|). The result should be a logical vector of same length as the input vectors. If any of the input values is TRUE, the result is TRUE, else the result is FALSE.

我有一个 opts 列表,其中包含一组相同长度的逻辑向量.

I have a list opts with a set of logical vectors of the same length.

> str(opts)
List of 5
 $ option1: logi [1:608247] FALSE FALSE FALSE FALSE FALSE FALSE ...
 $ option2: logi [1:608247] FALSE TRUE  FALSE TRUE  TRUE  TRUE  ...
 $ option3: logi [1:608247] FALSE TRUE  FALSE FALSE TRUE  FALSE ...
 $ option4: logi [1:608247] FALSE FALSE FALSE FALSE FALSE FALSE ...

我想要这样的结果:

logi [1:608247] FALSE TRUE FALSE TRUE TRUE TRUE ...

因此,结果的第一个值 FALSE 是因为 no TRUE 在所有向量的第一个位置列表.结果的第二个值 TRUE 是因为它们是两个(至少一个any)TRUE在向量的第二个位置.

Thus, the first value of the result, FALSE, is because the are no TRUE in the first position across all vectors of the list. The second value of the result, TRUE, is because the are two (at least one, any) TRUE in the second position of the vectors.

我可以将我的数据结构更改为 matrixdata.frame 或其他更好的东西,我只是从 lapply.

I am fine with changing my datastructure to be a matrix or data.frame or something else if it is better I just get this from a lapply.

推荐答案

Reduce怎么样:

Reduce("&", opts)
Reduce("|", opts)

这篇关于使用逻辑 OR 组合列表中的逻辑向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆