9lotus f <- factor(letters[1:5]) levels(f) <- c(levels(f), "new_lev") 返回 a b c d e levels: a b c d e new_lev 但 levels(f) <- c("new_lev", levels(f)) 却返回 new_lev a b c d levels: new_lev a b c d e 这是为什么呢?谢谢
tctcab factors带标签的整数,levels代表了整数到标签的映射关系(1-> levels(f)[1], 2-> levels(f)[2]这样),所以改变levels当然会改变factors显示的结果,但factors储存的整数并没有改变。你的代码其实就是很好的例子 可以用as.numeric(f)查看factor的整数向量。前后是一样的
9lotus 谢谢楼上两位答疑解惑。 但仍有一事不明: f <- factor(letters[1:5]) levels(f) <- c(levels(f), "new_lev") f 的长度为5,levels(f) 为长度为6的character, 1 -- a; 2 -- b; ... 5 -- e 映射,那么 "new_lev" 与什么映射呢? 谢谢