153

I've found R's ifelse statements to be pretty handy from time to time. For example:

ifelse(TRUE,1,2)
# [1] 1
ifelse(FALSE,1,2)
# [1] 2

But I'm somewhat confused by the following behavior.

ifelse(TRUE,c(1,2),c(3,4))
# [1] 1
ifelse(FALSE,c(1,2),c(3,4))
# [1] 3

Is this a design choice that's above my paygrade?

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Christopher DuBois
  • 42,350
  • 23
  • 71
  • 93

9 Answers9

124

The documentation for ifelse states:

ifelse returns a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is TRUE or FALSE.

Since you are passing test values of length 1, you are getting results of length 1. If you pass longer test vectors, you will get longer results:

> ifelse(c(TRUE, FALSE), c(1, 2), c(3, 4))
[1] 1 4

So ifelse is intended for the specific purpose of testing a vector of booleans and returning a vector of the same length, filled with elements taken from the (vector) yes and no arguments.

It is a common confusion, because of the function's name, to use this when really you want just a normal if () {} else {} construction instead.

joran
  • 169,992
  • 32
  • 429
  • 468
Nathan Kitchen
  • 4,799
  • 1
  • 24
  • 19
78

I bet you want a simple if statement instead of ifelse - in R, if isn't just a control-flow structure, it can return a value:

> if(TRUE) c(1,2) else c(3,4)
[1] 1 2
> if(FALSE) c(1,2) else c(3,4)
[1] 3 4
Ken Williams
  • 22,756
  • 10
  • 85
  • 147
  • @Ken, this works for me, even though I get what I need a constant warning `" Warning in if (req(inputval) == "All") { : the condition has length > 1 and only the first element will be used"` what should I do to get rid of this warning ? – user5249203 Jan 05 '18 at 21:23
  • 1
    @user5249203, the question and Ken's answer refer to the case where the condition is a single value, i.e., a vector of length 1. The warning indicates that `req(inputval)` has more elements. To get a single value the functions `any()` or `all()` might be useful. – Uwe Jul 20 '18 at 07:14
17

Note that you can circumvent the problem if you assign the result inside the ifelse:

ifelse(TRUE, a <- c(1,2), a <- c(3,4))
a
# [1] 1 2

ifelse(FALSE, a <- c(1,2), a <- c(3,4))
a
# [1] 3 4
Cath
  • 23,906
  • 5
  • 52
  • 86
  • 4
    IMHO, this is encouraging to misuse the vectorized `ifelse()` function in place of a control flow `if ... else ...` for assignment. If the condition is a single `TRUE` or `FALSE` value, I would prefer to write `a <- if (TRUE) c(1,2) else c(3,4)` or `if (TRUE) a <- c(1,2) else a <- c(3,4)` – Uwe Jul 20 '18 at 06:51
  • 2
    @Uwe though I don't think the difference in performance when using `ifelse` instead of `if`...`else` in case of a single condition can really be a problem and `ifelse` may be preferred in some cases inside code (simple guess here), I cannot disagree with you ;-). I just wanted to show a way with `ifelse`. – Cath Jul 20 '18 at 06:59
  • You could also slightly abuse `ifelse` and `list`s - `ifelse(TRUE, list(c(1,2)), list(c(3,4)) )[[1]]` – thelatemail May 25 '21 at 22:21
15

use `if`, e.g.

> `if`(T,1:3,2:4)
[1] 1 2 3
blueskyddd
  • 431
  • 4
  • 12
9

yeah, I think ifelse() is really designed for when you have a big long vector of tests and want to map each to one of two options. For example, I often do colors for plot() in this way:

plot(x,y, col = ifelse(x>2,  'red', 'blue'))

If you had a big long vector of tests but wanted pairs for outputs, you could use sapply() or plyr's llply() or something, perhaps.

Brendan OConnor
  • 9,624
  • 3
  • 27
  • 25
4

Sometimes the user just needs a switch statement instead of an ifelse. In that case:

condition <- TRUE
switch(2-condition, c(1, 2), c(3, 4))
#### [1] 1 2

(which is another syntax option of Ken Williams's answer)

agenis
  • 8,069
  • 5
  • 53
  • 102
4

Here is an approach similar to that suggested by Cath, but it can work with existing pre-assigned vectors

It is based around using the get() like so:

a <- c(1,2)
b <- c(3,4)
get(ifelse(TRUE, "a", "b"))
# [1] 1 2
bmonger
  • 77
  • 6
2

In your case, using if_else from dplyr would have been helpful: if_else is more strict than ifelse, and throws an error for your case:

library(dplyr)
if_else(TRUE,c(1,2),c(3,4))
#> `true` must be length 1 (length of `condition`), not 2
Matifou
  • 7,968
  • 3
  • 47
  • 52
0

Found on everydropr:

ifelse(rep(TRUE, length(c(1,2))), c(1,2),c(3,4))
#>[1] 1 2

Can replicate the result of your condition to return the desired length

SJGD
  • 132
  • 1
  • 2
  • 7