Factors in R
In this tutorial, we will learn about factors in R. We will cover the basics of creating, accessing, modifying, and performing operations on factors.
What is a Factor
A factor in R is used to represent categorical data. Factors are stored as integer vectors with a corresponding set of character values to use when the factor is displayed. They are useful in statistical modeling where categorical variables are required.
Creating Factors
Factors can be created in R using the factor()
function:
data <- c("high", "medium", "low", "medium")
levels <- c("low", "medium", "high")
factor_data <- factor(data, levels = levels)
Factors ensure that the levels are in a specific order.
Example 1: Initializing Factors
- Create a character vector representing categorical data.
- Define the levels for the factor.
- Create the factor using the
factor()
function. - Print the factor to see the results.
R Program
data <- c("high", "medium", "low", "medium")
levels <- c("low", "medium", "high")
factor_data <- factor(data, levels = levels)
print(factor_data)
Output
[1] high medium low medium Levels: low medium high
Example 2: Accessing Factor Levels
- Create a factor variable.
- Use the
levels()
function to access the levels of the factor. - Print the levels of the factor.
R Program
data <- c("high", "medium", "low", "medium")
levels <- c("low", "medium", "high")
factor_data <- factor(data, levels = levels)
print(levels(factor_data))
Output
[1] "low" "medium" "high"
Example 3: Modifying Factor Levels
- Create a factor variable.
- Modify the levels of the factor using the
levels()
function. - Print the modified factor.
R Program
data <- c("high", "medium", "low", "medium")
levels <- c("low", "medium", "high")
factor_data <- factor(data, levels = levels)
levels(factor_data) <- c("L", "M", "H")
print(factor_data)
Output
[1] H M L M Levels: L M H
Example 4: Using Factors in Conditional Statements
- Create a factor variable.
- Use an if statement to check if the factor contains a specific level.
- Print a message based on the condition.
R Program
data <- factor(c("yes", "no", "yes", "no"), levels = c("no", "yes"))
if ("yes" %in% levels(data)) {
print("Yes is a level")
} else {
print("Yes is not a level")
}
Output
Yes is a level
Example 5: Checking if a Factor Contains a Specific Level
- Create a factor variable.
- Use an if statement to check if the factor starts with a specific level.
- Print a message based on the condition.
R Program
data <- factor(c("apple", "banana", "cherry", "apple"), levels = c("apple", "banana", "cherry"))
if ("apple" %in% levels(data)) {
print("Factor contains apple")
} else {
print("Factor does not contain apple")
}
Output
Factor contains apple
Example 6: Using Factors to Check Even Numbers
- Create a numeric vector and convert it to a factor.
- Use an if statement to check if a number is even.
- Print a message based on the condition.
R Program
numbers <- factor(c(2, 3, 4, 5), levels = c(2, 3, 4, 5))
if (4 %% 2 == 0) {
print("4 is even")
} else {
print("4 is odd")
}
Output
4 is even