R Factors

Factors in R are used to represent categorical data. They store both the values and the unique levels, making them useful for statistical modeling and data analysis.

Key Topics

Factor Creation

Factors can be created using the factor() function.

# Creating a factor
fruit <- factor(c("apple", "banana", "apple", "cherry"))

print(fruit)

Output:

[1] apple banana apple cherry
Levels: apple banana cherry

Code Explanation: The factor() function creates a factor with three levels: apple, banana, and cherry. The unique levels are stored and displayed.

Factor Levels

You can access or modify the levels of a factor using the levels() function.

# Modifying factor levels
levels(fruit) <- c("apple", "banana", "grape")

print(fruit)

Output:

[1] apple banana apple grape
Levels: apple banana grape

Code Explanation: The levels() function is used to modify the levels of the factor fruit, changing cherry to grape.

Key Takeaways

  • Factors are used for categorical data and store unique levels.
  • The factor() function creates factors in R.
  • Levels can be accessed or modified using the levels() function.