R : Converting a factor to integer

Most of R Programmers make mistake while converting a factor variable to integer.

Let's create a factor variable
a <- factor(c(2, 4, 3, 3, 4))
Incorrect Way
a1 = as.numeric(a)
as. numeric() returns a vector of the levels of your factor and not the original values.

Correct Way
a2 = as.numeric(as.character(a))

R Tutorials : 75 Free R Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like banking, Telecom, HR and Health Insurance.

While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*
Related Posts:
1 Response to "R : Converting a factor to integer"
  1. Could you explain more about why you need to do this? Sort of a basic question, but would be helpful for us less experienced R users.


Next → ← Prev