R Combine Factor Level Groups of Data Frame Column (Example Code)

This tutorial shows how to regroup factor levels of a factor vector or column in the R programming language.

Creation of Example Data

data(iris)                                    # Loading example data
head(iris$Species)
# [1] setosa setosa setosa setosa setosa setosa
# Levels: setosa versicolor virginica

Example: Regroup Factor Column of Data Frame Using levels() Function in R

iris_new <- iris                              # Replicating data frame
levels(iris_new$Species) <- c("virginica",    # Changing factor levels
                              "versicolor",
                              "virginica")
head(iris_new$Species)                        # Display new factor column
# [1] virginica virginica virginica virginica virginica virginica
# Levels: virginica versicolor

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top