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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:43:07+00:00 2026-06-05T08:43:07+00:00

Note: The title may be misleading. If you understand my problem and think of

  • 0

Note: The title may be misleading. If you understand my problem and think of something more descriptive – please change it.

I’ve got a strange situation where the responses from a survey are all character, rather than numeric. It seems that R, really doesn’t like this. Let’s say I asked a question:

Q. In what area do you work? 
East
West
Central
North
South
None of the above

But respondents were only from the east, west and central.

dat <- rep(c("East", "West", "Central"),100)

Now, for presentation purposes, it’s important that I include North, south and None of the above, even if they are none. However, factoring those elements in is challenging.

Let’s try:

fac1 <- factor(dat, labels=c("East","West","Central","North","South","None of the above"))

Error in factor(dat, labels = c("East", "West", "Central", "North", "South",  : 
  invalid labels; length 6 should be 1 or 3

Basically, what i’d like to do is factor this data with the missing values. So that when I type something like summary(fac1) it shows them having 0 responses in that category.

There has to be an easier way to do this!

  • 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-05T08:43:09+00:00Added an answer on June 5, 2026 at 8:43 am

    Almost there. You need to use the levels argument:

    fac1 <- factor(dat, levels=c("East","West","Central","North","South","None of the above"))
    str(fac1)
     Factor w/ 6 levels "East","West",..: 1 2 3 1 2 3 1 2 3 1 ...
    

    The difference between levels and labels is this:

    • levels defines the factor levels in your data
    • labels allows you to rename the factor levels in one go.

    For example:

    fac2 <- factor(
      dat, 
      levels=c("East","West","Central","North","South","None of the above"),
      labels=c("E", "W", "C", "N", "S", "Other")
    )
    str(fac2)
    Factor w/ 6 levels "E","W","C","N",..: 1 2 3 1 2 3 1 2 3 1 ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an NSString with something like this in it: Note Title Here:@:Note
[Note: The title may be less than accurate, I didn't know how else to
Say my list is {1,2,3,4} My title may not be descriptive enough but here
NOTE: originally, I thought the issue was caused by something more complex; I see
NOTE: I have solved the majority of this problem but have run into a
The title may seem a little weird but what I find around the internet
So i bet i got you curious with that title. =) Here's the scenario
It was difficult to choose correct title, but here is the problem: I've an
I apologize that the title may not make sense, so let me describe my
Sorry if the title is misleading, wasn't sure how to describe this one. My

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.