Hello, readers! In this article, we will be focusing on the gl() function in R in terms of factors, thoroughly.
So, let us get started!
Table of Contents
Working of the R gl() function
Before diving deep into gl() function in R programming, let us quickly recall the concept of Factors in R.
Factors are those data structures in R that enable us to store categorical data values in them. They represent groups or categories of data with their structure. With factors, the group of categorical data values gets stored with certain levels assigned to them based on the label of the categories.
In the context of factor creation, the
gl() function helps us customize the process. That is, with gl() function, we can create customized factor data at ease.
The gl() function allows us to generate factors by specifying customized values for the levels or groups.
Let us have a look at the structure of gl() function to have a clear idea about it.
Syntax of gl()
Have a look at the below piece of code!
gl(n, r, len, labels, ordered = FALSE)
- number of levels in the factor: n
- length of the factor: len
- number of repetitions of the levels: r
- labels: Label values for the factor
- ordered: If set to TRUE, creates an ordered level for the factor
Example 1: Creation of factors with gl() function in R
In this example, we have created a factor using gl() function. And, we have provided 3 as the count of levels with 5 repetition per level as shown below!
rm(list = ls()) data<- gl(3, 5) print(data)
As seen below, it creates 3 levels [1,2 and 3] with 5 repetitions per level. By default, if we do not specify the length, it takes number of levels * number of repetitions as the total length i.e. 3*5=15
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 Levels: 1 2 3
Example 2: Creating customized length factors with gl() function
Apart from simple factors, with gl() function we can even specify the length of the factor in a customized manner. Usually, the default length of a factor is number of levels * number of repetitions using gl() function.
In the below example, we have specified 20 as the length of the factor. Thus, as the length exceeds the default length(which would be 15 in this case [3*5]), the levels repeat themselves until the length does not get exhausted.
rm(list = ls()) data<- gl(3, 5, 20) print(data)
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 1 1 1 1 1 Levels: 1 2 3
Example 3: Customized labeled factors with gl() function
We can set customized labels of the created factor levels by the gl() function. We can pass the labels as a vector using c() function in the parameter list of the gl() function
rm(list = ls()) data<- gl(3, 5, label=c('t','u','v','w','x')) print(data)
Thus, as seen below, we have set labels [t,u,v,w and x] to the levels of the created factors.
t t t t t u u u u u v v v v v Levels: t u v w x
Example 4: Ordering the levels with gl() function
With gl() function, we can have ordered levels using ordered parameter of the function. For the same, we need to set the ordered parameter to TRUE, as shown below!
rm(list = ls()) data<- gl(3, 5, label=c('t','u','v','w','x'),ordered=TRUE) print(data)
As a result, the levels have been ordered by the gl() function within the created factor.
t t t t t u u u u u v v v v v Levels: t < u < v < w < x
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.
For more such posts related to R programming, Stay tuned with us!
Do let us know about your understanding with regards to gl() function in the comment box.
Till then, Happy Learning!! 🙂