Reorder factor levels using names

佐手、 提交于 2019-11-29 16:12:01

Why don't you use the basic variant with giving new level names:

factor(iris$Species, levels=c("virginica", "versicolor", "setosa"))

Be sure to list all level names, though. Otherwise, you will end up with NA values.

However, for completeness: If you rely on the order of the elements within a factor, you probably should used ordered instead of factor. That is just a factor with, well, ordered levels, or, more mathematically, a relation < between the factor levels. See:

> ordered(1:3, levels=c('1', '3', '2'))
[1] 1 2 3
Levels: 1 < 3 < 2
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!