Thursday, 28 September 2017

R factors

Cited from the Book "Beginning Data Sciences in R"

ff <- factor(c("small", "small", "medium",  "large", "small", "large"), levels = c("small", "medium", "large"))

ordered(f, levels = c("small", "medium", "large"))
## [1] small small medium large small large
## Levels: small < medium < large

 

A factor is actually not stored as strings, even though we create it from a vector of strings. It is stored as a vector of integers where the integers are indices into the levels.

The easiest way to deal with a factor as the actual labels it has is to translate it into a vector of strings.

No comments:

Post a Comment