- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Extract a particular level from factor column in an R data frame.
To extract a particular level from factor column in an R data frame, we can use levels function with the number of the factor.
For Example, if we have a data frame called df that contains a factor column say F then we can find the third level in F by using the below given command −
levels(df$F)[2]
Example 1
Following snippet creates a sample data frame −
Grp<-factor(sample(LETTERS[1:4],20,replace=TRUE)) df1<-data.frame(Grp) df1
The following dataframe is created
Grp 1 D 2 C 3 C 4 D 5 B 6 C 7 D 8 A 9 B 10 C 11 B 12 C 13 B 14 B 15 C 16 A 17 C 18 B 19 B 20 D
To find the second level in Grp column of df1 on the above created data frame, add the following code to the above snippet −
Grp<-factor(sample(LETTERS[1:4],20,replace=TRUE)) df1<-data.frame(Grp) levels(df1$Grp)[2]
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "B"
To find the fourth level in Grp column of df1 on the above created data frame, add the following code to the above snippet −
Grp<-factor(sample(LETTERS[1:4],20,replace=TRUE)) df1<-data.frame(Grp) levels(df1$Grp)[4]
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "D"
Example 2
Following snippet creates a sample data frame −
Class<-factor(sample(c("First","Second","Third"),20,replace=TRUE)) df2<-data.frame(Class) df2
The following dataframe is created
Class 1 Third 2 Second 3 First 4 Third 5 Third 6 Third 7 First 8 Third 9 Third 10 Second 11 Third 12 First 13 Second 14 First 15 First 16 Third 17 First 18 First 19 Third 20 Second
To find the second level in Class column of df2 on the above created data frame, add the following code to the above snippet −
Class<-factor(sample(c("First","Second","Third"),20,replace=TRUE)) df2<-data.frame(Class) levels(df2$Class)[2]
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "Second"
To find the first level in Class column of df2 on the above created data frame, add the following code to the above snippet −
Class<-factor(sample(c("First","Second","Third"),20,replace=TRUE)) df2<-data.frame(Class) levels(df2$Class)[1]
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "First"
To find the third level in Class column of df2 on the above created data frame, add the following code to the above snippet −
Class<-factor(sample(c("First","Second","Third"),20,replace=TRUE)) df2<-data.frame(Class) levels(df2$Class)[3]
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "Third"
Example 3
Following snippet creates a sample data frame −
Category<-factor(sample(c("Low","Medium","High"),20,replace=TRUE)) df3<-data.frame(Category) df3
The following dataframe is created
Category 1 Low 2 Medium 3 Medium 4 Medium 5 Low 6 High 7 Low 8 Medium 9 Low 10 High 11 High 12 Medium 13 High 14 Medium 15 Medium 16 High 17 Low 18 High 19 High 20 Low
The levels will be sorted alphabetically hence the levels will be extracted based on first alphabet.
To find the first level in Category column of df3 on the above created data frame, add the following code to the above snippet −
Category<-factor(sample(c("Low","Medium","High"),20,replace=TRUE)) df3<-data.frame(Category) levels(df3$Category)[1]
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "High"
To find the second level in Category column of df3 on the above created data frame, add the following code to the above snippet −
Category<-factor(sample(c("Low","Medium","High"),20,replace=TRUE)) df3<-data.frame(Category) levels(df3$Category)[2]
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "Low"
To find the third level in Category column of df3 on the above created data frame, add the following code to the above snippet −
Category<-factor(sample(c("Low","Medium","High"),20,replace=TRUE)) df3<-data.frame(Category) levels(df3$Category)[3]
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "Medium"
- Related Articles
- How to extract the factor levels from factor column in an R data frame?
- How to extract a particular value based on index from an R data frame column?
- How to extract only factor columns name from an R data frame?
- How to set a level of a factor column in an R data frame to NA?
- How to extract a single column of an R data frame as a data frame?
- How to extract the first digit from a character column in an R data frame?
- How to create a subset for a factor level in an R data frame?
- How to sort a numerical factor column in an R data frame?
- Extract columns with a string in column name of an R data frame.
- How to extract columns based on particular column values of an R data frame that match\na pattern?
- How to check whether a particular word exists in an R data frame column?
- How to remove a column from an R data frame?
- How to add a new column in an R data frame with count based on factor column?
- How to extract the first highest occurring value in an R data frame column?
- How to find the most frequent factor value in an R data frame column?
