If not conditions in R?
is there something like "if not" in R?
easy Example (doesn't work):
fun <- function(x) { if (!x > 0) {print ("not bigger than zero")} } fun(5) Regards Philippe
The problem is how you define the condition. It should be
if(!(x > 0)){ instead
if(!x > 0){ This is because !x converts an input (numeric) to a logical one - which will give TRUE for all values โโexcept zero. So:
> fun <- function(x){ + if (!(x > 0)) {print ("not bigger than zero")} + } > fun(1) > fun(0) [1] "not bigger than zero" > fun(-1) [1] "not bigger than zero" Try:
if(!condition) { do something } How about this?
fun<-function(x){ ifelse(x>0,"not bigger than zero","zero or less") }
fun(5)
[1] "Bigger than zero"