0

I'm trying to convert all factor values into numerical in a dataset: enter image description here

The code is pretty straight forward, I used two methods, both do not return the same level values as they were in factor format:

    teenSf2_3 <- mutate_if(teenSf2_2, is.factor, as.numeric)

    teenSf2_2[] <- lapply(teenSf2_2, function(x) as.numeric(as.factor(x)))

The result has different level values: enter image description here

It seems there is an increment of 1 for each level. How do I keep the level values unchanged when converting to numerical?

Uwe Keim
  • 38,279
  • 56
  • 171
  • 280
lydias
  • 779
  • 9
  • 27

1 Answers1

0

I think I resolved the problem by using:

   teenSf2_2[] <- lapply(teenSf2_2, function(x) as.numeric(as.character(x)))
lydias
  • 779
  • 9
  • 27