获取 R 编程中因子的级别数 – nlevels()函数
R 语言中的nlevels()
函数用于获取因子的级别数。
Syntax: nlevels(x)
Parameters:
x: Factor Object
示例 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