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


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