Problem:
Check the different Levels of a Factor Column in R
Solution:
The Elements of a Factor are called Levels.
Suppose, we have loaded a DataSet into a DataFrame in R. Now, we need to know the Data Types of the Different Columns and their values or number of records for each Level. This can be achieved by the below piece of code. For this example, we will use the “iris” Dataset.
Code:
Output |
So, for the above example, we can see the iris Dataframe has 5 variables.
1. Sepal.Length – A Number Column
2. Sepal.Width – A Number Column
3. Petal.Length -A Number Column
4. Petal.Width – A Number Column
5. Species – A Factor Column
We can use the str() function to check the structure of the data frame.
Now, if we want to check the possible values for the column Species, we can use the levels() function.
To check how many records are there for each species, we can use the table() function.
You may also like:
Thank You!