Write a R program to find the levels of factor of a given vector

1. Introduction

This R program finds the levels of a factor of a given vector. Factor levels are the unique values in a vector that has been converted into a factor, which is useful in statistical modeling.

2. Program Steps

1. Create a vector.

2. Convert the vector into a factor.

3. Find and print the levels of the factor.

3. Code Program

# Step 1: Create a vector
vector <- c("high", "medium", "low", "high", "medium")

# Step 2: Convert the vector into a factor
factor_vector <- factor(vector)

# Step 3: Find the levels of the factor
levels_of_factor <- levels(factor_vector)

# Print the levels
print("Levels of the factor:")


Levels of the factor:
[1] "high"   "low"    "medium"


1. c("high", "medium", "low", "high", "medium"): Creates a vector with some repeated values.

2. factor(vector): Converts the vector into a factor. Factors are used to represent categorical data and can handle both string and integer data.

3. levels(factor_vector): Retrieves the unique levels from the factor.

4. print("Levels of the factor:"): Prints a message indicating the levels of the factor.

5. print(levels_of_factor): Displays the unique levels of the factor.