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?

1
  • 1
    Convert to character first then to numeric ... as.numeric(as.character(x)) Commented Apr 6, 2019 at 19:36

1 Answer 1

0

I think I resolved the problem by using:

   teenSf2_2[] <- lapply(teenSf2_2, function(x) as.numeric(as.character(x)))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.