length_levels

NoteAdded in 0.0.8

What it does

Check for length(levels(...)) and replace it with nlevels(...).

Why is this bad?

length(levels(...)) is harder to read nlevels(...).

Internally, nlevels() calls length(levels(...)) so there are no performance gains.

Example

x <- factor(1:3)
length(levels(x))

Use instead:

x <- factor(1:3)
nlevels(x)