Checking if the Object is a Factor in R Programming – is.factor() Function

is.factor() Function in detail

The is.factor() function in R is used to determine whether a given object is a factor. It returns TRUE if the object is a factor and FALSE otherwise.

Syntax:

is.factor(object)

Parameters:

  • object: The variable that needs to be checked.

Example 1: Checking a Factor Variable

# Creating a character vector
fruit <- c("Apple", "Banana", "Apple", "Orange")

# Converting the vector into a factor
fruit_factor <- factor(fruit)

# Checking if it is a factor
is.factor(fruit_factor)

Output:

[1] TRUE

Example 2: Checking a Non-Factor Variable

# Creating a numeric vector
numbers <- c(1, 2, 3, 4, 5)

# Checking if it is a factor
is.factor(numbers)

Output:

[1] FALSE

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *