Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

r
communitycreator

How to obtain the length of a factor in R?

Onyejiaku Theophilus Chidalu

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)
Implementing the length() function

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.

RELATED TAGS

r
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring