Section 5 Factor: Ordinal

5.1 Ordinal Factor

Example vector: x <- rep(c('L','M','H'), length=30)

Function Explanation Example
factor Make a vector to factor factor(x=x, levels=c(L,M,H), labels=c(Low,Med,High))
as.factor Convert a vector to factor as.factor(x)
ordered Make a vector to an ordered factor ordered(x=x, levels=c(L,M,H), labels=c(Low,Med,High))
as.ordered Convert a vector to ordered factor as.ordered(x)
levels Get the labels of the factor levels levels(x)
nlevels Get the number of levels of a factor nlevels(x)

Note: In the above table, levels and labels of a factor should be with single or double quotes.


5.2 Example

y <- rep(c('L','M','H'), length=30)

o1y <- as.ordered(x=y)

y
o1y

levels(o1y)
nlevels(o1y)
as.numeric(o1y)
table(o1y)
attributes(o1y)


o2y <- ordered(x=y, levels=c('L','M','H'), labels=c('Low','Med','High'))

y
o2y

levels(o2y)
nlevels(o2y)
as.numeric(o2y)
table(o2y)
attributes(o2y)