r - convert factor to numeric and remove levels

26,527

If you convert directly from factor to numeric, it will return the levels instead of the actual values. Here it doesn't matter, because it looks like the levels and the values are the same. If they are different, you have to convert to character first, and then to numeric.

df$game <- as.numeric(as.character(df$game))
Share:
26,527
dudemcgregor
Author by

dudemcgregor

Updated on July 16, 2022

Comments

  • dudemcgregor
    dudemcgregor almost 2 years

    I have a column in my data frame that is numbers 1, 2, 3, until 31.

    column is titled 'game'

    R tells me this column consists of Factors with 31 levels.

    enter image description here

    How do I convert the factors to numbers without it displaying the levels so I can analyze it numerically?

    I'm using R studio if that help at all.