Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>labels</code> function sounds like the perfect fit for getting the labels of a factor.</p> <p>...but the <code>labels</code> function has nothing to do with factors! It is used as a generic way of getting something to "label" an object. For atomic vectors, this would be the names. But if there are no names, the <code>labels</code> function returns the element indices coerced to strings - something like <code>as.character(seq_along(x))</code>.</p> <p>...So that's what your seeing when you try labels on a factor. The factor is an integer vector without any names, but with a <code>levels</code> attribute.</p> <p>A factor has no labels. It only has levels. The <code>labels</code> argument to <code>factor</code> is just a way to be able to give a set of strings but produce another set of strings as the levels... But to confuse things further, the <code>dput</code> function prints the <code>levels</code> attributes as <code>.Label</code>! I think that is a legacy thing...</p> <pre><code># Translate lower case letters to upper case. f &lt;- factor(letters[2:4], letters[1:3], LETTERS[1:3]) dput(f) #structure(c(2L, 3L, NA), .Label = c("A", "B", "C"), class = "factor") attributes(f) #$levels #[1] "A" "B" "C" # #$class #[1] "factor" </code></pre> <p>However, since <code>labels</code> is a generic function, it would probably be a good idea to define <code>labels.factor</code> as follows (currently there is none). Perhaps something for R core to consider?</p> <pre><code>labels.factor &lt;- function(x, ...) as.character(x) </code></pre>
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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