R中是否存在三元算子?

R中是否存在三元算子?,r,operators,R,Operators,正如问题所问,R中是否有类似于C的控制序列?如果是,你如何使用它?谢谢 我想看看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 FALS

正如问题所问,R中是否有类似于C的控制序列?如果是,你如何使用它?谢谢

我想看看
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"

您的链接指向一个
if
语句

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

要访问
if
的帮助页面,您需要将
if
嵌入背景标记中:

?`if`
ifelse
的帮助页面位于:

`?ifelse`

As
if
R
中的函数,如果else与
?:
等效,则返回最新的计算结果

> a <- 1
> x <- if(a==1) 1 else 2
> x
[1] 1
> x <- if(a==2) 1 else 2
> x
[1] 2

开玩笑吧,你可以定义c风格的
?:

`?` <- function(x, y)
    eval(
      sapply(
        strsplit(
          deparse(substitute(y)), 
          ":"
      ), 
      function(e) parse(text = e)
    )[[2 - as.logical(x)]])
但你需要括号来分配:(

>y
[1] 1
>y y
[1] 6

最后,您可以使用c执行非常类似的操作:

`?` <- function(x, y) {
  xs <- as.list(substitute(x))
  if (xs[[1]] == as.name("<-")) x <- eval(xs[[3]])
  r <- eval(sapply(strsplit(deparse(substitute(y)), ":"), function(e) parse(text = e))[[2 - as.logical(x)]])
  if (xs[[1]] == as.name("<-")) {
    xs[[3]] <- r
        eval.parent(as.call(xs))
  } else {
    r
  }
}       

`?`它并不显式存在,但您可以执行以下操作:

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

condition1 <- TRUE
x1 <- if(condition1) y else z
set.seed(21)

y像其他人说的那样,使用
ifelse
,但是您可以定义运算符,这样您几乎可以使用三元运算符语法

`%?%` <- function(x, y) list(x = x, y = y)
`%:%` <- function(xy, z) if(xy$x) xy$y else z

TRUE %?% rnorm(5) %:% month.abb
## [1]  0.05363141 -0.42434567 -0.20000319  1.31049766 -0.31761248
FALSE %?% rnorm(5) %:% month.abb
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
# or, more generally
condition %?% value1 %:% value2
(这是因为
的优先级低于

不幸的是,这会破坏现有的帮助运算符和顺序运算符。

就像恶作剧一样,您可以重新定义
运算符,使其(几乎)像三元运算符一样工作(这是个坏主意):


if
如果以以下方式使用,则其工作原理与未反光的ifelse类似:

`if`(condition, doIfTrue, doIfFalse)
与ifelse相比,使用它的优势在于当矢量化受到阻碍时(即,结果是标量布尔值和列表/向量)


我已经编写了一个小的语言扩展,它模拟了R中C的条件三元运算符。它可以作为一个包安装在

该实现基于,但我做了一些更改,以便在if_true和if_false参数包含冒号的情况下,它是健壮的,允许链接条件语句并保留
运算符的基本功能


我会参考其他人关于重新定义运算符的危险性的警告,但这是一个很好的例子,说明了语言R的动态性!

你想要比
ifelse
更强大的东西,还是更紧凑的形式?@CarlWitthoft大多数情况下更紧凑的形式;只是一种节省编写
if(x>1)的方法y=2其他y=3
。写
y=
一次对它有一定的吸引力。正如@kohske所说,这也会起作用:
print(如果(xHi Paul——你是不是想用你的例子展示一下
ifelse
)谢谢Joshua,你的回答帮助很大,我从你提到的帖子中找到了答案
`?` <- function(x, y) {
  xs <- as.list(substitute(x))
  if (xs[[1]] == as.name("<-")) x <- eval(xs[[3]])
  r <- eval(sapply(strsplit(deparse(substitute(y)), ":"), function(e) parse(text = e))[[2 - as.logical(x)]])
  if (xs[[1]] == as.name("<-")) {
    xs[[3]] <- r
        eval.parent(as.call(xs))
  } else {
    r
  }
}       
> y <- 1 ? 2*3 : 4
> y
[1] 6
> y <- 0 ? 2*3 : 4
> y
[1] 4
> 1 ? 2*3 : 4
[1] 6
> 0 ? 2*3 : 4
[1] 4
set.seed(21)
y <- 1:10
z <- rnorm(10)

condition1 <- TRUE
x1 <- if(condition1) y else z
condition2 <- sample(c(TRUE,FALSE),10,TRUE)
x2 <- ifelse(condition2, y, z)
`%?%` <- function(x, y) list(x = x, y = y)
`%:%` <- function(xy, z) if(xy$x) xy$y else z

TRUE %?% rnorm(5) %:% month.abb
## [1]  0.05363141 -0.42434567 -0.20000319  1.31049766 -0.31761248
FALSE %?% rnorm(5) %:% month.abb
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
# or, more generally
condition %?% value1 %:% value2
`?` <- function(x, y) if(x) y[[1]] else y[[2]]
`:` <- function(y, z) list(y, z)

TRUE ? rnorm(5) : month.abb
## [1]  1.4584104143  0.0007500051 -0.7629123322  0.2433415442  0.0052823403
FALSE ? rnorm(5) : month.abb
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
`?` <- 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")
rm(`?`)
`if`(condition, doIfTrue, doIfFalse)
ifelse(TRUE, c(1,2), c(3,4))
[1] 1
`if`(TRUE, c(1,2), c(3,4))
[1] 1 2