function in R Language is used to get the number of levels of a factor.**nlevels()**

Syntax:nlevels(x)

Parameters:

x:Factor Object

**Example 1:**

`# R program to get the number ` `# of levels of a factor ` ` ` `# Creating a factor ` `x <` `-` `gl(` `3` `, ` `2` `) ` `x ` ` ` `# Calling nlevels() function ` `# to get the number of levels ` `nlevels(x) ` |

*chevron_right*

*filter_none*

**Output:**

[1] 1 1 2 2 3 3 Levels: 1 2 3 [1] 3

**Example 2:**

`# R program to get the number ` `# of levels of a factor ` ` ` `# Creating a factor ` `gender <` `-` `factor(c(` `"female"` `, ` `"male"` `, ` `"male"` `, ` `"female"` `)); ` `gender ` ` ` `# Calling nlevels() function ` `# to get the number of levels ` `nlevels(gender) ` |

*chevron_right*

*filter_none*

**Output:**

[1] female male male female Levels: female male [1] 2

## Recommended Posts:

- Get or Set Levels of a Factor in R Programming - levels() Function
- Applying User-defined Functions on Factor Levels of Dataset in R Programming - by() Function
- Removing Levels from a Factor in R Programming - droplevels() Function
- Checking if the Object is a Factor in R Programming - is.factor() Function
- Convert a Vector into Factor in R Programming - as.factor() Function
- Convert an Unordered Factor to an Ordered Factor in R Programming - as.ordered() Function
- Check if a Factor is an Ordered Factor in R Programming - is.ordered() Function
- Convert Factor to Numeric and Numeric to Factor in R Programming
- Generate Factors with specified Levels in R Programming - gl() Function
- Create a Vector of Colors from a Vector of Gray Levels in R Programming - gray() or grey() Function
- Replace values of a Factor in R Programming - recode_factor() Function
- Performing Analysis of a Factor in R Programming - factanal() Function
- The Factor Issue in a DataFrame in R Programming
- Get the number of columns of an Object in R Programming - ncol() Function
- Get the number of rows of an Object in R Programming - nrow() Function
- Get the List of Arguments of a Function in R Programming - args() Function
- Get Exclusive Elements between Two Objects in R Programming - setdiff() Function
- Get or Set Dimensions of a Matrix in R Programming - dim() Function
- Get Indices of Specified Values of an Array in R Programming - arrayInd() Function
- Get Transpose of a Matrix or Data Frame in R Programming - t() Function

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.