在不改变 R 中值的顺序的情况下重新排序因子水平
在本文中,我们将讨论如何在 R 编程语言中重新排序因子级别而不改变值的顺序。
如果我们想改变级别的顺序,我们可以在 factor()函数指定 levels() 参数
句法:
factor(actual_vector,levels)
在哪里,
- actual_vector 是因子输入向量
- 级别是要按该顺序指定的名称。 levels 参数可以接受向量元素。因此,重新排序是手动完成的。
示例:重新排序级别的 R 程序
R
# create a student names vector
student= factor(c("sravan","bobby","ojaswi",
"rohith","gnanesh"))
# display
print(student)
# change the order levels
changed = factor(student, c("bobby", "sravan",
"ojaswi", "gnanesh",
"rohith"))
# display
print(changed)
R
# create a student names vector
student= factor(c("sravan","bobby","ojaswi",
"rohith","gnanesh"))
# display
print(student)
# change the order levels
changed = factor(student, c("sravan", "ojaswi", "bobby",
"gnanesh","rohith"))
# display
print(changed)
输出:
[1] sravan bobby ojaswi rohith gnanesh
Levels: bobby gnanesh ojaswi rohith sravan
[1] sravan bobby ojaswi rohith gnanesh
Levels: bobby sravan ojaswi gnanesh rohith
示例:重新排序级别的 R 程序
电阻
# create a student names vector
student= factor(c("sravan","bobby","ojaswi",
"rohith","gnanesh"))
# display
print(student)
# change the order levels
changed = factor(student, c("sravan", "ojaswi", "bobby",
"gnanesh","rohith"))
# display
print(changed)
输出:
[1] sravan bobby ojaswi rohith gnanesh
Levels: bobby gnanesh ojaswi rohith sravan
[1] sravan bobby ojaswi rohith gnanesh
Levels: sravan ojaswi bobby gnanesh rohith