Get the Number of Levels of a Factor in R Programming – nlevels() Function Last Updated : 16 Jun, 2020 Improve Improve Like Article Like Save Share Report nlevels() function in R Language is used to get the number of levels of a factor. 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) 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) Output: [1] female male male female Levels: female male [1] 2 Like Article Suggest improvement Previous R Programming Exercises, Practice Questions and Solutions Next Get or Set Levels of a Factor in R Programming - levels() Function Share your thoughts in the comments Add Your Comment Please Login to comment...