The factor is one of the special cases of the vector in R language to store categorical or ordinal variables. In some of the data, you may come across attributes like gender which has two factors, male and female. In this article, we will talk about factors in R programming.
The advantage of using the factors in R is, the system can store the values like 1,2,1 instead of male, female, and male. This process will save a lot of memory required for the process.
A simple example of factors in R
Let’s see a simple illustration that shows the working of factors in R. We will use the factor() function for this process.
#Creates a dataframe df <- factor(c('Male','Female','Male','Female','Female')) #Prints the data df
Male Female Male Female Female Levels: Female Male
Here you can see that the factor() function has returned the levels in the data. It stores male as 1 and female 2 so that it can save most of the memory.
Factor’s in R – The Slicing
Now, let’s see how we can access the elements in the levels.
#Creats a dataframe df <- factor(c('O+','B+','AB-','A+','B+','B+','AB-','AB-','A+')) #Pirnts the data df
O+ B+ AB- A+ B+ B+ AB- AB- A+ Levels: A+ AB- B+ O+
#Returs the elements from range 1 to 2 df[1:2]
O+ B+ Levels: A+ AB- B+ O+
That’s it. If you want to access the data, you can do it by mentioning the range of data that you want to access or slice from the data.
A factor cannot be used for character vector that is not truly categorical. If a vector includes mostly unique values such as names and identification strings, then just keep it as a character vector.
The factor data structure also allows us to include information about the order of a nominal categorical. This provides a convenient way to store ordinal data.
The factors in R can save most of the memory as it stores the data in the form of numerical numbers such as 1 and 2 and 3 and so on. You can also order the sequence which shows the hierarchy of the levels i.e. Easy medium and Hard.
That’s all for now, Happy factoring!!!
More read: R documentation