‘relevel’ only for (unordered) factors

Explanation:

For the query ‘relevel’, the ‘relevel’ function in R is used to reorder the levels of a factor.

Consider the following example:

    
# Create a factor
factor_example <- factor(c("A", "B", "C", "B", "A"))

# Check the original levels
levels(factor_example)
# Output: "A" "B" "C"

# Use relevel to reorder the levels
factor_example <- relevel(factor_example, ref = "C")

# Check the new levels
levels(factor_example)
# Output: "C" "A" "B"
    
  

In the above example, we have a factor called 'factor_example' with levels "A", "B", and "C". By using the 'relevel' function and setting the reference level as "C", the levels are reordered as "C", "A", and "B".

By changing the reference level, we can control the order in which the levels are represented in the factor.

Related Post

Leave a comment