当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 levels()用法及代码示例


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

用法: levels(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

相关用法


注:本文由纯净天空筛选整理自nidhi_biet大神的英文原创作品 Get or Set Levels of a Factor in R Programming – levels() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。