samedi 29 juillet 2017

if statement on arrays in R

in a simple way of stating my problem, consider I have the following function:

ff<-function(a){ if (a>0){ return ("positive") } else{ return("negative") } } now: ff(-1) [1] "negative" ff(1) [1] "positive" while when i give it an array: print(ff(c(-1,1))) [1] "negative" "negative" Warning message: In if (a > 0) { : the condition has length > 1 and only the first element will be used

I was expecting print(ff(c(-1,1)))=("negative" "positive")

How should I solve this?

Aucun commentaire:

Enregistrer un commentaire