Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我从数值向量创建一个因子,因子类别是否会按现在被视为类别的值自动排序?
即 [1,4,7,3,2] -> 类别 = {1,2,3,4,7}
简短的回答:是的。
长答案:这取决于。factor()如果您使用函数转换向量而不调用任何额外参数,R 将对唯一值进行排序并按该顺序分配类别:
factor()
> x <- c(3,1,4,5,1,4) > factor(x) [1] 3 1 4 5 1 4 Levels: 1 3 4 5
但是,当您使用参数时它不会levels:
levels
> factor(x, levels=unique(x)) [1] 3 1 4 5 1 4 Levels: 3 1 4 5
在这种情况下,它将级别的顺序作为它分配类别的顺序。