Levels in R: What are the Factor Levels

Factors are used to represent categorical data. Factors can be ordered or unordered and are an important class for statistical analysis and for plotting. To create a factor in R, use the factor() function. Levels of a factor are gathered from the data if not provided. 

Levels in R

The levels() is an inbuilt R function that provides access to the levels attribute. The first form returns the value of the levels of its argument, and the second sets the attribute.

You can assign the individual levels using the gl() function.

When you first get a dataset, you will usually notice that it contains particular factor levels. Nevertheless, sometimes you need to change the names of these levels for clarity or other reasons. R supports you to do this with the function levels(). Let’s see the syntax of the levels() function.

Syntax

levels(x) 
levels(x) <- value

Parameters

The x is an input factor.

The value 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 length at least the number of levels of x, or a named list specifying how to rename the levels.

Example

data_vector <- c("Hermione", "Harry", "Ron", "Albus")
factor_vector <- factor(data_vector)
factor_vector

data <- factor_vector[1]
data

Output

[1] Hermione Harry Ron Albus
Levels: Albus Harry Hermione Ron

[1] Hermione
Levels: Albus Harry Hermione Ron

In this example, we have not defined any levels, so it takes the levels from the data.

We can assign the new levels using the levels() function.

data_vector <- c("Hermione", "Harry", "Ron", "Draco")
factor_vector <- factor(data_vector)
factor_vector

levels(factor_vector) <- c("Godric", "Salazar", "Hufflepuff", "Ravenclaw")
factor_vector

Output

[1] Hermione Harry Ron Draco
Levels: Draco Harry Hermione Ron
[1] Hufflepuff Salazar Ravenclaw Godric
Levels: Godric Salazar Hufflepuff Ravenclaw

You can summarize the vector 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 is it for levels in R programming.

Leave a Comment