R Program to find the levels of factor of a given vector

July 6, 2021, Learn eTutorial

How to find the levels of a factor of a given vector Using the R program?

In this R program, we are using these built-in function levels(factor()) for this calculation. Here we are passing the values to the built-in functions directly. The levels(factor()) function in R computes the levels of factors of the vector in a single function.

Using the function factor() we can create a factor of the vector and by using the level() function we can find levels of a factor. Factors are stored as integer vectors and which is closely related to vectors. In this R program, we are using the variable 'v' for storing the vector.


STEP 1: Assign variable v with vector values

STEP 2: Use the built-in functions

STEP 3: First print the original vector values

STEP 4: levels(factor(v) with an argument as v to find levels of factors

STEP 5: print the result of the function


R Source Code

                                          v = c(1, 2, 3, 3, 4, NA, 3, 2, 4, 5, NA, 5)
print("The original vector is:")
print("Levels of factor of the vector:")



[1] "The original vector is:"
[1]  1  2  3  3  4 NA  3  2  4  5 NA  5
[1] "Levels of factor of the vector:"
[1] "1" "2" "3" "4" "5"