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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:34:08+00:00 2026-06-01T17:34:08+00:00

This question is related to this question , but not quite the same. Say

  • 0

This question is related to this question, but not quite the same.

Say I have this data frame,

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

and a string with human readable information about the profession codes. Say,

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

Now, I’m looking for the easiest way to replace the values in df$profession with the text found in profession.code. Preferably without use of special libraries, unless it shortens the code significantly.

I would like my end result to be

df <- data.frame(
                id = c(1:6),
                profession = c("Optometrists", "Nurses", 
                "Financial analysts", NA, 0, "Nurses"))

Any help would be greatly appreciated.

Thanks,
Eric

  • 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-01T17:34:09+00:00Added an answer on June 1, 2026 at 5:34 pm

    You can do it this way:

    df <- data.frame(id = c(1:6),
                     profession = c(1, 5, 4, NA, 0, 5))
    
    profession.code <- c(`0` = 0, Optometrists=1, Accountants=2, Veterinarians=3, 
                         `Financial analysts`=4,  Nurses=5)
    
    df$profession.str <- names(profession.code)[match(df$profession, profession.code)]
    df
    #   id profession     profession.str
    # 1  1          1       Optometrists
    # 2  2          5             Nurses
    # 3  3          4 Financial analysts
    # 4  4         NA               <NA>
    # 5  5          0                  0
    # 6  6          5             Nurses
    

    Note that I had to add a 0 entry in your profession.code vector to account for those zeroes.

    EDIT: here is an updated solution to account for Eric’s comment below that the data may contain any number of profession codes for which there are no corresponding descriptions:

    match.idx <- match(df$profession, profession.code)
    df$profession.str <- ifelse(is.na(match.idx),
                                df$profession,
                                names(profession.code)[match.idx])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to (but perhaps not quite the same as): Does Django
[This question is related to but not the same as this one .] My
[This question is related to but not the same as this one .] If
This is kind of related to my previous question, but not really. I have
[This question is somewhat related to this question , but the answers are not...]
This question might not seem programming related at first, but let me explain. I'm
This is not exactly a programming question, but it's highly related. We are writing
This is a not related to my project code , but a question came
I´m not sure if this is really a programming question, but it's related to
I know this is not a real programming question. But, it relates to programming

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.