ordered in R: How to create or convert the ordered factor in R?

ordered in r

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:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *