- Machine Learning with R Quick Start Guide
- Iván Pastor Sanz
- 145字
- 2021-06-24 16:01:30
Factor levels
Levels are possible values that a variable can take. Suppose the original value of 1 is repeated; it will appear only once in the levels.
Factors can either be numeric or character variables, but levels of a factor can only be characters.
Let's run the level command:
levels(r)
## [1] "1" "4" "7" "8" "9"
As you can see, 1, 4, 7, 8, and 9 are the possible levels that the level r can have.
The exclude parameter allows you to exclude levels of a custom factor:
factor(r, exclude=4)
## [1] 1 <NA> 7 9 8 1
## Levels: 1 7 8 9
Finally, let's find out if our factor values are ordered or unordered:
a<- c(1,2,7,7,1,2,2,7,1,7)
a<- factor(a, levels=c(1,2,7), ordered=TRUE)
a
## [1] 1 2 7 7 1 2 2 7 1 7
## Levels: 1 < 2 < 7