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


R語言 is.ordered()用法及代碼示例


is.ordered()R語言中的函數用於檢查傳遞的因子是否為有序因子。

用法: is.ordered(factor)

參數:
factor:檢查因子

範例1:


# 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)

輸出:

[1] FALSE

範例2:


# 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

相關用法


注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Check if a Factor is an Ordered Factor in R Programming – is.ordered() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。