R Factors


R Factors

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.



Initializing Factors

  1. Create a character vector representing categorical data.
  2. Define the levels for the factor.
  3. Create the factor using the factor() function.
  4. 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


Accessing Factor Levels

  1. Create a factor variable.
  2. Use the levels() function to access the levels of the factor.
  3. 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"


Modifying Factor Levels

  1. Create a factor variable.
  2. Modify the levels of the factor using the levels() function.
  3. 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


Using Factors in Conditional Statements

  1. Create a factor variable.
  2. Use an if statement to check if the factor contains a specific level.
  3. 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


Checking if a Factor Contains a Specific Level

  1. Create a factor variable.
  2. Use an if statement to check if the factor starts with a specific level.
  3. 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


Using Factors to Check Even Numbers

  1. Create a numeric vector and convert it to a factor.
  2. Use an if statement to check if a number is even.
  3. 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