How to remove ordering of the levels from factor variable in R? How to remove ordering of the levels from factor variable in R? r r

How to remove ordering of the levels from factor variable in R?


All you need is

x <- factor( x , ordered = FALSE )

e.g.

x <- factor( c(1,2,"a") , ordered = TRUE )x#[1] 1 2 a#Levels: 1 < 2 < ax <- factor( x , ordered = FALSE )x#[1] 1 2 a#Levels: 1 2 a


If you created your variable via ordered, it's as simple as resetting its class to factor.

f <- ordered(letters)class(f) <- "factor"identical(f, factor(letters))

In a linear or additive model (including linear regression, logistic regression and anything fit with lm, glm and gam) a factor predictor is treated exactly the same as an ordered predictor in terms of the overall model fit. You'll get the same predicted values, residuals, lack-of-fit statistics, etc regardless of which one you use.

However, the contrasts are different for the two classes. A factor uses treatment contrasts, ie the usual dummy-variable coding with a given level treated as the baseline. An ordered factor uses polynomial contrasts, which are based on orthogonal polynomials (whatever that means; I've never had a reason to use ordered factors). Because of this, the t-stats and P-values for individual coefficients will be different.