lundi 1 juin 2020

ifelse not returning a vector in BASE R

Maybe this is too basic of a question. But in my function foo below, I expected that because of using ifelse when I input a vector for the input values, I get a vector output.

But I wonder why in the example below instead of getting > [1] 1 6 in the output, I just get > [1] 1 as the output?

foo <- function(mpre = NA, mpos = NA, mdif = NA){
ifelse(!is.na(mdif), mdif, ifelse(!is.na(mpre) & !is.na(mpre) & is.na(mdif), mpos - mpre, NA))
}
## EXAMPLE:

foo(c(1,3), c(2,9))

Aucun commentaire:

Enregistrer un commentaire