- 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 split a data frame by column in R?
If we have a data frame column that contains some duplicate values or represent categories then we might want to split the data frame based on that column.
For example, if we have a data frame called df that contains a column say Col then we can split the data frame by Col by using the command given below −
split(df,df$Col)
Example 1
Following snippet creates a sample data frame −
Group<-sample(c("Male","Female"),20,replace=TRUE) Score<-rpois(20,8) df1<-data.frame(Group,Score) df1
The following dataframe is created −
Group Score 1 Male 8 2 Female 6 3 Female 5 4 Female 7 5 Female 12 6 Female 10 7 Female 9 8 Male 9 9 Male 10 10 Female 13 11 Female 4 12 Male 11 13 Female 5 14 Male 6 15 Female 9 16 Male 9 17 Female 5 18 Female 8 19 Male 3 20 Female 5
To split df1 based on Group column, add the following code to the above snippet −
Group<-sample(c("Male","Female"),20,replace=TRUE) Score<-rpois(20,8) df1<-data.frame(Group,Score) split(df1,df1$Group)
Output
If you execute all the above given snippets as a single program, it generates the following output −
$Female Group Score 2 Female 6 3 Female 5 4 Female 7 5 Female 12 6 Female 10 7 Female 9 10 Female 13 11 Female 4 13 Female 5 15 Female 9 17 Female 5 18 Female 8 20 Female 5 $Male Group Score 1 Male 8 8 Male 9 9 Male 10 12 Male 11 14 Male 6 16 Male 9 19 Male 3
Example 2
Following snippet creates a sample data frame −
Class<-sample(c("I","II","III"),20,replace=TRUE) Number_of_Customers<-sample(1:10,20,replace=TRUE) df2<-data.frame(Class,Number_of_Customers) df2
The following dataframe is created −
Class Number_of_Customers 1 II 2 2 I 10 3 III 2 4 III 4 5 III 5 6 III 7 7 III 10 8 II 4 9 II 9 10 I 7 11 III 4 12 I 1 13 I 1 14 I 1 15 III 5 16 II 5 17 II 9 18 II 8 19 II 8 20 I 9
To split df2 based on Class column, add the following code to the above snippet −
Class<-sample(c("I","II","III"),20,replace=TRUE) Number_of_Customers<-sample(1:10,20,replace=TRUE) df2<-data.frame(Class,Number_of_Customers) split(df2,df2$Class)
Output
If you execute all the above given snippets as a single program, it generates the following output −
$I Class Number_of_Customers 2 I 10 10 I 7 12 I 1 13 I 1 14 I 1 20 I 9 $II Class Number_of_Customers 1 II 2 8 II 4 9 II 9 16 II 5 17 II 9 18 II 8 19 II 8 $III Class Number_of_Customers 3 III 2 4 III 4 5 III 5 6 III 7 7 III 10 11 III 4 15 III 5
- Related Articles
- How to unsplit a split in R data frame?
- How to split a data frame using row number in R?
- How to match a column in a data frame with a column in another data frame in R?
- How to scale the R data frame by excluding a particular column?
- How to repeat column values in R data frame by values in another column?
- How to subset a data frame by excluding a column using dplyr in R?
- How to split a big data frame into smaller ones in R?
- How to split a data frame in R into multiple parts randomly?
- How to split a data frame in R with conditional row values?
- How to split month and year from 6-digit numbers in an R data frame column?
- How to drop data frame columns in R by using column name?
- How to divide each value in a data frame by column total in R?
- How to repeat a column of a data frame and join it with another data frame in R by rows?
- How to standardized a column in an R data frame?
- How to find the row sums by excluding a column in R data frame?
