當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。