1

This is really confusing behavior:

x <- factor(1:3, levels = 1:4, labels = letters[1:4])
x
# [1] a b c
# Levels: a b c d

labels(x)
# [1] "1" "2" "3"

levels(x)
# [1] "a" "b" "c" "d"

Why does labels() return the values given to levels = (though filtered to what was actually used, for some reason), while levels() returns what was given to labels = (which is also given as Levels: when inspecting x)?

Wasabi
  • 2,879
  • 3
  • 26
  • 48

1 Answers1

2

This is expected behavior as described in the documentation (see Value section):

... For a vector the results is the names or seq_along(x)...

> seq_along(x)
[1] 1 2 3
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197