mardi 7 juillet 2020

ifelse not returning the revised vector [closed]

I have a vector

my.vec <- c(1, 2, 3, 4, 5)
x <- 7

I want to check if my.vec contains x. If not, add x to my.vec.

if(!x %in% my.vec){
   my.vec <- c(my.vec, x)
} else {
  my.vec <- my.vec
}

my.vec

I want to do this using ifelse as follows

ifelse(!x %in% my.vec, c(my.vec, x), my.vec)

But it only returns 1

Aucun commentaire:

Enregistrer un commentaire