How to obtain the length of a factor in R?

Overview

A factor in R is used to categorize a given data. The length of a factor in R is the number of items present in the factor.

To obtain the length or number of items present in a factor, we use the length() function.

Syntax

length(factor)

Parameter value

The length() function takes the factor object as its single parameter value.

Return value

The length() function returns the length of a given factor.

Code example

# creating a factor object
Height <- factor(c("Tall", "Average", "Short", "Tall"))
# implementing the length() function
length(Height)

Code explanation

  • Line 2: We create a factor object Height using the factor() function.
  • Line 5: We obtain the number of items in the factor Height using the length() function.

Free Resources