[1] "hello" "hello" "ciao" "ciao" "hola" "hola"
[1] hello hello ciao ciao hola hola
Levels: ciao hello hola
vectors
factors
lists
matrices
arrays
dataframes
Factors are a peculiar data type: although they look like simple characters, they are actually a type of integer vector with some additional properties.
Factors can be created either by converting a character vector with the as.factor() command …
… or by explicitly creating a factor with the factor() command:
[1] hello hello ciao ciao hola hola
Levels: ciao hello hola
… equivalently
In practice, they assign a label to an integer value:
Why these numbers?
Factors store levels() as metadata,
regardless of which ones are actually present in the vector. For example, if I create a factor made only of the elements of my_fact different from ciao:
However, we can drop the levels that are no longer needed with the droplevels() command:
[1] hello hello hola hola
Levels: hello hola
Why reorder? The first level is the reference category: statistical models often compare every group to it.
[1] placebo caffeine nicotine placebo caffeine
Levels: caffeine nicotine placebo
[1] "caffeine" "nicotine" "placebo"
[1] 3 1 2 3 1
Here placebo should be the reference, but the alphabetical order puts it last.
levels in factor(): the levels in the order we want
[1] "placebo" "caffeine" "nicotine"
[1] 1 2 3 1 2
relevel()If we only need to choose the reference category, relevel() moves it to the first place and leaves the others as they are:
levels(x) <- ...The new names are assigned by position: the first name goes to the first level, and so on.
[1] "placebo" "caffeine" "nicotine"
[1] placebo caffeine nicotine placebo caffeine
Levels: placebo caffeine nicotine
[1] PLA CAF NIC PLA CAF
Levels: PLA CAF NIC
We wanted placebo first, so we write the names in the order we want…
[1] placebo caffeine nicotine placebo caffeine
Levels: caffeine nicotine placebo
[1] nicotine placebo caffeine nicotine placebo
Levels: placebo caffeine nicotine
No error, no warning, but every value is now wrong! levels(x) <- renames, it does not reorder: “caffeine” (first level) was renamed “placebo”, and so on.
factor(x, levels, labels)To reorder and rename in one step:
[1] placebo caffeine nicotine placebo caffeine
Levels: caffeine nicotine placebo
[1] PLA CAF NIC PLA CAF
Levels: PLA CAF NIC
levels: the existing values, in the order we wantlabels: their new names, in the same order: matched by name, so the data cannot get mixed up| to… | use | does the data change? |
|---|---|---|
| see the levels | levels(x) |
no |
| reorder the levels | factor(x, levels = c(...)) |
no |
| choose the reference | relevel(x, ref = "...") |
no |
| rename the levels | levels(x) <- c(...) |
no, if you follow the current order |
| reorder + rename | factor(x, levels = c(...), labels = c(...)) |
no |
| remove unused levels | droplevels(x) |
no |
The order of the levels only decides which comes first (reference, tables, plots). It does not mean that one group is “bigger” than another.
A subtype of factors are ordered factors, which correspond to ordinal variables, i.e. variables whose levels are sorted in increasing order:
[1] never sometimes often always
Levels: always < never < often < sometimes
So what should we do?
Open this link and keep it open: