samedi 16 février 2019

R ifelse return value of another vector via index

I cant get my head around this problem regarding ifelse:

say I have two vectors: x<-c(0,1:4,1:4) y<- letters[1:3]

when I do ifelse(x==2,y[x],x) I get "0" "1" "c" "3" "4" "1" "c" "3" "4"

however it should return "b" as position 2 of vector y. why is ifelse doing that?

Aucun commentaire:

Enregistrer un commentaire