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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:30:17+00:00 2026-06-11T15:30:17+00:00

I’m trying to convert three character variables in a data frame to factors, and

  • 0

I’m trying to convert three character variables in a data frame to factors, and I keep getting an error for one of the three. The problematic variable, fund.category, has 4 possible values: “Undefined”, “Small”, “Large Existing”, and “Large New Construction”. My code follows – I first read the data frame from an excel sheet using XLConnect, then dropped unnecessary variables and renamed the ones that I kept:

a.projects <- readWorksheet(wb, sheet = "ProjectsDetail")
a.projects.2 <- a.projects[c("ProjectNumber", "BusinessType", "Fund.Category")]
a.projects.2 <- rename(a.projects.2,
                       c("ProjectNumber" = "project.number",
                         "BusinessType" = "business.type",
                         "Fund.Category" = "fund.catetgory"))
str(a.projects.2)
a.projects.2$project.number <- as.factor(a.projects.2$project.number)
a.projects.2$business.type <- as.factor(a.projects.2$business.type)   
a.projects.2$fund.category <- as.factor(a.projects.2$fund.category)  

Here is the structure of a.projects.2, produced before I tried to do the factor conversions:

'data.frame':   4291 obs. of  3 variables:
 $ project.number: chr  "APS-10-02825" "APS-10-02826" "APS-10-02876" "APS-10-03134" ...
 $ business.type : chr  "Office" "Office" "Process Industrial" "K-12 School" ...
 $ fund.catetgory: chr  "Undefined" "Undefined" "Large Existing" "Large New Construction" ...

And here is the error from the console:

a.projects.2$fund.category <- as.factor(a.projects.2$fund.category)

Error in `$<-.data.frame`(`*tmp*`, "fund.category", value = integer(0)) : 
replacement has 0 rows, data has 4291

The same code produced no errors for my other two character variables (project.number and business.type). Any ideas why this isn’t working?

  • 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-11T15:30:18+00:00Added an answer on June 11, 2026 at 3:30 pm

    You have misspelled “fund.category” in your earlier statement:

    a.projects.2 <- rename(a.projects.2,
                           c("ProjectNumber" = "project.number",
                             "BusinessType" = "business.type",
                             "Fund.Category" = "fund.catetgory"))
    

    Fix the typo and it should be happy 🙂


    To understand the error,

    a.projects.2$fund.category returns NULL

    as.factor(NULL) returns factor(0)

    and it is when assigning factor(0) to a.projects.2$fund.category that you get the error:

    Error in `$<-.data.frame`(`*tmp*`, "fund.category", value = integer(0)) : 
    replacement has 0 rows, data has 4291
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.