Home > Enterprise >  levels and nlevels in R
levels and nlevels in R

Time:11-05

I'm new to R, and I came upon levels and nlevels function. Can someone explain difference between these two and potential usage ?

Thanks in advance!

I tried both functions and did not understand output of nlevels, but did understand output of levels.

CodePudding user response:

Just run examples:

levels(iris$Species)
#> [1] "setosa"     "versicolor" "virginica"

nlevels(iris$Species)
#> [1] 3

Created on 2023-11-04 with reprex v2.0.2

The former gives the levels themselves, the latter how many levels, the count. So in the example, nlevels outputs the length of the vector output by the previously run levels, which is 3.
In plain English, the iris data set records measurements from 3 different species, "setosa", "versicolor" and "virginica".

  •  Tags:  
  • r
  • Related