1

I would like to define a factor where some levels, despite being unique, share the same label. This is simply for output purposes. I understand that duplicate levels are not supported, but instead I am receiving a warning when providing duplicate labels for a unique set of levels.

To reproduce this warning, the following code:

var1 <- factor(c("a", "c", "b", "c"), levels = c("a", "b", "c"), labels = c("X", "X", "Y"))

produces the following warning:

Warning message:
In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated

Though printing var1 gives the desired and expected output, despite the 'levels' attribute now being the specified labels instead. Perhaps duplicate levels in this warning is referring to the duplicate labels of those levels instead?:

> print(var1)
[1] X Y X Y
Levels: X X Y

Should duplicate labels be possible, and if so, how should this be achieved without receiving a warning? Or could this be a possible bug or misunderstood warning message in R (perhaps an issue with terminology describing levels and their labels) as the warning is about the levels rather than labels, despite the original levels being unique in contradiction to the warning? Is there an alternative implementation of factors in R that supports duplicate labels for a unique set of factor levels?

johannux
  • 186
  • 10
  • 2
    See http://stackoverflow.com/a/7128515/6455166, especially the final section. The upshot is that "levels" and "labels" are not two separate attributes of the factor object -- there is only a "levels" attribute, despite what the arguments to `factor(...)` may suggest. – Weihuang Wong Jan 26 '17 at 03:39

0 Answers0