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


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

nlevels()R语言中的函数用于获取因子的级别数。

用法: nlevels(x)

参数:
x:因子对象

范例1:


# R program to get the number
# of levels of a factor
  
# Creating a factor
x <- gl(3, 2)
x
  
# Calling nlevels() function
# to get the number of levels
nlevels(x)

输出:

[1] 1 1 2 2 3 3
Levels:1 2 3
[1] 3

范例2:


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

输出:

[1] female male   male   female
Levels:female male
[1] 2

相关用法

注:本文由纯净天空筛选整理自nidhi_biet大神的英文原创作品 Get the Number of Levels of a Factor in R Programming – nlevels() Function。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。