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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:47:45+00:00 2026-05-31T13:47:45+00:00

I’m dealing with a categorical variable retrieved from a database and am wanting to

  • 0

I’m dealing with a categorical variable retrieved from a database and am wanting to use factors to maintain the “fullness” of the data.

For instance, I have a table which stores colors and their associated numerical ID

  ID  | Color
------+-------
    1 | Black
 1805 | Red
 3704 | White

So I’d like to use a factor to store this information in a data frame such as:

Car Model | Color
----------+-------
Civic     | Black
Accord    | White
Sentra    | Red

where the color column is a factor and the underlying data stored, rather than being a string, is actually c(1, 3704, 1805) — to IDs associated with each color.

So I can create a custom factor by modifying the levels attribute of an object of the factor class to achieve this effect.

Unfortunately, as you can see in the example, my IDs are not incremented. In my application, I have ~30 levels and the maximum ID for one level is ~9,000. Because the levels are stored in an array for a factor, that means I’m storing an integer vector of length 9,000 with only 30 elements in it.

Is there any way to use a hash or list to accomplish this effect more efficiently? i.e. if I were to use a hash in the levels attribute of a factor, I could store all 30 elements with whatever indices I please without having to create an array of size max(ID).

Thanks in advance!

  • 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-31T13:47:46+00:00Added an answer on May 31, 2026 at 1:47 pm

    Well, I’m pretty sure you can’t change how factors work. A factor always has level ids that are integer numbers 1..n where n is the number of levels.

    …but you can easily have a translation vector to get to your color ids:

    # The translation vector...
    colorIds <- c(Black=1,Red=1805,White=3704)
    
    # Create a factor with the correct levels 
    # (but with level ids that are 1,2,3...)
    f <- factor(c('Red','Black','Red','White'), levels=names(colorIds))
    as.integer(f) # 2 1 2 3
    
    # Translate level ids to your color ids
    colorIds[f] # 1805 1 1805 3704
    

    Technically, colorIds does not need to define the names of the colors, but it makes it easier to have in one place since the names are used when creating the levels for the factor. You want to specify the levels explicitly so that the numbering of them matches even if the levels are not in alphabetical order (as yours happen to be).

    EDIT It is however possible to create a class deriving from factor that has the codes as an attribute. Lets call this new glorious class foo:

    foo <- function(x = character(), levels, codes) {
        f <- factor(x, levels)
        attr(f, 'codes') <- codes
        class(f) <- c('foo', class(f))
        f
    }
    
    `[.foo` <- function(x, ...) {
        y <- NextMethod('[')
        attr(y, 'codes') <- attr(x, 'codes')
        y
    }
    
    as.integer.foo <- function(x, ...) attr(x,'codes')[unclass(x)]
    
    # Try it out
    set.seed(42)
    f <- foo(sample(LETTERS[1:5], 10, replace=TRUE), levels=LETTERS[1:5], codes=101:105)
    
    d <- data.frame(i=11:15, f=f)
    
    # Try subsetting it...
    d2 <- d[2:5,]
    
    # Gets the codes, not the level ids...
    as.integer(d2$f) # 105 102 105 104
    

    You could then also fix print.foo etc…

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

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.