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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。