R语言 获取或设置一个因子的水平 – levels()函数

R语言 获取或设置一个因子的水平 – levels()函数

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

语法: level(x)

参数:

x: 因子对象

例子 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程