📜  在 R 编程中获取或设置一个因子的级别 – levels()函数

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

在 R 编程中获取或设置一个因子的级别 – levels()函数

R 语言中的levels()函数用于获取或设置因子的水平。

示例 1:

# R program to get levels of a factor
  
# Creating a factor
gender <- factor(c("female", "male", "male", "female")); 
gender
  
# Calling levels() function
# to get the levels
levels(gender)

输出:

[1] female male   male   female
Levels: female male
[1] "female" "male"  

示例 2:

# R program to set levels of a factor
  
# Creating a factor
gender <- factor(c("female", "male", "male", "female")); 
gender
  
# Calling levels() function
# to set the levels
levels(gender) <- c("abc", "def")
gender

输出:

[1] female male   male   female
Levels: female male
[1] abc def def abc
Levels: abc def