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?
You have misspelled “fund.category” in your earlier statement:
Fix the typo and it should be happy 🙂
To understand the error,
a.projects.2$fund.categoryreturnsNULLas.factor(NULL)returnsfactor(0)and it is when assigning
factor(0)toa.projects.2$fund.categorythat you get the error: