samedi 29 août 2015

if else function returns not what I asked for

I have the following function

aa<-function(x){
    if (x==c(3,4,5))
        return (1)
    if (x==c(6,7,8))
        return(2)
    else
        return(0)
}

I then try the following:

> `aa(3)`
[1] 1
> `aa(4`)
[1] 0
> `aa(5)`
[1] 0
> `aa(6)`
[1] 2
> `aa(7)`
[1] 0
> `aa(8)`
[1] 0

I don't know why only aa(3) and aa(6) gives me the desired outcome, while aa(4) and aa(5) won't return 1 and aa(7) and aa(8) won't return 2. How can I correct my code so that a value of either 3, 4, or 5 returns 1, and 6, 7, or 8 returns 2, and 0 otherwise?

Aucun commentaire:

Enregistrer un commentaire