nlevels(x)
If the argument is not a factor, NA is returned.
factor
NA
The actual factor levels (if they exist) can be obtained with the levels function.
levels
nlevels(gl(3,7)) # = 3