r - 在有序因子列中获取所有NA

时间:2016-10-25 22:39:47

标签: r rstudio

而不是在列中显示more2或更少2,它只显示为NA。为什么不出现角色名称呢?

careermore2 <- vector(mode="character",length=length(mlb$careeryrs))
"less2" <- careermore2[mlb$careeryrs<=2]
"more2" <- careermore2[mlb$careeryrs>=2]
No.seasons <-       factor(careermore2,levels=c("more2","less2"),exclude=NA,ordered=TRUE)
mlb2 <- cbind(mlb,No.seasons)
str(mlb2$No.seasons)
head(mlb2$No.seasons)
mlb2[mlb2$No.seasons=="more2",]

1 个答案:

答案 0 :(得分:0)

careermore2我会说你的方法错了:

"less2" <- careermore2[mlb$careeryrs<=2]
"more2" <- careermore2[mlb$careeryrs>=2]

创建两个对象。你的意思是:

careermore2[mlb$careeryrs<=2] = "less2"
careermore2[mlb$careeryrs>=2] = "more2"

即在careermore2中设置相应的值。你可能想要<2>2而不是= ......