Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is the terminology of labels and levels in factors so weird?
    primarykey
    data
    text
    <p>An example of a non-settable function would be <code>labels</code>. You can only set factor labels when they are created with the factor function. There is no <code>labels&lt;-</code> function. Not that 'labels' and 'levels' in factors make any sense....</p> <pre><code>&gt; fac &lt;- factor(1:3, labels=c("one", "two", "three")) &gt; fac [1] one two three Levels: one two three &gt; labels(fac) [1] "1" "2" "3" </code></pre> <p>OK, I asked for labels, which one might assume were as set by the factor call, but I get something quite ... what's the word, unintuitive? </p> <pre><code>&gt; levels(fac) [1] "one" "two" "three" </code></pre> <p>So it appears that setting labels is really setting levels.</p> <pre><code>&gt; fac &lt;- factor(1:3, levels=c("one", "two", "three")) &gt; levels(fac) [1] "one" "two" "three" </code></pre> <p>OK that is as expected. So what are labels when one sets levels?</p> <pre><code>&gt; fac &lt;- factor(1:3, levels=c("one", "two", "three"), labels=c("x","y", "z") ) &gt; labels(fac) [1] "1" "2" "3" &gt; levels(fac) [1] "x" "y" "z" </code></pre> <p>Effing weird, if you ask me. It would seem that 'labels' arguments for factor trump any 'levels' arguments for the specification of levels. Why should this be? Seems like a confused terminology. And why does <code>labels()</code> return what I would have imagined to be retrieved with as.character(as.numeric(fac))?</p> <p>(This was a tangential comment [labelled as such] in an earlier answer about assignment functions to which I was asked to move to a question. So here's your opportunity to enlighten me.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload