Wednesday 18 September 2019

r - How to keep level value unchanged when converting to numerical

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?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...