📌  相关文章
📜  在不改变 R 中值的顺序的情况下重新排序因子水平

📅  最后修改于: 2022-05-13 01:54:53.191000             🧑  作者: Mango

在不改变 R 中值的顺序的情况下重新排序因子水平

在本文中,我们将讨论如何在 R 编程语言中重新排序因子级别而不改变值的顺序。

如果我们想改变级别的顺序,我们可以在 factor()函数指定 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)


输出:

示例:重新排序级别的 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)

输出: