繁体   English   中英

否则if(){}与ifelse()

[英]else if(){} VS ifelse()

为什么我们可以使用ifelse()而不是else if(){}with()within()声明?

我听说第一个是矢量化的,而不是后者。 这是什么意思 ?

if构造仅在将向量传递给它时才考虑第一个组件(并给出警告)

if(sample(100,10)>50) 
    print("first component greater 50") 
else 
    print("first component less/equal 50")

ifelse函数对每个组件执行检查并返回一个向量

ifelse(sample(100,10)>50, "greater 50", "less/equal 50")

例如, ifelse函数对于transform很有用。 使用&|通常很有用| ifelse情况下和&&|| if

第二部分的答案:

* 使用if当x具有1长度但y分别为大于1 *

 x <- 4
 y <- c(8, 10, 12, 3, 17)
if (x < y) x else y

[1]  8 10 12  3 17
Warning message:
In if (x < y) x else y :
  the condition has length > 1 and only the first element will be used

当x的长度为1但y的长度大于1时使用ifelse

ifelse (x < y,x,y)
[1] 4 4 4 3 4

暂无
暂无

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

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