为什么在as.factor()R中出现级别错误?

我无法通过as.factor来识别一组级别。无论出于什么原因,我都看不到错误/错误。它使我发疯。

代码是:

yearsPostEDI<-as.matrix(namedat$`Days post EDI`)
yearsPostEDI<-as.numeric(yearsPostEDI)/365
yearsPostEDI<-round(yearsPostEDI,0)
yearsPostEDI[1:10]<-c(rep("HIV_neg",10))
yearsPostEDI[yearsPostEDI == "0"] <- "<1 Year"
yearsPostEDI[yearsPostEDI == "1"] <- "1 Year"
yearsPostEDI[yearsPostEDI == "2"] <- "2 Years"
yearsPostEDI[yearsPostEDI == "3"] <- "3 Years"
yearsPostEDI[yearsPostEDI == "4"] <- "4 Years"
yearsPostEDI[yearsPostEDI == "5"] <- ">4 Years"
yearsPostEDI[yearsPostEDI == "6"] <- ">4 Years"
yearsPostEDI[yearsPostEDI == "7"] <- ">4 Years"
yearsPostEDI<-as.factor(yearsPostEDI, levels = c("HIV_neg", "<1 Year", "1 Year", "2 Years", "3 Years", "4 Years", ">4 Years"))
yearsPostEDI

错误消息是:

Error in as.factor(yearsPostEDI, levels = c("HIV_neg", "<1 Year", "1 Year",  : 
  unused argument (levels = c("HIV_neg", "<1 Year", "1 Year", "2 Years", "3 Years", "4 Years", ">4 Years"))
评论
  • 零零柒
    零零柒 回复

    你需要

    yearsPostEDI<-factor(yearsPostEDI, levels = c("HIV_neg", "<1 Year", "1 Year", "2 Years", "3 Years", "4 Years", ">4 Years"))
    

    factor() not as.factor()