What is the gl() function in R?

Overview

The gl() function in R is used to generate factors simply by specifying the pattern of their levels.

Syntax

gl(n, k, length = n*k, labels = seq_len(n), ordered = FALSE)

Parameter values

The gl() function takes the following parameter values:

  • n: This is an integer that indicates the number of levels.
  • k: This is an integer that indicates the number of replications.
  • length: This is an integer that indicates the length of the result.
  • labels: This is an optional vector of labels for the output of the factor levels.
  • ordered: This takes a logical value (either TRUE or FALSE), which indicates whether the output should be ordered or not.

Code example

# implementing the gl() function
# using factor of 3
# using a replication of 1
gl(3, 1, length = 8, labels=c('yes', 'no', 'maybe'))

Code explanation

In the code given above, we implement the gl() function using a factor level of 3, a replication of 1, and a length of 8.

Free Resources