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

  • SEARCH
  • Home
  • 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 7901403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:12:35+00:00 2026-06-03T09:12:35+00:00

In an answer to another question, @Marek posted the following solution: https://stackoverflow.com/a/10432263/636656 dat <-

  • 0

In an answer to another question, @Marek posted the following solution:
https://stackoverflow.com/a/10432263/636656

dat <- structure(list(product = c(11L, 11L, 9L, 9L, 6L, 1L, 11L, 5L, 
                                  7L, 11L, 5L, 11L, 4L, 3L, 10L, 7L, 10L, 5L, 9L, 8L)), .Names = "product", row.names = c(NA, -20L), class = "data.frame")

`levels<-`(
  factor(dat$product),
  list(Tylenol=1:3, Advil=4:6, Bayer=7:9, Generic=10:12)
  )

Which produces as output:

 [1] Generic Generic Bayer   Bayer   Advil   Tylenol Generic Advil   Bayer   Generic Advil   Generic Advil   Tylenol
[15] Generic Bayer   Generic Advil   Bayer   Bayer  

This is just the printout of a vector; so to store it, you can do the even more confusing:

res <- `levels<-`(
  factor(dat$product),
  list(Tylenol=1:3, Advil=4:6, Bayer=7:9, Generic=10:12)
  )

Clearly this is some kind of call to the levels function, but I have no idea what’s being done here. What is the term for this kind of sorcery, and how do I increase my magical ability in this domain?

  • 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-06-03T09:12:36+00:00Added an answer on June 3, 2026 at 9:12 am

    The answers here are good, but they are missing an important point. Let me try and describe it.

    R is a functional language and does not like to mutate its objects. But it does allow assignment statements, using replacement functions:

    levels(x) <- y
    

    is equivalent to

    x <- `levels<-`(x, y)
    

    The trick is, this rewriting is done by <-; it is not done by levels<-. levels<- is just a regular function that takes an input and gives an output; it does not mutate anything.

    One consequence of that is that, according to the above rule, <- must be recursive:

    levels(x)[1] <- "a"
    

    is

    levels(x) <- `[<-`(levels(x), 1, "a")
    

    is

    x <- `levels<-`(x, `[<-`(levels(x), 1, "a"))
    

    It’s kind of beautiful that this pure-functional transformation (up until the very end, where the assignment happens) is equivalent to what an assignment would be in an imperative language. This construct in functional languages is called a lens. Lenses can be awkward to use in some programming languages, but in R they just work.

    But then, once you have defined replacement functions like levels<-, you get another, unexpected windfall: you don’t just have the ability to make assignments, you have a handy function that takes in a factor, and gives out another factor with different levels. There’s really nothing "assignment" about it!

    So, the code you’re describing is just making use of this other interpretation of levels<-. I admit that the name levels<- is a little confusing because it suggests an assignment, but this is not what is going on. The code is simply setting up a sort of pipeline:

    • Start with dat$product

    • Convert it to a factor

    • Change the levels

    • Store that in res

    Personally, I think that line of code is beautiful 😉

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

Sidebar

Related Questions

An answer posted for one of my previous questions brings up another question; I
I was following the answer to another question , and I got: // itemCounter
While trying my code to answer another question I found out that the following
Per a great answer from another question I have begun mounting global resources (css/js/images)
In an answer to another question it was mentioned that passing XML as a
So I saw an answer in another question saying that this should work: using
I saw this in an answer to another question , in reference to shortcomings
In a comment on this answer to another question , someone said that they
The answer to another SO question was to use this SQL query: SELECT o.Id,
In another question, the answer states that on Unixes with /proc , the really

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.