- 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
How to create a subset for a factor level in an R data frame?
In data analysis, we often deal with factor variables and these factor variables have different levels. Sometimes, we want to create subset of the data frame in R for specific factor levels to analyze the data only for that particular level of the factor variable. This can be simply done by using subset function.
Example
Consider the below data frame −
> set.seed(99) > Factor<-rep(c("India","China","USA","UK","Canada"),times=4) > Percentage<-sample(1:100,20) > df<-data.frame(Factor,Percentage) > df Factor Percentage 1 India 48 2 China 33 3 USA 44 4 UK 22 5 Canada 62 6 India 32 7 China 13 8 USA 20 9 UK 31 10 Canada 68 11 India 9 12 China 82 13 USA 88 14 UK 30 15 Canada 86 16 India 84 17 China 95 18 USA 14 19 UK 4 20 Canada 78
Here, we have five levels of factor variable Factor. Now suppose we want to create a subset of Percentage for each of these levels then it can be done as shown below −
> India<-subset(df,Factor=="India") > India Factor Percentage 1 India 48 6 India 32 11 India 9 16 India 84 > UK<-subset(df,Factor=="UK") > UK Factor Percentage 4 UK 22 9 UK 31 14 UK 30 19 UK 4 > China<-subset(df,Factor=="China") > China Factor Percentage 2 China 33 7 China 13 12 China 82 17 China 95 > USA<-subset(df,Factor=="USA") > USA Factor Percentage 3 USA 44 8 USA 20 13 USA 88 18 USA 14 > Canada<-subset(df,Factor=="Canada") > Canada Factor Percentage 5 Canada 62 10 Canada 68 15 Canada 86 20 Canada 78
- Related Articles
- How to subset factor columns in an R data frame?
- How to create scatterplot for factor levels in an R data frame?
- How to create a new level using unique levels of a factor in R data frame?
- How to drop factor levels in subset of a data frame in R?
- How to set a level of a factor column in an R data frame to NA?
- Extract a particular level from factor column in an R data frame.
- How to select the first row for each level of a factor variable in an R data frame?
- How to create a subset of an R data frame based on multiple columns?
- How to create a line chart for a subset of a data frame using ggplot2 in R?
- How to subset a data frame by excluding a specific text value in an R data frame?
- How to create a new column with a subset of row sums in an R data frame?
- How to create table of two factor columns in an R data frame?
- How to create a subset of an R data frame having complete cases of a particular column?
- How to create a subset of a data frame in R without using column names?
- How to sort a numerical factor column in an R data frame?

Advertisements