正如问题所问,R中是否存在一个类似于C的三元算子的控制序列?如果有,你如何使用它?谢谢!


当前回答

你的链接指向一个if语句。

> x <- 1
> if(x < 2) print("Less than") else print("Greater than")
[1] "Less than"

如果你的输入变量是一个向量,那么ifelse可能更适合:

> x <- 1:3
> ifelse(x<=2, "Less than or equal", "Greater than")
[1] "Less than or equal" "Less than or equal" "Greater than"   

要访问if的帮助页面,你需要在反引号中嵌入if:

?`if`

ifelse的帮助页面在:

`?ifelse`

其他回答

它并不明确存在,但你可以这样做:

set.seed(21)
y <- 1:10
z <- rnorm(10)

condition1 <- TRUE
x1 <- if(condition1) y else z

or

condition2 <- sample(c(TRUE,FALSE),10,TRUE)
x2 <- ifelse(condition2, y, z)

两者之间的区别在于,condition1必须是长度为1的逻辑向量,而condition2必须是与x、y和z长度相同的逻辑向量。第一个将返回y或z(整个对象),而第二个将返回y (condition2==TRUE)或z (condition2==FALSE)的对应元素。

还要注意,ifelse将比if / else if条件慢,y和z都是长度为1的向量。

我会看一下ifelse命令。我认为它更好,因为它也是向量化的。一个使用cars数据集的例子:

> cars$speed > 20
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
[49]  TRUE  TRUE

> ifelse(cars$speed > 20, 'fast', 'slow')
 [1] "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow"
[11] "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow"
[21] "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow"
[31] "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow" "slow"
[41] "slow" "slow" "slow" "fast" "fast" "fast" "fast" "fast" "fast" "fast"

只是一个恶作剧,你可以重新定义?操作符(几乎)工作于三元操作符(这是一个坏主意):

`?` <- function(x, y) { y <-substitute(y); if(x) eval(y[[2]], parent.frame()) else eval(y[[3]], parent.frame()) }

x <- 1:3
length(x) ? (x*2) : 0
x <- numeric(0)
length(x) ? (x*2) : 0

for(i in 1:5) cat(i, (i %% 2) ? "Odd\n" : "Even\n")

... 但是你需要把表达式放在括号里因为默认的优先级和C语言不同。

只要记得在游戏结束后恢复旧的帮助功能:

rm(`?`)

If的工作方式类似于未向量化的ifelse,如果按以下方式使用:

`if`(condition, doIfTrue, doIfFalse)

使用这个优于ifelse的优势是当向量化是在路上(即我有标量布尔和列表/向量的东西作为结果)

ifelse(TRUE, c(1,2), c(3,4))
[1] 1
`if`(TRUE, c(1,2), c(3,4))
[1] 1 2

你的链接指向一个if语句。

> x <- 1
> if(x < 2) print("Less than") else print("Greater than")
[1] "Less than"

如果你的输入变量是一个向量,那么ifelse可能更适合:

> x <- 1:3
> ifelse(x<=2, "Less than or equal", "Greater than")
[1] "Less than or equal" "Less than or equal" "Greater than"   

要访问if的帮助页面,你需要在反引号中嵌入if:

?`if`

ifelse的帮助页面在:

`?ifelse`