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.
In this example,
age_groups
which contains the values 'Young'
, 'Middle'
, 'Old'
, and 'Elderly'
. This vector represents different age groups.factor()
function to convert the age_groups
vector into a factor. We assign the result to a variable named age_factor
. The factor()
function automatically identifies the unique levels of the vector.fct_collapse()
function from the `forcats` package to collapse the levels of the age_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 named collapsed_age_factor
.collapsed_age_factor
to the console to verify the new levels.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)
[1] Young-Middle Young-Middle Old-Elderly Old-Elderly Young-Middle Old-Elderly Levels: Young-Middle Old-Elderly
In this example,
product_categories
which contains the values 'Electronics'
, 'Furniture'
, 'Clothing'
, 'Food'
, and 'Electronics'
. This vector represents different product categories.factor()
function to convert the product_categories
vector into a factor. We assign the result to a variable named product_factor
. The factor()
function automatically identifies the unique levels of the vector.fct_collapse()
function from the `forcats` package to collapse the levels of the product_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 named collapsed_product_factor
.collapsed_product_factor
to the console to verify the new levels.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)
[1] Non-Consumable Non-Consumable Consumable Consumable Non-Consumable Levels: Non-Consumable Consumable
In this example,
survey_responses
which contains the values 'Strongly Agree'
, 'Agree'
, 'Neutral'
, 'Disagree'
, and 'Strongly Disagree'
. This vector represents different levels of agreement in a survey.factor()
function to convert the survey_responses
vector into a factor. We assign the result to a variable named response_factor
. The factor()
function automatically identifies the unique levels of the vector.fct_collapse()
function from the `forcats` package to collapse the levels of the response_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 named collapsed_response_factor
.collapsed_response_factor
to the console to verify the new levels.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)
[1] Positive Positive Neutral Negative Negative Levels: Positive Neutral Negative
In this tutorial, we learned How to Collapse Factor Levels in R language with well detailed examples.