我正在尝试将数据框中的三个字符变量转换为因子,并且我不断收到这三个变量之一的错误。有问题的变量 fund.category 有 4 个可能的值:“未定义”、“小”、“大型现有”和“大型新建”。我的代码如下 - 我首先使用 XLConnect 从 Excel 表中读取数据框,然后删除不必要的变量并重命名我保留的变量:
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)
这是 a.projects.2 的结构,在我尝试进行因子转换之前生成:
'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" ...
这是来自控制台的错误:
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
相同的代码对我的其他两个字符变量(project.number 和 business.type)没有产生错误。任何想法为什么这不起作用?