如何测试多个条件? [英] How to test for more than one conditional?

查看:158
本文介绍了如何测试多个条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个变量y和一个变量i.

Suppose I have a variable y and a variable i.

y<- c(TRUE, TRUE, TRUE)
i<- 0

假设我想针对>中的每个 布尔条件测试以下if语句:

Let's say I would like to test the following if-statement for every boolean condition in y:

if (y) {
i<-1
}

我该怎么做?也就是说,如果y中的每个布尔条件是TRUE,我想要i = 1.

How would I do this? That is, I want i = 1 if every boolean condition in y is TRUE.

如果y<- c(TRUE, FALSE,TRUE),那么我希望if语句的评估结果为FALSEi=0.有谁知道我该怎么做?目前,我收到以下警告消息:

If y<- c(TRUE, FALSE,TRUE), then I want the if-statement to evaluate to FALSE and i=0. Does anyone know how would I do this? Currently I getting this warning message:

Warning message:
In if (y) { :
  the condition has length > 1 and only the first element will be used.

如何为每个布尔条件测试变量y?

How would I test the variable y for each of it's boolean conditions?

推荐答案

您正在寻找all函数.

> y <- c(T, T, T)
> all(y)
[1] TRUE
> y <- c(T, T, F)
> all(y)
[1] FALSE

这篇关于如何测试多个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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