2个以上序数变量中的排序因子

时间:2019-05-18 19:55:04

标签: r syntax ordinal

在比较三个以上序数变量中的因素时,我有一个问题。

我已经在Rstudio和Datacamp上进行了尝试。设置了特定的顺序以处理两个以上的序数变量(低,中,高)后,在比较高和中时,为什么“ 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]

1 个答案:

答案 0 :(得分:0)

对标识符的分配来自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