Factors in R Programming Language are data structures that are implemented to categorize the data or represent categorical data and store it on multiple levels.
They can be stored as integers with a corresponding label to every unique integer. The R factors may look similar to character vectors, they are integers and care must be taken while using them as strings. The R factor accepts only a restricted number of distinct values. For example, a data field such as gender may contain values only from female, male, or transgender.
In the above example, all the possible cases are known beforehand and are predefined. These distinct values are known as levels. After a factor is created it only consists of levels that are by default sorted alphabetically.
Attributes of Factors in R Language
- x: It is the vector that needs to be converted into a factor.
- Levels: It is a set of distinct values which are given to the input vector x.
- Labels: It is a character vector corresponding to the number of labels.
- Exclude: This will mention all the values you want to exclude.
- Ordered: This logical attribute decides whether the levels are ordered.
- nmax: It will decide the upper limit for the maximum number of levels.

Creating a Factor in R Programming Language
The command used to create or modify a factor in R language is – factor() with a vector as input.
The two steps to creating an R factor :
- Creating a vector
- Converting the vector created into a factor using function factor()
Examples: Let us create a factor gender with levels female, male and transgender.
R
x <- c ( "female" , "male" , "male" , "female" )
print (x)
gender <- factor (x)
print (gender)
|
Output
[1] "female" "male" "male" "female"
[1] female male male female
Levels: female male
Levels can also be predefined by the programmer.
R
gender <- factor ( c ( "female" , "male" , "male" , "female" ),
levels = c ( "female" , "transgender" , "male" ));
gender
|
Output
[1] female male male female
Levels: female transgender male
Further one can check the levels of a factor by using function levels().
Checking for a Factor in R
The function is.factor() is used to check whether the variable is a factor and returns “TRUE” if it is a factor.
R
gender <- factor ( c ( "female" , "male" , "male" , "female" ));
print ( is.factor (gender))
|
Output
[1] TRUE
Function class() is also used to check whether the variable is a factor and if true returns “factor”.
R
gender <- factor ( c ( "female" , "male" , "male" , "female" ));
class (gender)
|
Output
[1] "factor"
Accessing elements of a Factor in R
Like we access elements of a vector, the same way we access the elements of a factor. If gender is a factor then gender[i] would mean accessing an ith element in the factor.
Example
R
gender <- factor ( c ( "female" , "male" , "male" , "female" ));
gender[3]
|
Output
[1] male
Levels: female male
More than one element can be accessed at a time.
Example
R
gender <- factor ( c ( "female" , "male" , "male" , "female" ));
gender[ c (2, 4)]
|
Output
[1] male female
Levels: female male
Subtract one element at a time.
Example
R
gender <- factor ( c ( "female" , "male" , "male" , "female" ));
gender[-3]
|
Output
[1] female male female
Levels: female male
- First, we create a factor vector gender with four elements: “female”, “male”, “male”, and “female”.
- Then, we use the square brackets [-3] to subset the vector and remove the third element, which is “male”.
- The output is the remaining elements of the gender vector, which are “female”, “male”, and “female”. The output also shows the levels of the factor, which are “female” and “male”.
Modification of a Factor in R
After a factor is formed, its components can be modified but the new values which need to be assigned must be at the predefined level.
Example
R
gender <- factor ( c ( "female" , "male" , "male" , "female" ));
gender[2]<- "female"
gender
|
Output
[1] female female male female
Levels: female male
For selecting all the elements of the factor gender except ith element, gender[-i] should be used. So if you want to modify a factor and add value out of predefined levels, then first modify levels.
Example
R
gender <- factor ( c ( "female" , "male" , "male" , "female" ));
levels (gender) <- c ( levels (gender), "other" )
gender[3] <- "other"
gender
|
Output
[1] female male other female
Levels: female male other
Factors in Data Frame
The Data frame is similar to a 2D array with the columns containing all the values of one variable and the rows having one set of values from every column. There are four things to remember about data frames:
- column names are compulsory and cannot be empty.
- Unique names should be assigned to each row.
- The data frame’s data can be only of three types- factor, numeric, and character type.
- The same number of data items must be present in each column.
In R language when we create a data frame, its column is categorical data, and hence a R factor is automatically created on it.
We can create a data frame and check if its column is a factor.
Example
R
age <- c (40, 49, 48, 40, 67, 52, 53)
salary <- c (103200, 106200, 150200,
10606, 10390, 14070, 10220)
gender <- c ( "male" , "male" , "transgender" ,
"female" , "male" , "female" , "transgender" )
employee<- data.frame (age, salary, gender)
print (employee)
print ( is.factor (employee$gender))
|
Output
age salary gender
1 40 103200 male
2 49 106200 male
3 48 150200 transgender
4 40 10606 female
5 67 10390 male
6 52 14070 female
7 53 10220 transgender
[1] TRUE
Get 90% Course fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.
The next 90 Days of focus & determination can unlock your full potential. The Three 90 challenge has started and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!
Similar Reads
Generate Factors with specified Levels in R Programming - gl() Function
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 gener
2 min read
How to Include Factors in Regression using R Programming?
Categorical variables (also known as a factor or qualitative variables) are variables that classify observational values into groups. They are either string or numeric are called factor variables in statistical modeling. Saving normal string variables as factors save a lot of memory. Factors can also be stored as level or label variables. They have
4 min read
Extract rows from R DataFrame based on factors
In this article, we will discuss how to extract rows from dataframe based on factors in R Programming Language. Method 1: Using indexing methods The data frame column can be accessed using its name (df$col-name) or by its index (df[[ col-indx ]]) to access a particular column. The data frame columns may contain values as factors by explicit convers
3 min read
How to Fix in R: Contrasts can be applied only to factors with 2 or more levels.
In this article, we will discuss how we can fix "contrasts can be applied only to factors with 2 or more levels" error in the R programming language. Contrasts can be applied only to factors with 2 or more levels: It is a common error produced by the R compiler. The complete form of this error is given below: Error in `contrasts<-`(`*tmp*`, valu
3 min read
How to do 3D line plots grouped by two factors with the Plotly package in R?
Users can construct dynamic and visually appealing charts with Plotly, a strong and adaptable data visualization library. We will be able to produce 3D line plots with Plotly that can be used to evaluate complex data and effectively convey findings. In this article, we will explore the process of creating 3D line plots that are grouped by two facto
5 min read
Level Ordering of Factors in R Programming
In this article, we will see the level ordering of factors in the R Programming Language. R - Level Ordering of Factors Factors are data objects used to categorize data and store it as levels. They can store a string as well as an integer. They represent columns as they have a limited number of unique values. Factors in R can be created using the f
4 min read
How to convert dataframe columns from factors to characters in R?
In this article, we will discuss how to convert dataframe columns from factors to characters in R Programming Language. A dataframe can have different types of columns stacked together to form a tubular structure. Easy modification of the columns' data as well as conversion between data types can be conducted over a dataframe. R Language provides
5 min read
Create Boxplot with respect to two factors using ggplot2 in R
Multiple variable distributions can be visualized with boxplots. ggplot2 allows us to create beautiful boxplots quickly. It is possible to have multiple subgroups for a variable of interest. In those situations, it is very useful to visualize using “grouped boxplots”. The ggplot2 package in R programming language provides a number of options for vi
3 min read
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
Given a number X and an array of N numbers. The task is to print all the numbers in the array whose set of prime factors is a subset of the set of the prime factors of X. Examples: Input: X = 60, a[] = {2, 5, 10, 7, 17} Output: 2 5 10 Set of prime factors of 60: {2, 3, 5} Set of prime factors of 2: {2} Set of prime factors of 5: {5} Set of prime f
13 min read
Find number of factors of N when location of its two factors whose product is N is given
Given a and b which represent the location of two factors of N whose product is equal to the number N when the factors are arranged in ascending order. The task is to find the total number of factors of N.Examples: Input : a = 2, b = 3 Output : 4 N = 6 factors are {1, 2, 3, 6} No of factors are 4 Input : a = 13, b = 36 Output : 48 Approach: The
4 min read