jeudi 21 novembre 2019

R loops with factor creation

I want to check if there are less than 4 uniques in my variable, and it's true, I want to create a factor from it.

I created a fynction:

checker <- function(x) {
  if (is.numeric(x) == TRUE) {

   if (length(unique(x)) < 5) {
       x <- factor(x)
   }

} else {
break
}
return(x)
}

But it still returns numeric vectors.

I suppose, for some reason

x <- factor(x)

doesn't work, when I tried:

checker <- function(x) {
              x <- factor(x)
              return(x)
}

it also returned numerics.

I'm very sad...

I' m very sad.

Aucun commentaire:

Enregistrer un commentaire