Browse By

R Language –Data Structures : Online Course

R Factors

In R, factors are one of the data structures where it is used to represent categorical data and store it at multiple levels. For example – gender, availability, country, marital status, etc. Such data is called categorical data.
They can store both strings and integers. These factors are created with the help of factor() functions.

Examples of factors are:

  • Demography: Male/Female
  • Music: Rock, Pop, Classic, Jazz
  • Training: Strength, Stamina

Syntax of Factors

We use the factor() function to create factors. The following syntax of the factor() function:

factor_name=factor(x=character(),levels,labels,
exclude,ordered,nmax)

Create a Factor

In R, The factor() command is used to create and modify factors. The two steps to creating a factor are:  

  • First is Creating a vector.
  • Second is Converting the vector created into a factor.
Input: Let us create a factor day with levels.
#Creating a vector as input.
days <- c("Friday", "Tuesday", "Thursday", "Monday", "Wednesday", "Monday",
"Wednesday", "Monday", "Monday", "Wednesday", "Sunday", "Saturday")
Levels in alphabetical order
my_factor <- factor(days) my_factor print("_______________________________________")

#levels of a factor can be checked using the levels() function.
levels(my_factor)
Output:

Access Factors Elements

Accessing factors element is very much similar to that of vectors. Let’s see an example ,we understand the different ways of accessing the elements.

Input: To create different ways of accessing the elements
#Creating a vector as input.
data = factor(c(“March”,”April”,”January”,”November”,”January”,
“September”,”October”,”September”,”November”,”August”,
“January”,”November”,”November”,”February”,”May”,”August”,
“July”,”December”,”August”,”August”,”September”,”November”,
“February”,”April”) )
#Printing all elements of factor
data
print(“_________________________________________“)
#Accessing 4th element of factor
print(data[4])
print(“_________________________________________“)
#Accessing 5th and 7th element
print(data[c(5,7)])
print(“_________________________________________“)
#Accessing all element except 4th one
print(data[-4])
Output:

Modification Factor Elements

In R, using reassignment can modify existing values ​​and also add new ones as well. The new values ​​must be assigned within the predefined level of the factor.

Input: Let’s see an example, how the modification is done in factors.
#Creating a vector as input.
days = c(“Friday”, “Tuesday”, “Thursday”, “Monday”, “Wednesday”, “Monday”,
“Wednesday”, “Monday”, “Monday”, “Wednesday”, “Sunday”, “Saturday”)
my_factor = factor(days)
my_factor
#Change 4th element of factor
days[4] = “Sat”
days
#add new level
levels(days) <- c(levels(days), “other”)
days[3] <- “other”
days
Output:

R Language: Online Course

आशा करता हूँ, कि यह आर्टिकल आपको पसंद आया होगा तो सोच क्या रहे हैं अभी इसी वक्त इसे अपने दोस्तों के साथ सोशल मीडिया पर Share करें।

Thanking You………………धन्यवाद………………..शुक्रिया………………..मेहरबानी…………………..

Read More

Reference

R Tutorial

https://r-coder.com/factor-r/