Wednesday, September 18, 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

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...