To drop levels from a factor in R, you can use the droplevels()
function, which removes unused levels from a factor. This is useful when you have a factor with levels that are no longer needed or relevant.
In this example,
colors
which contains the values 'red'
, 'green'
, 'blue'
, 'red'
, and 'blue'
. This vector represents different color categories.factor()
function to convert the colors
vector into a factor. We assign the result to a variable named colors_factor
. The factor()
function automatically identifies the unique levels of the vector, which in this case are 'red'
, 'green'
, and 'blue'
.colors_factor
to include only the first three elements: 'red'
, 'green'
, and 'blue'
. This removes any occurrence of 'red'
and 'blue'
in the subset but keeps their levels.droplevels()
function to remove unused levels from the colors_factor
. This will drop the levels that are no longer present in the data after subsetting.colors_factor
to the console to see the dropped levels. This allows us to verify that the unused levels have been correctly removed from the factor.colors <- c('red', 'green', 'blue', 'red', 'blue')
colors_factor <- factor(colors)
colors_factor <- colors_factor[1:3]
colors_factor <- droplevels(colors_factor)
print(colors_factor)
[1] red green blue Levels: green blue
In this example,
fruits
which contains the values 'apple'
, 'banana'
, 'cherry'
, and 'date'
. This vector represents different types of fruits.factor()
function to convert the fruits
vector into a factor. We assign the result to a variable named fruits_factor
. The factor()
function automatically identifies the unique levels of the vector, which in this case are 'apple'
, 'banana'
, 'cherry'
, and 'date'
.fruits_factor
to include only the first two elements: 'apple'
and 'banana'
. This removes 'cherry'
and 'date'
from the subset but keeps their levels.droplevels()
function to remove unused levels from the fruits_factor
. This will drop the levels 'cherry'
and 'date'
that are no longer present in the data after subsetting.fruits_factor
to the console to see the dropped levels. This allows us to verify that the unused levels have been correctly removed from the factor.fruits <- c('apple', 'banana', 'cherry', 'date')
fruits_factor <- factor(fruits)
fruits_factor <- fruits_factor[1:2]
fruits_factor <- droplevels(fruits_factor)
print(fruits_factor)
[1] apple banana Levels: apple banana
In this example,
animals
which contains the values 'dog'
, 'cat'
, 'bird'
, and 'fish'
. 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 animals_factor
. The factor()
function automatically identifies the unique levels of the vector, which in this case are 'dog'
, 'cat'
, 'bird'
, and 'fish'
.animals_factor
to include only the first three elements: 'dog'
, 'cat'
, and 'bird'
. This removes 'fish'
from the subset but keeps its level.droplevels()
function to remove unused levels from the animals_factor
. This will drop the level 'fish'
that is no longer present in the data after subsetting.animals_factor
to the console to see the dropped levels. This allows us to verify that the unused levels have been correctly removed from the factor.animals <- c('dog', 'cat', 'bird', 'fish')
animals_factor <- factor(animals)
animals_factor <- animals_factor[1:3]
animals_factor <- droplevels(animals_factor)
print(animals_factor)
[1] dog cat bird Levels: dog cat bird
In this tutorial, we learned How to Drop Levels from a Factor in R language with well detailed examples.