The levels() function in R is used primarily with factors, which are data structures used to categorize data and store it as levels. It allows you to get or set the levels of a factor.
Syntax
levels(x)
levels(x) <- value
Parameters
- x: It is the x is an input factor.
- value: It is a valid value for levels(x). For the default method, NULL or a character vector. For the factor method, a vector of character strings with a length of at least the number of levels of x or a named list specifying how to rename the levels.
Example 1: Getting Levels of a Factor
# Create a factor
colors <- factor(c("red", "blue", "green", "red", "blue"))
# Get levels of the factor
cat("Levels of the 'colors' factor:", levels(colors), "\n")
Output
Levels of the 'colors' factor: blue green red
You can see that it will display the unique levels of the colors factor: “blue”, “green”, and “red”.
Example 2: Modifying Levels of a Factor
To reorder or rename the levels, you can directly assign the new levels to the factor.
# Create a factor
fruits <- factor(c("apple", "banana", "apple", "cherry"))
# Set new levels for the factor
levels(fruits) <- c("banana", "cherry", "apple", "grape")
# Print the modified levels
cat("Modified 'fruits' factor levels:", levels(fruits), "\n")
Output
Modified 'fruits' factor levels: banana cherry apple grape
Example 3: Adding new levels using c() function
# Create a factor
fruits <- factor(c("apple", "banana", "cherry"))
# Print original levels
print(levels(fruits))
# Add a new level
levels(fruits) <- c(levels(fruits), "grape")
# Print updated levels
print(levels(fruits))
Output
[1] "apple" "banana" "cherry"
[1] "apple" "banana" "cherry" "grape"
Remember, when you modify the levels of a factor, the underlying integer codes that represent the factor’s values don’t change; only their labels (levels) do.
Ensure that the new levels align correctly with the data to avoid misinterpretation.
Example 4: Factor with unused levels
# Create a factor with specified levels
grades <- factor(c("A", "B", "A"), levels = c("A", "B", "C", "D", "F"))
# Display the levels
cat("Levels of the 'grades' factor:", levels(grades), "\n")
Output
Levels of the 'grades' factor: A B C D F
Example 5: Summarize the factor
You can summarize the factor using the summary() method.
data_vector <- c("Hermione", "Harry", "Ron", "Draco")
factor_vector <- factor(data_vector)
factor_vector
levels(factor_vector) <- c("Godric", "Salazar", "Hufflepuff", "Ravenclaw")
summary(factor_vector)
Output
[1] Hermione Harry Ron Draco
Levels: Draco Harry Hermione Ron
Godric Salazar Hufflepuff Ravenclaw
1 1 1 1
That’s it!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.