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 objectHeight <- factor(c("Tall", "Average", "Short", "Tall"))# implementing the length() functionlength(Height)
Code explanation
- Line 2: We create a factor object
Heightusing thefactor()function. - Line 5: We obtain the number of items in the factor
Heightusing thelength()function.