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 4573970
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:51:32+00:00 2026-05-21T19:51:32+00:00

There seems to be a difference between levels and labels of a factor in

  • 0

There seems to be a difference between levels and labels of a factor in R.
Up to now, I always thought that levels were the ‘real’ name of factor levels, and labels were the names used for output (such as tables and plots). Obviously, this is not the case, as the following example shows:

df <- data.frame(v=c(1,2,3),f=c('a','b','c'))
str(df)
'data.frame':   3 obs. of  2 variables:
 $ v: num  1 2 3
 $ f: Factor w/ 3 levels "a","b","c": 1 2 3

df$f <- factor(df$f, levels=c('a','b','c'),
  labels=c('Treatment A: XYZ','Treatment B: YZX','Treatment C: ZYX'))
levels(df$f)
[1] "Treatment A: XYZ" "Treatment B: YZX" "Treatment C: ZYX"

I thought that the levels (‘a’,’b’,’c’) could somehow still be accessed when scripting, but this doesn’t work:

> df$f=='a'
[1] FALSE FALSE FALSE

But this does:

> df$f=='Treatment A: XYZ' 
[1]  TRUE FALSE FALSE

So, my question consists of two parts:

  • What’s the difference between levels and labels?

  • Is it possible to have different names for factor levels for scripting and output?

Background: For longer scripts, scripting with short factor levels seems to be much easier. However, for reports and plots, this short factor levels may not be adequate and should be replaced with preciser names.

  • 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-21T19:51:33+00:00Added an answer on May 21, 2026 at 7:51 pm

    Very short : levels are the input, labels are the output in the factor() function. A factor has only a level attribute, which is set by the labels argument in the factor() function. This is different from the concept of labels in statistical packages like SPSS, and can be confusing in the beginning.

    What you do in this line of code

    df$f <- factor(df$f, levels=c('a','b','c'),
      labels=c('Treatment A: XYZ','Treatment B: YZX','Treatment C: ZYX'))
    

    is telling to R that there is a vector df$f

    • which you want to transform into a factor,
    • in which the different levels are coded as a, b, and c
    • and for which you want the levels to be labeled as Treatment A etc.

    The factor function will look for the values a, b and c, convert them to numerical factor classes, and add the label values to the level attribute of the factor. This attribute is used to convert the internal numerical values to the correct labels. But as you see, there is no label attribute.

    > df <- data.frame(v=c(1,2,3),f=c('a','b','c'))    
    > attributes(df$f)
    $levels
    [1] "a" "b" "c"
    
    $class
    [1] "factor"
    
    > df$f <- factor(df$f, levels=c('a','b','c'),
    +   labels=c('Treatment A: XYZ','Treatment B: YZX','Treatment C: ZYX'))    
    > attributes(df$f)
    $levels
    [1] "Treatment A: XYZ" "Treatment B: YZX" "Treatment C: ZYX"
    
    $class
    [1] "factor"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There seems to be a difference between the last commit, the HEAD and the
I found an Ask Tom article explaining that there is not much difference between
There seems to be a difference between the two, Though I can't tell what
I know that Strings are immutable. Then, what is the difference between: String name
Is there any low-level, implementation-related difference (performance-ish) between these approaches..? # check if string
I know there are various flavours of JSDoc around. And it seems that each
Are there any differences between get_object_vars($obj) and (array) $obj ? Both seem to return
There seems to be a breaking (i.e. backwards-incompatible) change in the CSS Syntax Module
There seems to be a lot of questions asking for regex to detect a
There seems to be a distinct lack of documentation of the GObjects module for

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.