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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:39:57+00:00 2026-06-01T08:39:57+00:00

With the help of flodel I found a way to replace numeric codes with

  • 0

With the help of flodel I found a way to replace numeric codes with value labels from a lookup table.

Ambitious as I am, I now want to put that into a function. Also, I have a lot of lookup tables I need to swoop onto my data so a function would be handy.

But first some sample data, starting with a data fram,

df <- data.frame(id = c(1:6),
                 profession = c(1, 5, 4, NA, 0, 5))

df
#  id profession
#  1          1
#  2          5
#  3          4
#  4         NA
#  5          0
#  6          5

and a lookup table with human readable information about the profession codes,

profession.lookuptable <- c(Optometrists=1, Accountants=2, Veterinarians=3, 
                            `Financial analysts`=4,  Nurses=5)

flodel showed me how replace numeric codes with value labels from a lookup table. Like this,

match.idx <- match(df$profession, profession.lookuptable)
df$profession <- ifelse(is.na(match.idx), 
                 df$profession, names(profession.lookuptable)[match.idx])

df
#  id         profession
#  1        Optometrists
#  2              Nurses
#  3  Financial analysts
#  4                <NA>
#  5                   0
#  6              Nurses

I now want to put this into a function where I can state the data frame df and the name of the variable profession and have the function take care of the rest.

I define my function like this,

ADDlookup <- function(orginalDF, orginalVAR) {
   DF.VAR <- paste(orginalDF, "$", orginalVAR, sep="")
   lookup.table <- paste(orginalVAR, ".lookuptable")
   match.idx <- match(DF.VAR, lookup.table)
   DF.VAR <- ifelse(is.na(match.idx), DF.VAR, names(lookup.table)[match.idx])
}

but apparently that is not working

ADDlookup(df, profession)

I get the errorer messes

Error in paste(orginalDF, "$", orginalVAR, sep = "") : 
  object 'profession' not found

Now, this is where I get stuck.

Can anyone please tell what manual page I need to read or maybe a friendly hint on how to solve this?

Thank you for reading.

  • 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-01T08:39:59+00:00Added an answer on June 1, 2026 at 8:39 am

    It’s because you’re passing profession into the ADDlookup function, but it doesn’t exist yet.

    The way you’ve written your function, you have to distinguish between using the character vector containing the name of the variable, and the variable itself.

    For example, your first few lines paste(originalDF,'$',originalVAR,sep='') etc appear to expect originalDF and originalVAR to be strings, and you’ll have DF.VAR being the string 'df$profession'. However, when you do match it looks like you want DF.VAR to be the variable df$profession.

    This is how I suggest you get around it:
    – pass in originalDF as an object, being df
    – pass in originalVAR as a string, being 'profession' (it’s a column name and hence a string)

    Then, retrieve the column contained in originalVar from the data frame via:

    DF.VAR <- originalDF[,originalVAR] # e.g. df[,'profession']
    

    Now your next line where you look for the object profession.lookuptable is a little trickier: you construct the string 'profession.lookuptable', and then you want to look up the object that has that name.

    For this, you can use get (?get). get('df') will return the df data frame:

    lookup.table <- get(paste(orginalVAR, "lookuptable",sep='.'))
    

    This will retrieve the object called 'profession.lookuptable'. It follows the same rules as if you’d typed profession.lookuptable directly, so you have to make sure that the function can “see” that object (in your case you should be fine).

    Next, it looks like you want to return the originalDF data frame where the originalVAR column has been substituted with the lookup values.

    I’ll just modify the originalDF[,originalVAR] column by replacing it with the lookup values:

    originalDF[,originalVAR] <- 
       ifelse(is.na(match.idx), DF.VAR, names(lookup.table)[match.idx])
    

    NOTE that we are not actually modifying the df data frame that you passed in as an argument to ADDlookup; R makes a copy of the data frame within the function. So, your original df is preserved.

    Finally, we have to return the data frame:

    return(originalDF)
    

    All together now:

    ADDlookup <- function(orginalDF, orginalVAR) {
       # retrieve the originalVAR column of originalDF
       DF.VAR <- originalDF[,originalVAR] 
       # find the variable called {originalVAR}.lookuptable
       lookup.table <- get(paste(originalVAR, "lookuptable",sep='.'))
       # look up the values
       match.idx <- match(DF.VAR, lookup.table)
       # replace the originalVAR column with the looked-up values
       originalDF[,originalVAR] <- 
           ifelse(is.na(match.idx), DF.VAR, names(lookup.table)[match.idx])
       # return the modified data frame
       return(originalDF)
    }
    

    And now to test it:

    > ADDlookup(df,'profession')
      id         profession
    1  1       Optometrists
    2  2             Nurses
    3  3 Financial analysts
    4  4               <NA>
    5  5                  0
    6  6             Nurses
    

    Note that the original df is unmodified; in general R functions do not modify the parameters that are passed in to them.


    As another improvement — it is generally a bit dangerous to rely on the professions.lookup table having been created before you call the ADDlookup function.

    Instead of the whole lookup.table <- get( 'profession.lookup' ) shebang (which, depending on if you have multiple ‘profession.lookup’ tables in various scopes), I’d strongly recommend you just pass in the lookup table as a parameter:

    ADDlookup <- function( originalDF, originalVAR, lookup.table )
    

    Then you can avoid that entire get(xxxx) line (and all associated scoping problems that go with it).

    Then you’d call the function via:

    ADDlookup( df, 'profession', profession.lookup )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

help out a noob with a simple web development question?? I want to create
Help. TCHAR* b; TCHAR* c=TEXT(qwerty); I want to allocate memory and copy content of
HELP......Just moved to C# from vb and Im really lost with this. var ldapmembershipUser
Help me to find best way for replacing version variable in my ui.xml <gwt:HTML>
Help me pleas. I need to find some way to open files with external
Help , How can you detect whenever the power cord is unplugged from electrical
Help me please. How could I get the url from the address bar. Im
Help me in reading this dictionary: (its an NSDictionary generated from XMLData) Menus =
Help me out, gurus /* * In this function, I want the static variable
Help! I'm using the ASP.Net Login control on a Login page, but the Login

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.