In this article, we will show you what the ordered
in R does and how to perform the ordered()
in R. The ordered()
in R is used to create the ordered factor or convert the object to the ordered factor
. Let’s learn more about it with the explanation and examples below.
What does the ordered do in R?
The ordered()
function is used to convert the data to the ordered factor. Besides that, the ordered is also the hyperparameter of the factor()
function which is used to create the ordered factor. Let’s take a look at the syntax of two functions.
Syntax:
# Convert the object to the ordered factor.
ordered(data)
Parameters:
- data: The R object. Usually, it is the factor or the vector.
Return value:
The ordered factor.
# Create the ordered factor.
factor(x, levels, labels = levels, exclude, ordered = is.ordered(x), nmax)
Parameters:
- x: The R object. Usually, it is the factor or the vector.
- levels: The vector of the unique values.
- labels: The default is levels.
- exclude: The default is NA. The vector of the excluded values.
- nmax: The default is NA.
After learning the usage and the syntax of the ordered in R, you will learn how to perform it in the next title below.
How to use the ordered in R?
Convert the data to the ordered factor
You can convert the data to the ordered factor by the ordered()
function.
Look at the example below.
vec <- c("d", "a", "d", "b", "c", "b", "a") # Create the factor fac <- factor(vec, levels = c("d", "c", "b", "a")) fac
Output
[1] d a d b c b a
Levels: d c b a
Take a look at the type of the ‘fac’.
vec <- c("d", "a", "d", "b", "c", "b", "a") fac <- factor(vec, levels = c("d", "c", "b", "a")) class(fac)
Output
[1] "factor"
Convert to the ordered factor.
vec <- c("d", "a", "d", "b", "c", "b", "a") fac <- factor(vec, levels = c("d", "c", "b", "a")) ordered_fac <- ordered(fac) class(ordered_fac)
Output
[1] "ordered" "factor"
Create the ordered factor
You can create the ordered factor by the factor()
function.
Look at the example below.
vec <- c("d", "a", "d", "b", "c", "b", "a") # Create the ordered factor. ordered_fac <- factor(vec, levels = c("d", "c", "b", "a"), ordered = TRUE) ordered_fac
Output
[1] d a d b c b a
Levels: d < c < b < a
Look at the type of this object.
vec <- c("d", "a", "d", "b", "c", "b", "a") ordered_fac <- factor(vec, levels = c("d", "c", "b", "a"), ordered = TRUE) class(ordered_fac)
Output
[1] "ordered" "factor"
You can also learn the R language with the series of R tutorials here.
Summary
You have learned what the ordered in R does, and some functions use the ordered
in R. This function is used to work with the ordered factor that helps convert the object to the ordered factor or create the ordered factor. If you have any questions about this guide, leave your comment below, and I will answer your questions. Thanks!
Maybe you are interested:
- The first() function in R
- The ln() function in R
- lead function in R: Set the NA values at the end of the R object.

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R