How to Collapse Factor Levels in R - Step by Step Examples
How to Collapse Factor Levels in R ?
Answer
To collapse factor levels in R, you can use the `fct_collapse` function from the `forcats` package. This function allows you to group existing levels of a factor into new combined levels, making it easier to analyze or visualize data.
✐ Examples
1 Collapsing Levels of a Factor Representing Age Groups
In this example,
- We start by creating a character vector named
age_groups
which contains the values'Young'
,'Middle'
,'Old'
, and'Elderly'
. This vector represents different age groups. - Next, we use the
factor()
function to convert theage_groups
vector into a factor. We assign the result to a variable namedage_factor
. Thefactor()
function automatically identifies the unique levels of the vector. - We then use the
fct_collapse()
function from the `forcats` package to collapse the levels of theage_factor
into fewer groups. In this example, we group'Young'
and'Middle'
into a new level'Young-Middle'
and'Old'
and'Elderly'
into a new level'Old-Elderly'
. We assign the result to a variable namedcollapsed_age_factor
. - We print the
collapsed_age_factor
to the console to verify the new levels.
R Program
library(forcats)
age_groups <- c('Young', 'Middle', 'Old', 'Elderly', 'Young', 'Old')
age_factor <- factor(age_groups)
collapsed_age_factor <- fct_collapse(age_factor,
'Young-Middle' = c('Young', 'Middle'),
'Old-Elderly' = c('Old', 'Elderly'))
print(collapsed_age_factor)
Output
[1] Young-Middle Young-Middle Old-Elderly Old-Elderly Young-Middle Old-Elderly Levels: Young-Middle Old-Elderly
2 Collapsing Levels of a Factor Representing Product Categories
In this example,
- We start by creating a character vector named
product_categories
which contains the values'Electronics'
,'Furniture'
,'Clothing'
,'Food'
, and'Electronics'
. This vector represents different product categories. - Next, we use the
factor()
function to convert theproduct_categories
vector into a factor. We assign the result to a variable namedproduct_factor
. Thefactor()
function automatically identifies the unique levels of the vector. - We then use the
fct_collapse()
function from the `forcats` package to collapse the levels of theproduct_factor
into fewer groups. In this example, we group'Electronics'
and'Furniture'
into a new level'Non-Consumable'
and'Clothing'
and'Food'
into a new level'Consumable'
. We assign the result to a variable namedcollapsed_product_factor
. - We print the
collapsed_product_factor
to the console to verify the new levels.
R Program
library(forcats)
product_categories <- c('Electronics', 'Furniture', 'Clothing', 'Food', 'Electronics')
product_factor <- factor(product_categories)
collapsed_product_factor <- fct_collapse(product_factor,
'Non-Consumable' = c('Electronics', 'Furniture'),
'Consumable' = c('Clothing', 'Food'))
print(collapsed_product_factor)
Output
[1] Non-Consumable Non-Consumable Consumable Consumable Non-Consumable Levels: Non-Consumable Consumable
3 Collapsing Levels of a Factor Representing Survey Responses
In this example,
- We start by creating a character vector named
survey_responses
which contains the values'Strongly Agree'
,'Agree'
,'Neutral'
,'Disagree'
, and'Strongly Disagree'
. This vector represents different levels of agreement in a survey. - Next, we use the
factor()
function to convert thesurvey_responses
vector into a factor. We assign the result to a variable namedresponse_factor
. Thefactor()
function automatically identifies the unique levels of the vector. - We then use the
fct_collapse()
function from the `forcats` package to collapse the levels of theresponse_factor
into fewer groups. In this example, we group'Strongly Agree'
and'Agree'
into a new level'Positive'
,'Neutral'
remains as it is, and'Disagree'
and'Strongly Disagree'
into a new level'Negative'
. We assign the result to a variable namedcollapsed_response_factor
. - We print the
collapsed_response_factor
to the console to verify the new levels.
R Program
library(forcats)
survey_responses <- c('Strongly Agree', 'Agree', 'Neutral', 'Disagree', 'Strongly Disagree')
response_factor <- factor(survey_responses)
collapsed_response_factor <- fct_collapse(response_factor,
'Positive' = c('Strongly Agree', 'Agree'),
'Negative' = c('Disagree', 'Strongly Disagree'))
print(collapsed_response_factor)
Output
[1] Positive Positive Neutral Negative Negative Levels: Positive Neutral Negative
Summary
In this tutorial, we learned How to Collapse Factor Levels in R language with well detailed examples.
More R Factors Tutorials
- How to Create Factors in R ?
- How to find Length of a Factor in R ?
- How to Loop over a Factor in R ?
- How to Convert Data to Factors in R ?
- How to Order Factor Levels in R ?
- How to Access Factor Levels in R ?
- How to Modify Factor Levels in R ?
- How to Reorder Factor Levels in R ?
- How to Add Levels to a Factor in R ?
- How to Drop Levels from a Factor in R ?
- How to Rename Levels of a Factor in R ?
- How to Use Factors in Data Frames in R ?
- How to Generate Summary Statistics for Factors in R ?
- How to Merge Factors in R ?
- How to Split Data by Factors in R ?
- How to Plot Factors in R ?
- How to Convert Factors to Numeric in R ?
- How to Convert Factors to Character in R ?
- How to Handle Missing Values in Factors in R ?
- How to Use Factors in Conditional Statements in R ?
- How to Compare Factors in R ?
- How to Create Ordered Factors in R ?
- How to Check if a Variable is a Factor in R ?
- How to Use Factors in Statistical Models in R ?
- How to Collapse Factor Levels in R ?
- How to Use Factors in Grouping Operations in R ?
- How to Use Factors in Aggregation Functions in R ?
- How to Deal with Unused Factor Levels in R ?
- How to Encode and Decode Factors in R ?
- How to Use Factors in Regression Analysis in R ?
- How to Convert Factors to Dates in R ?