r - convert factor to numeric and remove levels

dudemcgregor picture dudemcgregor · Dec 26, 2015 · Viewed 23.8k times · Source

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.

Answer

ytk picture ytk · Dec 26, 2015

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))