📜  检查因子是否是 R 编程中的有序因子 - is.ordered()函数

📅  最后修改于: 2022-05-13 01:55:45.126000             🧑  作者: Mango

检查因子是否是 R 编程中的有序因子 - is.ordered()函数

R 编程语言中的is.ordered()函数用于检查传递的因子是否为有序因子。

R 编程示例中的 is.ordered()函数

示例 1: R 的演示 – is.ordered()函数

R
# Creating a vector
x<-c("female", "male", "male", "female")
 
# Converting vector into factor
gender <- factor(x)
 
# Using is.ordered() Function
# to check if a factor is ordered
is.ordered(gender)


R
# creating vector size
size = c("small", "large", "large", "small", 
         "medium", "large", "medium", "medium")  
sizes <- ordered(c("small", "large", "large",
                   "small", "medium"))
   
# ordering the levels
sizes <- ordered(sizes, levels = c("small", "medium",
                                   "large"))    
 
# Checking if the factor is ordered
# using is.ordered() function
is.ordered(sizes)


输出:

[1] FALSE

示例 2:

R

# creating vector size
size = c("small", "large", "large", "small", 
         "medium", "large", "medium", "medium")  
sizes <- ordered(c("small", "large", "large",
                   "small", "medium"))
   
# ordering the levels
sizes <- ordered(sizes, levels = c("small", "medium",
                                   "large"))    
 
# Checking if the factor is ordered
# using is.ordered() function
is.ordered(sizes)

输出:

[1] TRUE