# 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"


Updated on: 09-Nov-2021

1K+ Views