6

Can someone explain to me why the following happens?

ifelse(TRUE, numeric(0), 1)
> [1] NA

I would expect numeric(0) of course. I suspect that this is because ifelse is vectorized, e.g. the following works, but I don't understand exactly what is going on.

if (TRUE) numeric(0) else 1
#> numeric(0)
6
  • 1
    Looks like you can't have a vector of numeric(0), see: c(numeric(0), numeric(0)). I suppose it tries to vectorize it in a way that is possible.
    – cory
    Commented Apr 16, 2020 at 11:10
  • 4
    ifelse() returns a vector of the same length as the test so it can't return a zero length vector.
    – lroha
    Commented Apr 16, 2020 at 11:13
  • @H Isn't the length of the test in the example 1, not zero? Since length(TRUE) is 1.
    – user11538509
    Commented Apr 16, 2020 at 11:17
  • @machine yes but length of numeric(0) is 0 so I guess it does something like rep_len(numeric(0), 1)
    – Sebastian
    Commented Apr 16, 2020 at 11:18
  • 1
    @Sebastian I am just pointing out that the problem is not the length of the test, as suggested by H 1, but the length of numeric(0).
    – user11538509
    Commented Apr 16, 2020 at 11:20

2 Answers 2

4

you can access the implementation of ifelse, which is

function (test, yes, no) 
{
    if (is.atomic(test)) {
        if (typeof(test) != "logical") 
            storage.mode(test) <- "logical"
        if (length(test) == 1 && is.null(attributes(test))) {
           #... let's skip this part..
        }
    }
    else test <- if (isS4(test)) 
        methods::as(test, "logical")
    else as.logical(test)
    ans <- test
    len <- length(ans)
    ypos <- which(test)
    npos <- which(!test)
    if (length(ypos) > 0L) 
        ans[ypos] <- rep(yes, length.out = len)[ypos]
    if (length(npos) > 0L) 
        ans[npos] <- rep(no, length.out = len)[npos]
    ans
}
<bytecode: 0x00000123e6b7d3a0>
<environment: namespace:base>

So, yes, it is because ifelse is vectorized - specifically along the condition - and the return object ans is initialized as a vector of the same length as the condition.

The description of 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.

Let test <- TRUE. The interesting lines are

ypos <- which(test)
rep(numeric(0), length.out = 1)[ypos]
1
  • 1
    Why is the length.out argument set to 0 in your example? If we use test <- TRUE, then inside ifelse() we have ans <- test; len <- length(ans); rep(yes, length.out = len). Shouldn't be the argument set to 1?
    – user11538509
    Commented Apr 16, 2020 at 12:00
1

If you want to adjust the function so that it returns numeric(0) in your situation you can change the if(length(yes) == 1) to if (length(yes) == 0 | length(yes) == 1) inside the function. This gives you:

ifelse2 <- function (test, yes, no) {
  if (is.atomic(test)) {
    if (typeof(test) != "logical") 
      storage.mode(test) <- "logical"
    if (length(test) == 1 && is.null(attributes(test))) {
      if (is.na(test)) 
        return(NA)
      else if (test) {
    if (length(yes) == 0 | length(yes) == 1) { # Here is what I changed
          yat <- attributes(yes)
          if (is.null(yat) || (is.function(yes) && identical(names(yat), 
                                                         "srcref"))) 
            return(yes)
        }
  }
      else if (length(no) == 1) {
        nat <- attributes(no)
        if (is.null(nat) || (is.function(no) && identical(names(nat), 
                                                          "srcref"))) 
          return(no)
      }
    }
  }
  else test <- if (isS4(test)) 
    methods::as(test, "logical")
  else as.logical(test)
  ans <- test
  len <- length(ans)
  ypos <- which(test)
  npos <- which(!test)
  if (length(ypos) > 0L) 
    ans[ypos] <- rep(yes, length.out = len)[ypos]
  if (length(npos) > 0L) 
    ans[npos] <- rep(no, length.out = len)[npos]
  ans
}

Trying it:

ifelse2(TRUE, numeric(0), 1)
> [1] numeric(0)

You can do the same with the no argument if it can be numeric(0) in your case, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.