To merge factors in R, you can use the factor()
function along with the levels
and labels
arguments to combine levels of one or more factors into a single factor. This is useful when you need to simplify categorical data by combining similar levels.
In this example,
product_categories
which contains the values 'Electronics'
, 'Clothing'
, 'Clothing'
, 'Furniture'
, 'Electronics'
, and 'Gadgets'
. 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.levels
and labels
arguments of the factor()
function to merge some of the levels. Specifically, we combine 'Electronics'
and 'Gadgets'
into a single level called 'Electronics/Gadgets'
. We assign the modified factor to a variable named merged_product_factor
.merged_product_factor
to the console to see the new factor levels. This allows us to verify that the levels have been correctly merged.product_categories <- c('Electronics', 'Clothing', 'Clothing', 'Furniture', 'Electronics', 'Gadgets')
product_factor <- factor(product_categories)
merged_product_factor <- factor(product_factor, levels = c('Clothing', 'Electronics', 'Furniture', 'Gadgets'), labels = c('Clothing', 'Electronics/Gadgets', 'Furniture', 'Electronics/Gadgets'))
print(merged_product_factor)
[1] Electronics/Gadgets Clothing Clothing Furniture Electronics/Gadgets Electronics/Gadgets Levels: Clothing Electronics/Gadgets Furniture
In this example,
feedback
which contains the values 'Good'
, 'Average'
, 'Poor'
, 'Excellent'
, and 'Good'
. This vector represents different levels of customer feedback.factor()
function to convert the feedback
vector into a factor. We assign the result to a variable named feedback_factor
. The factor()
function automatically identifies the unique levels of the vector.levels
and labels
arguments of the factor()
function to merge some of the levels. Specifically, we combine 'Good'
and 'Excellent'
into a single level called 'Positive'
. We assign the modified factor to a variable named merged_feedback_factor
.merged_feedback_factor
to the console to see the new factor levels. This allows us to verify that the levels have been correctly merged.feedback <- c('Good', 'Average', 'Poor', 'Excellent', 'Good')
feedback_factor <- factor(feedback)
merged_feedback_factor <- factor(feedback_factor, levels = c('Average', 'Excellent', 'Good', 'Poor'), labels = c('Average', 'Positive', 'Positive', 'Poor'))
print(merged_feedback_factor)
[1] Positive Average Poor Positive Positive Levels: Average Positive Poor
In this example,
animals
which contains the values 'Cat'
, 'Dog'
, 'Bird'
, 'Fish'
, 'Cat'
, and 'Dog'
. This vector represents different types of animals.factor()
function to convert the animals
vector into a factor. We assign the result to a variable named animal_factor
. The factor()
function automatically identifies the unique levels of the vector.levels
and labels
arguments of the factor()
function to merge some of the levels. Specifically, we combine 'Cat'
and 'Dog'
into a single level called 'Mammal'
. We assign the modified factor to a variable named merged_animal_factor
.merged_animal_factor
to the console to see the new factor levels. This allows us to verify that the levels have been correctly merged.animals <- c('Cat', 'Dog', 'Bird', 'Fish', 'Cat', 'Dog')
animal_factor <- factor(animals)
merged_animal_factor <- factor(animal_factor, levels = c('Bird', 'Cat', 'Dog', 'Fish'), labels = c('Bird', 'Mammal', 'Mammal', 'Fish'))
print(merged_animal_factor)
[1] Mammal Mammal Bird Fish Mammal Mammal Levels: Bird Fish Mammal
In this tutorial, we learned How to Merge Factors in R language with well detailed examples.