Suppose that I want to convert factor to number. I write the following code:

DWQA QuestionsCategory: R LanguageSuppose that I want to convert factor to number. I write the following code:
Editor Staff asked 11 months ago

Suppose that I want to convert factor to number. I write the following code:

1 Answers
Editor Staff answered 11 months ago

fac <- factor(c(2.3, 1.5, 3, 4.9))
as.numeric(fac)
I obtain the output as – 2,1,3,4 which is clearly not what was expected. How should I rectify
this problem?
In the above code, you are converting factor to number which is problematic. R simply floors the
values without any warning. In order to convert factors to numbers properly, we use the integer
vector to perform indexing on the factor levels as follows –
levels(fac)[fac]
Have you checked R factor analysis