超过 2 个有序变量的排序因子

Sorting factors in more than 2 ordinal variables

我有一个关于比较超过 3 个有序变量的因子的问题。

我已经在 Rstudio 和 Datacamp 上试过了。在处理超过 2 个序数变量(低、中、高)时设置特定顺序后,在比较高和中时,为什么 "high > medium" 会产生 FALSE?

temperature_vector <- c("High", "Low", "High","Low", "Medium")
factor_temperature_vector <- factor(temperature_vector, order = TRUE, levels = c("Low", "Medium", "High"))
factor_temperature_vector 

#The above line returns:
#[1] High   Low    High   Low    Medium
#Levels: Low < Medium < High

high <- temperature_vector[1]
medium <- temperature_vector[5]
low <- temperature_vector[2]

high > low #returns FALSE

high > medium #returns FALSE. Why?

已解决:

需要比较因子而不是变量:

high <- **factor_**temperature_vector[1]
medium <- **factor_**temperature_vector[5]
low <- **factor_**temperature_vector[2]

标识符的分配来自 character 向量,而不是 factor 向量。对于 character 字符串,排序是字母顺序,其中 h 小于 m

high <- factor_temperature_vector[1]
medium <- factor_temperature_vector[5]
low <- factor_temperature_vector[2]

high > low
#[1] TRUE
high > medium
#[1] TRUE