R consists of various data types, and “factor” is one of them. You can use the factor type to represent categorical data such as gender (male or female), marital status(married, divorced, single, widowed), etc.
If you want to validate the data and ensure that you want to handle categorical data efficiently, you must first identify whether the input object is a factor, and that’s where is.factor() comes into the picture.
is.factor() Function
The is.factor() is a built-in R function that checks whether an input object is a factor. It checks whether a variable has the class “factor”, if it does, it returns TRUE; otherwise, it returns FALSE.
The most important usage of this function is that you can validate the variable before operating on it. This makes our code error-proof and efficient.
Syntax
is.factor(input_obj)
Parameters
Name | Value |
input_obj | It is an R object that will be checked for a type “factor”. |
Visual representation
Simple validation of a factor
You can create a new factor using the factor() function.
main_factor <- factor(c("low", "medium", "high"))
is.factor(main_factor) # Output: TRUE
Let’s check for a variable that’s not a factor.
non_factor <- c("apple", "banana", "cherry")
is.factor(non_factor) # Output: FALSE
Vector is not a factor, and hence it returns FALSE. You can check if a variable is a vector using is.vector() function.
Conditional Logic
One of the advantages of a function returning boolean values is that you can use it in “if-else” statements. Based on the boolean output, you can perform certain operations.
# Defining non-factor variable
non_factor <- c("apple", "banana", "cherry")
# Checking if the variable is a factor
if (is.factor(non_factor)) {
print("The variable is a factor")
} else {
print("The variable is not a factor")
}
Output
[1] "The variable is not a factor"
Lists
The list contains elements of different types but is not a type factor. So, if you pass it to the is.factor() function, it returns FALSE.
# Define a list
main_list <- list("pg", "vmm", "dltn")
# Check if the list is a factor
if (is.factor(main_list)) {
print("The list is a factor")
} else {
print("The list is not a factor")
}
Output
[1] "The list is not a factor"
Data Frames
The data frame itself is an R object with the class “data frame”. Hence, it is not a factor.
The data frame consists of rows and columns. Columns can have different data types so that individual columns can contain “factor” types, but overall, they are not factors.
If a specific column of the data frame is “factor” and you are checking for that column, then the function returns TRUE, but if you apply the whole data frame to the function, it returns FALSE.
df <- data.frame(
id = 1:3,
category = as.factor(c("X", "Y", "Z")),
value = c(10, 20, 30)
)
# Checking for the whole data frame
is.factor(df) # Output: FALSE
# Checking if a specific column is of type factor
is.factor(df$category) # Output: TRUE
is.factor(df$id) # Output: FALSE
You can see that when working with complex data types such as data frames, you must apply the is.factor() function to its individual elements or columns in this case.
as.factor() function
If you are operating on a vector and you want to convert it into a factor then you can use the as.factor() function. It will accept a vector as an argument and return a factor. Then, you can use is.factor() to check if it has a class “factor”.
# Defining a vector
main_vec <- c("krunal", "ankit", "rushabh")
# Check if the vector is a factor and convert if it is not
if (!is.factor(main_vec)) {
# Converting a vector into factor
main_vec <- as.factor(main_vec)
print("The vector has been converted to a factor")
} else {
print("The vector is already a factor")
}
# Printting the vector to verify
print(main_vec)
print(is.factor(main_vec))
Output
[1] "The vector has been converted to a factor"
[1] krunal ankit rushabh
Levels: ankit krunal rushabh
[1] TRUE
Putting is.factor() function in a conditional statement, we filtered out a vector and then used the as.factor() function to convert a vector into a factor and then display its value, and validate its data type.
That’s how you can use the combination of both functions to make your program more error-free.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.