Method 1:
such as : x <- factor(c(3,4,5,1)) (x) [1]2 3 4 1
It's best to do this:
(levels(x)[x]) [1] 3 4 5 1
Method 2:
(())
This method is the best!
Method 3:
When reading it, it is read as a numerical type. When reading R, the default "NA" is missing. If your data is not represented by NA, then this column is read as string or factor.
Supplementary: The problem of converting factor into numeric in R language
I have always thought that as long as it is a number, no matter what type it is, it can be converted into a corresponding numeric type number through the() function, for example
x<-"123", x is character type, and (x) is numeric type 123. But the factor type is different.
a<-factor(c(100,200,300,301,302,400,10)), their values are 100 200 300 301 302 400 10, however
(a) The corresponding value is not 100 200 300 301 302 400 10, but 2 3 4 5 6 7 1.
The rules for converting factor into numeric types are as follows:
There are n numbers in total, so the converted number will take the value in 1-n, the smallest number will take one, the smallest number will take two, and so on.
So how do you make the numerical type convert the corresponding numerical type in the factor type?
1. mean(((factorname)))
2. mean((levels(factorname)[factorname]))
All the above codes can implement the corresponding numerical type conversion of the numerical type in the factor type. The idea is to convert it into a character type first and then convert it into a numerical type.