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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:20:29+00:00 2026-06-12T20:20:29+00:00

I’m working with a dataset in R that comes with a codebook, which basically

  • 0

I’m working with a dataset in R that comes with a codebook, which basically tells me what the labels for the different levels of my factor variables should be. For example, with the codebook, I can see that in my “Sex” variable, the 0s are “Female” and the 1s are “Male.” I’m using this information to label the values in my variables accordingly.

However, I’ve recently discovered, to my dismay, that the codebook is not complete. For example, it tells me for one variable that 1s are “Yes” and 2s are “No,” but doesn’t tell me what 7s, 8s, and 9s are, which I can see in the data. What I would like to do is label this variable as follows (or something like this):

data$variable <- factor(data$variable,
                        levels=c(1, 2, 7, 8, 9),
                        labels=c("Yes", "No", "7", "8", "9"))

Basically, I would like for all levels that weren’t specified in the codebook to be labeled as themselves. The problem I am having is that this codebook is missing quite a few of these, and I would really rather not have to manually look at all undefined values in my data to construct the above code for ever variable. Plus, if I just leave out those missing levels, R automatically labels them as “NA,” which I do not want.

Summary: I am trying to figure out how to use factor() such that instead of labeling all unspecified levels as “NA,” it labels them as themselves.

  • 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-12T20:20:31+00:00Added an answer on June 12, 2026 at 8:20 pm

    You can convert levels after you create a factor so we can use that to our advantage.

    mydat <- c(1, 2, 3,2,3,4,3,2,1,2,4,4,6,5,7,8,9)
    # convert to factor ignoring code book
    dat <- factor(mydat)
    # Create map corresponding to codebook levels
    mymap <- c("1" = "Yes", "2" = "No")
    # Figure out which levels are accounted for by codebook
    id <- levels(dat) %in% names(mymap)
    # Convert to appropriate values
    levels(dat)[id] <- mymap[levels(dat)[id]]
    

    Alternatively (and probably a little easier)

    # alternatively we can construct the map if we have two vectors
    # of the value and the codebook value
    val <- c(1, 2)
    lev <- c("Yes", "No")
    
    dat <- factor(mydat)
    levels(dat)[val] <- lev
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.