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
相關用法
- R語言 nlevels()用法及代碼示例
- R語言 is.factor()用法及代碼示例
- R語言 as.factor()用法及代碼示例
- R語言 as.ordered()用法及代碼示例
- R語言 is.ordered()用法及代碼示例
- R語言 droplevels()用法及代碼示例
- R語言 recode_factor()用法及代碼示例
- R語言 factanal()用法及代碼示例
- R語言 gl()用法及代碼示例
- R語言 dim()用法及代碼示例
- R語言 names()用法及代碼示例
- R語言 mode()用法及代碼示例
- R語言 structure()用法及代碼示例
- R語言 head()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Get or Set Levels of a Factor in R Programming – levels() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。