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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:37:36+00:00 2026-06-01T23:37:36+00:00

This is a followup question to this question , initially inspired by this question

  • 0

This is a followup question to this question, initially inspired by this question, but not quite the same.

This is my situation. First I pull some data from a database,

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

Second, I pull a key-table with human readable information about the profession codes,

profession.codes <- data.frame(profession.code = c(1,2,3,4,5),
                               profession.label = c('Optometrists',
                               'Accountants', 'Veterinarians', 
                               'Financial analysts',  'Nurses'))                 
   profession.codes
#  profession.code   profession.label
#               1       Optometrists
#               2        Accountants
#               3      Veterinarians
#               4 Financial analysts
#               5             Nurses

Now, I would like to overwrite the profession variable in my df with the labels from profession.codes, preferably using join from the plyr package, but I’m open to any smart solution. Though I do like that ply preserves the order of x.

I currently do it like this,

# install.packages('plyr', dependencies = TRUE)
library(plyr)

profession.codes$profession <- profession.codes$profession.code
df <- join(df, profession.codes, by="profession")
# levels(df$profession.label)
df$profession.label <- factor(df$profession.label, 
   levels = c(levels(df$profession.label), 
   setdiff(df$profession, df$profession.code)))
# levels(df$profession.label)
df$profession.label[df$profession==0 ] <- 0
df$profession.code <- NULL
df$profession  <- NULL
names(df) <- c("id", "profession")
df
#  id         profession
#  1       Optometrists
#  2             Nurses
#  3 Financial analysts
#  4               <NA>
#  5                  0
#  6             Nurses

This is how I overwrite profession without losing the NA and the 0.

The problem is that the 0 could be a 17 or any number and I would like to account for that in some way. Furthermore, I would also like to shorten my code, if possible.

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-01T23:37:37+00:00Added an answer on June 1, 2026 at 11:37 pm

    This is one approach in base:

    df <- data.frame(id = c(1:6),
                     profession = c(1, 5, 4, NA, 0, 5))
    
    pc <- data.frame(profession.code = c(1,2,3,4,5),
                                   profession.label = c('Optometrists',
                                   'Accountants', 'Veterinarians', 
                                   'Financial analysts',  'Nurses'))  
    
    
    df$new <- as.character(pc[match(df$profession,  
        pc$profession.code), 'profession.label'])
    df[is.na(df$new), 'new'] <- df[is.na(df$new), 'profession'] 
    df$new <- as.factor(df$new)
    df
    

    Which yields:

      id profession                new
    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Followup question from this one: Swing font names do not match? (Making a font
This is a followup question of How to encode characters from Oracle to Xml?
This is a followup question to the one I posted last week Ajax.ActionLink not
This is a followup to this question: How to wait for input from the
This is a followup question from this one. Connecting Pyside with matplotlib My PythonFu
This is a followup question to: SQL Query with ORDER BY But I think
this is a followup question to Remove default apps from Django-admin I am trying
This is in part a followup to this question . I'm not sure the
This is a followup question from a previous question, which is hopefully a little
This is a followup to my previous question. Parsing file names from a character

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.