0

what is error with the following code?

 odd_even <-function(x)
 {
  {if (x %% 2 == 0) y <- TRUE
   else  y<-FALSE
 }
 y
 }
 odd_even(c((-5:5),2,-2))

it shows following answer

*[1] FALSE

Warning message:

In if (x%%2 == 0) y <- TRUE else y <- FALSE : the condition has length > 1 and only the first element will be used*

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
akansha
  • 11
  • 1
  • 1
    `if` wants a vector of length 1. You are feeding it a vector of length 13 (if I'm counting right). That is what the warning *the condition has length > 1 and only the first element will be used* means. – lmo Mar 15 '17 at 11:36
  • 4
    Use this: `odd_even <- function(x) x %% 2 == 0`. – Roland Mar 15 '17 at 11:38
  • 1
    `if ()` can evaluate only one element. `ifelse()` can work on vectors. – jogo Mar 15 '17 at 11:53

0 Answers0