R Basic

What is is.factor() Function in R

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.

Recent Posts

How to Remove Single and Multiple Columns from Data Frame in R

DataFrames are like tables that contain rows and columns. Each column can have a different…

11 hours ago

How to Convert Date to Numeric in R

Dates in R are stored as the number of days since 1970-01-01, so converting a…

2 days ago

How to Create a Data Frame from Vectors in R

In R, you can think of a vector as a series of values in a…

2 weeks ago

R dplyr::filter() Function: Complete Guide

The dplyr filter() function in R subsets a data frame and retains all rows that…

2 weeks ago

R distinct() Function from dplyr

The dplyr::distinct() function in R removes duplicate rows from a data frame or tibble and keeps…

2 weeks ago

How to Remove NA Values from Data Frame in R

NA values are missing values. They are somehow absent from a data frame. Before creating…

3 weeks ago