Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6361379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:46:12+00:00 2026-05-24T23:46:12+00:00

An example of a non-settable function would be labels . You can only set

  • 0

An example of a non-settable function would be labels. You can only set factor labels when they are created with the factor() function. There is no labels<- function. Not that ‘labels’ and ‘levels’ in factors make any sense….

>  fac <- factor(1:3, labels=c("one", "two", "three"))
> fac
[1] one   two   three
Levels: one two three
> labels(fac)
[1] "1" "2" "3"

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?

> levels(fac)
[1] "one"   "two"   "three"

So it appears that setting labels is really setting levels.

>  fac <- factor(1:3, levels=c("one", "two", "three"))
> levels(fac)
[1] "one"   "two"   "three"

OK that is as expected. So what are labels when one sets levels?

>  fac <- factor(1:3, levels=c("one", "two", "three"), labels=c("x","y", "z") )
> labels(fac)
[1] "1" "2" "3"
> levels(fac)
[1] "x" "y" "z"

It would seem that ‘labels’ arguments for factor() trump any ‘levels’ arguments for the specification of levels. Why should this be? And why does labels() return what I would have imagined to be retrieved with as.character(as.numeric(fac))?

(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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:46:12+00:00Added an answer on May 24, 2026 at 11:46 pm

    I think the way to think about the difference between labels and levels (ignoring the labels() function that Tommy describes in his answer) is that levels is intended to tell R which values to look for in the input (x) and what order to use in the levels of the resulting factor object, and labels is to change the values of the levels after the input has been coded as a factor … as suggested by Tommy’s answer, there is no part of the factor object returned by factor() that is called labels … just the levels, which have been adjusted by the labels argument … (clear as mud).

    For example:

    > f <- factor(x=c("a","b","c"),levels=c("c","d","e"))
    > f
    [1] <NA> <NA> c  
    Levels: c d e
    > str(f)
    Factor w/ 3 levels "c","d","e": NA NA 1
    

    Because the first two elements of x were not found in levels, the first two elements of f are NA. Because "d" and "e" were included in levels, they show up in the levels of f even though they did not occur in x.

    Now with labels:

    > f <- factor(c("a","b","c"),levels=c("c","d","e"),labels=c("C","D","E"))
    > f
    [1] <NA> <NA> C   
    Levels: C D E
    

    After R figures out what should be in the factor, it re-codes the levels. One can of course use this to do brain-frying things such as:

    > f <- factor(c("a","b","c"),levels=c("c","d","e"),labels=c("a","b","c"))
    > f
    [1] <NA> <NA> a   
    Levels: a b c
    

    Another way to think about levels is that factor(x,levels=L1,labels=L2) is equivalent to

    f <- factor(x,levels=L1)
    levels(f) <- L2
    

    I think an appropriately phrased version of this example might be nice for Pat Burns’s R inferno — there are plenty of factor puzzles in section 8.2, but not this particular one …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone give me an example of successful non-programmer, 5GL (not that I am
Is there a non-trivial example application written in Factor language, pregerrably for GUI application
Assume that I want to listen to a non-common port (9090 for example) 24/7
How can I find the largest increasing (non-contiguous) subset of an array? For example,
I need to call a const function from a non-const object. See example struct
Can I have an identity (unique, non-repeating) column span multiple tables? For example, let's
Are there algorithms to solve non-trivial algebra problems, for example to simplify the following
I've read the RebaseProject page and tried a non-trivial example (not rebasing a complete
I've created a super simple example application using Macruby. The problem is that the
How can I handle a non-ascii url in App Engine? For example http://example.org /Ñöñ-ÅŞÇİİ.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.