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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:49:13+00:00 2026-06-03T03:49:13+00:00

I run across this often enough that I figure there has to be a

  • 0

I run across this often enough that I figure there has to be a good idiom for it. Suppose I have a data.frame with a bunch of attributes, including “product.” I also have a key which translates products to brand + size. Product codes 1-3 are Tylenol, 4-6 are Advil, 7-9 are Bayer, 10-12 are Generic.

What’s the fastest (in terms of human time) way to code this up?

I tend to use nested ifelse‘s if there are 3 or fewer categories, and type out the data table and merge it in if there are more than 3. Any better ideas? Stata has a recode command that is pretty nifty for this sort of thing, although I believe it promotes data-code intermixing a little too much.

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")
  • 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-03T03:49:14+00:00Added an answer on June 3, 2026 at 3:49 am

    One could use a list as an associative array to define the brand -> product code mapping, i.e.:

    brands <- list(Tylenol=1:3, Advil=4:6, Bayer=7:9, Generic=10:12)
    

    Once you have this, you can then either invert this to create a product code -> brand list (could take a lot of memory), or just use a search function:

    find.key <- function(x, li, default=NA) {
        ret <- rep.int(default, length(x))
        for (key in names(li)) {
            ret[x %in% li[[key]]] <- key
        }
        return(ret)
    }
    

    I’m sure there are better ways of writing this function (the for loop is annoying me!), but at least it is vectorised, so it only requires a single pass through the list.

    Using it would be something like:

    > dat$brand <- find.key(dat$product, brands)
    > dat
       product   brand
    1       11 Generic
    2       11 Generic
    3        9   Bayer
    4        9   Bayer
    5        6   Advil
    6        1 Tylenol
    7       11 Generic
    8        5   Advil
    9        7   Bayer
    10      11 Generic
    11       5   Advil
    12      11 Generic
    13       4   Advil
    14       3 Tylenol
    15      10 Generic
    16       7   Bayer
    17      10 Generic
    18       5   Advil
    19       9   Bayer
    20       8   Bayer
    

    The recode and levels<- solutions are very nice, but they are also significantly slower than this one (and once you have find.key this is easier-for-humans than recode and on par with the levels<-):

    > microbenchmark(
         recode=recode(dat$product,recodes="1:3='Tylenol';4:6='Advil';7:9='Bayer';10:12='Generic'"), 
         find.key=find.key(dat$product, brands),
         levels=`levels<-`(factor(dat$product),brands))
    Unit: microseconds
          expr      min        lq    median        uq      max
    1 find.key   64.325   69.9815   76.8950   83.8445  221.748
    2   levels  240.535  248.1470  274.7565  306.8490 1477.707
    3   recode 1636.039 1683.4275 1730.8170 1855.8320 3095.938
    

    (I can’t get the switch version to benchmark properly, but it appears to be faster than all of the above, although it is even worse-for-humans than the recode solution.)

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

Sidebar

Related Questions

I run across this problem frequently suppose I have a text file that I
We have a very data centric application, which is often run across networks with
I have run across this where I need to know whether my current viewcontroller
I have run across this method in our code base and wonder what the
Just wondering if you have ever run across this. On Internet Explorer I get
I have often come across this snippet : General form of invokeLater is -
So I've run across a problem that has been addressed in other languages but
I've run across this sort of thing on multiple websites and was wondering what
Search the web for the phrase I cosay. I run across this phrase being
Ok, I've run across my first StackOverflowError since joining this site, I figured this

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.