简体   繁体   English

if else语句串联-R

[英]if else statement concatenation - R

This is a very common question: 1 , 2 , 3 , 4 , 5 , and still I cannot find even an answer to my problem. 这是一个很常见的问题: 12345 ,和我还是无法找到,甚至回答我的问题。

If a == 1, then do X.
If a == 0, then do Y.
If a == 0 and b == 1, then do Z.

Just to explain: the if else statements has to do Y if a==0 no matter the value of b . 只是为了解释:如果a==0则if else语句必须做Y ,而不管b的值如何。 But if b == 1 and a == 0 , Z will do additional changes to those already done by Y . 但是,如果b == 1a == 0 ,则Z将对Y所做的其他更改进行其他更改。

My current code and its error: 我当前的代码及其错误:

if (a == 1){
X
} else if(a == 0){
Y
} else if (a == 0 & b == 1){ 
Z}

Error in !criterion : invalid argument type

An else only happens if a previous if hasn't happened . 一个else ,如果以前只发生if 没有发生

When you say 当你说

But if b == 1 and a == 0 , Z will do additional changes to those already done by Y 但是,如果b == 1a == 0 ,则Z将对Y所做的其他更改

Then you have two options: 然后,您有两个选择:

## Option 1: nest Z inside Y
if (a == 1){
  X
} else if(a == 0){
  Y
  if (b == 1){ 
    Z
  }
}


## Option 2: just use `if` again (not `else if`):
if (a == 1) {
  X
} else if(a == 0) {
  Y
}

if (a == 0 & b == 1) {  
  Z
}

Really, you don't need any else here at all. 真的,您根本不需要else任何else

## This will work just as well 
## (assuming that `X` can't change the value of a from 1 to 0
if (a == 1) {
  X
}

if (a == 0) {
  Y
  if (b == 1){ 
    Z
  }
}

Typically else is needed when you want to have a "final" action that is done only if none of the previous if options were used, for example: 通常, else当你想有一个只能如果没有以前的做了“最终”的行动需要if使用选项,例如:

# try to guess my number between 1 and 10
if (your_guess == 8) {
  print("Congratulations, you guessed my number!")
} else if (your_guess == 7 | your_guess = 9) {
  print("Close, but not quite") 
} else {
  print("Wrong. Not even close!")
}

In the above, else is useful because I don't want to have enumerate all the other possible guesses (or even bad inputs) that a user might enter. 在上面, else很有用,因为我不想枚举用户可能输入的所有其他可能的猜测(甚至错误的输入)。 If they guess 8, they win. 如果他们猜到8,他们就赢了。 If they guess 7 or 9, I tell them they were close. 如果他们猜到7或9,我告诉他们他们很接近。 Anything else, no matter what it is, I just say "wrong". 还有什么,不管是什么,我都说“错”。

Note: this is true for programming languages in general. 注意:通常对于编程语言都是如此。 It is not unique to R. 它不是R独有的。

However, since this is in the R tag, I should mention that R has if{}else{} and ifelse() , and they are different. 但是,由于它在R标签中,因此我应该提到R具有if{}else{}ifelse() ,它们是不同的。

  • if{} (and optionally else{} ) evaluates a single condition, and you can run code to do anything in {} depending on that condition. if{} (和可选的else{} )将评估一个条件,并且您可以根据该条件运行代码以在{}执行任何操作
  • ifelse() is a vectorized function, it's arguments are test , yes , no . ifelse()是向量化函数,其参数为testyesno The test evaluates to a boolean vector of TRUE and FALSE values. test评估为TRUE和FALSE值的布尔向量。 The yes and no arguments must be vectors of the same length as test . yesno参数必须是与test长度相同的向量 The result will be a vector of the same length as test , with the corresponding values of yes (when test is TRUE) and no (when test is FALSE). 结果将是一个长度与test相同的向量,其对应值为yes (当test为TRUE时)和no (当test为FALSE时)。

I believe you want to include Z in the second condition like this: 我相信您想在第二个条件中包括Z,如下所示:

if (a == 1){X}
else if(a == 0){
    Y
    if (b == 1){Z}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM