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))
str(a)
Incorrect Way
a1 = as.numeric(a)as. numeric() returns a vector of the levels of your factor and not the original values.
str(a1)
Correct Way
a2 = as.numeric(as.character(a))
str(a2)
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.
ReplyDelete