 GeeksforGeeks App
Open App Browser
Continue

# Convert an Unordered Factor to an Ordered Factor in R Programming – as.ordered() Function

`as.ordered()` function in R Language takes an unordered factor as argument and converts it into an ordered factor.

Syntax: as.ordered(factor)

Parameters:
factor: Unordered Factor to be converted

Example 1:

 `# Creating a vector ``x<``-``c(``"North"``, ``"North"``, ``"East"``, ``"West"``) `` ` `# Converting vector into factor``Directions <``-` `factor(x)`` ` `# Using as.ordered() Function``# to order an unordered factor``as.ordered(Directions)`

Output:

``` North North East  West
Levels: East < North < West
```

Example 2:

 `# creating vector size ``size ``=` `c(``"small"``, ``"large"``, ``"large"``, ``"small"``,  ``         ``"medium"``, ``"large"``, ``"medium"``, ``"medium"``)   ``sizes <``-` `ordered(c(``"small"``, ``"large"``, ``"large"``, ``                   ``"small"``, ``"medium"``)) ``   ` `# Using as.ordered() Function``# to order an unordered factor``as.ordered(sizes)`

Output:

``` small  large  large  small  medium
Levels: large < medium < small
```
My Personal Notes arrow_drop_up