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

Syntax:levels(x)

Parameters:

x:Factor Object

**Example 1:**

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

*chevron_right*

*filter_none*

**Output:**

[1] female male male female Levels: female male [1] "female" "male"

**Example 2:**

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

*chevron_right*

*filter_none*

**Output:**

[1] female male male female Levels: female male [1] abc def def abc Levels: abc def

## Recommended Posts:

- Get the Number of Levels of a Factor in R Programming - nlevels() 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
- Applying User-defined Functions on Factor Levels of Dataset in R Programming - by() Function
- Removing Levels from a Factor in R Programming - droplevels() 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
- 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
- Get or Set Dimensions of a Matrix in R Programming - dim() Function
- Get or Set names of Elements of an Object in R Programming - names() Function
- Get or Set the Type of an Object in R Programming - mode() Function
- Get or Set the Structure of a Vector in R Programming - structure() Function
- Get the First parts of a Data Set in R Programming - head() Function
- Get the Last parts of a Data Set in R Programming - tail() Function
- Get the List of Arguments of a Function in R Programming - args() 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.