简体   繁体   中英

why do I get a levels error in as.factor() R?

I can't get as.factor to recognise a set of levels. For whatever reason I just can't see the error/typo. It's driving me mad.

The code is:

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

The error message is:

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"))

You need

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

factor() not as.factor()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM