Generate Factors with specified Levels in R Programming – gl() Function
Last Updated :
16 Jun, 2020
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:
x1 < - gl( 2 , 5 )
x2 < - gl( 3 , 4 , 12 )
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:
x1 < - gl( 3 , 4 , 12 , label = letters[ 1 : 12 ])
x2 < - gl( 3 , 4 , 12 , label = letters[ 1 : 12 ], ordered = T)
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...