Generate Factors with specified Levels in R Programming - gl() Function Last Updated : 15 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report gl() function in R Language is used to generate factors by specifying the pattern of their levels. Syntax: gl(x, k, length, labels, ordered) Parameters: x: Number of levels k: Number of replications length: Length of result labels: Labels for the vector(optional) ordered: Boolean value to order the levels Example 1: Python3 1== # R Program to generate factors # Creating a factor # using gl() function x1 <- gl(2, 5) # gl() function with # length specified x2 <- gl(3, 4, 12) # Printing the factors print(x1) print(x2) Output: [1] 1 1 1 1 1 2 2 2 2 2 Levels: 1 2 [1] 1 1 1 1 2 2 2 2 3 3 3 3 Levels: 1 2 3 Example 2: Python3 1== # R Program to generate factors # gl() function with # length and labels specified x1 <- gl(3, 4, 12, label = letters[1:12]) # gl() function with # length, label and order specified x2 <- gl(3, 4, 12, label = letters[1:12], ordered = T) # Printing the factors print(x1) print(x2) Output: [1] a a a a b b b b c c c c Levels: a b c d e f g h i j k l [1] a a a a b b b b c c c c Levels: a < b < c < d < e < f < g < h < i < j < k < l Comment More infoAdvertise with us Next Article Get a List of Numbers in the Specified Range in R Programming - seq.int() Function N nidhi_biet Follow Improve Article Tags : R Language R Factor-Function Similar Reads Get or Set Levels of a Factor in R Programming - levels() Function levels() function in R Language is used to get or set the levels of a factor. Syntax: levels(x) Parameters: x: Factor Object Example 1: Python3 1== # R program to get levels of a factor # Creating a factor gender <- factor(c("female", "male", "male", "female 1 min read Generate a Sequence from 1 to any Specified Number in R Programming - seq_len() Function seq_len() function in R Language is used to generate a sequence from 1 to the specified number. Syntax: seq_len(x) Parameters: x: specified number Example 1: Python3 # R program to illustrate # seq_len function # Calling the seq_len() function seq_len(1) seq_len(3) seq_len(0) seq_len(6) Output: [1] 1 min read Get the Number of Levels of a Factor in R Programming - nlevels() Function nlevels() function in R Language is used to get the number of levels of a factor. Syntax: nlevels(x) Parameters: x: Factor Object Example 1: Python3 1== # R program to get the number # of levels of a factor # Creating a factor x <- gl(3, 2) x # Calling nlevels() function # to get the number of le 1 min read Get a List of Numbers in the Specified Range in R Programming - seq.int() Function seq.int() function in R Language is used to return a list of numbers in the specified range. Syntax: seq.int(from, to, by) Parameters: from: specified range from to: specified range to by: specified number by which it jumps through returned number Example 1: Python3 # R program to illustrate # seq.i 1 min read Generating sequenced Vectors in R Programming - sequence() Function sequence() function in R Language is used to create a vector of sequenced elements. It creates vectors with specified length, and specified differences between elements. It is similar to seq() function. Syntax: sequence(x) Parameters: x: Maximum element of vector Example 1: Python3 1== # R program t 1 min read Generate Color Vectors of desired Length in R Programming - rainbow() Function rainbow() function in R Language is a built in color palettes which can be used to quickly generate color vectors of desired length taken as the parameter and returns the hexadecimal code of the colours available. This hexadecimal code is of eight digits. This is because the last two digits specify 1 min read Like