- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Convert a data frame with grouping column into a list based on groups in R.
To convert a data frame with grouping column into a list based on groups, we can use split function.
For Example, if we have a data frame called df that contains a categorical column say Group and a numerical column say DV then we can convert df into a list based on groups in Group column by using the command as mentioned below −
split(df$DV,df1$Group).
Example 1
Following snippet creates a sample data frame −
Group<-sample(LETTERS[1:5],20,replace=TRUE) Response_var<-rpois(20,5) df1<-data.frame(Group,Response_var) df1
The following dataframe is created
Group Response_var 1 B 4 2 E 3 3 C 9 4 D 4 5 C 5 6 A 4 7 B 5 8 D 9 9 E 4 10 A 5 11 C 2 12 B 11 13 E 5 14 E 6 15 D 1 16 B 4 17 E 2 18 B 2 19 D 4 20 A 7
To split the df1 into a list based on Group column on the above created data frame, add the following code to the above snippet −
Group<-sample(LETTERS[1:5],20,replace=TRUE) Response_var<-rpois(20,5) df1<-data.frame(Group,Response_var) split(df1$Response_var,df1$Group) $A
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 4 5 7
To split the df1 into a list based on Group column on the above created data frame, add the following code to the above snippet −
Group<-sample(LETTERS[1:5],20,replace=TRUE) Response_var<-rpois(20,5) df1<-data.frame(Group,Response_var) split(df1$Response_var,df1$Group) $B
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 4 5 11 4 2
To split the df1 into a list based on Group column on the above created data frame, add the following code to the above snippet −
Group<-sample(LETTERS[1:5],20,replace=TRUE) Response_var<-rpois(20,5) df1<-data.frame(Group,Response_var) split(df1$Response_var,df1$Group) $C
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 9 5 2
To split the df1 into a list based on Group column on the above created data frame, add the following code to the above snippet −
Group<-sample(LETTERS[1:5],20,replace=TRUE) Response_var<-rpois(20,5) df1<-data.frame(Group,Response_var) split(df1$Response_var,df1$Group) $D
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 4 9 1 4
To split the df1 into a list based on Group column on the above created data frame, add the following code to the above snippet −
Group<-sample(LETTERS[1:5],20,replace=TRUE) Response_var<-rpois(20,5) df1<-data.frame(Group,Response_var) split(df1$Response_var,df1$Group) $E
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 3 4 5 6 2
Example 2
Following snippet creates a sample data frame −
Class<-sample(c("First","Second","Third","Fourth","Fifth"),20,replace=TRUE) Score<-sample(1:10,20,replace=TRUE) df2<-data.frame(Class,Score) df2
The following dataframe is created
Class Score 1 Third 6 2 Second 2 3 Fourth 3 4 Fifth 2 5 Second 3 6 Fifth 6 7 Fourth 2 8 First 4 9 Third 10 10 Third 5 11 Third 4 12 First 2 13 Third 6 14 Third 5 15 First 1 16 Fourth 4 17 Second 8 18 First 8 19 Second 7 20 Third 10
To split the df2 into a list based on Class column on the above created data frame, add the following code to the above snippet −
Class<-sample(c("First","Second","Third","Fourth","Fifth"),20,replace=TRUE) Score<-sample(1:10,20,replace=TRUE) df2<-data.frame(Class,Score) split(df2$Score,df2$Class) $Fifth
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 2 6
To split the df2 into a list based on Class column on the above created data frame, add the following code to the above snippet −
Class<-sample(c("First","Second","Third","Fourth","Fifth"),20,replace=TRUE) Score<-sample(1:10,20,replace=TRUE) df2<-data.frame(Class,Score) split(df2$Score,df2$Class) $First
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 4 2 1 8
To split the df2 into a list based on Class column on the above created data frame, add the following code to the above snippet −
Class<-sample(c("First","Second","Third","Fourth","Fifth"),20,replace=TRUE) Score<-sample(1:10,20,replace=TRUE) df2<-data.frame(Class,Score) split(df2$Score,df2$Class) $Fourth
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 3 2 4
To split the df2 into a list based on Class column on the above created data frame, add the following code to the above snippet −
Class<-sample(c("First","Second","Third","Fourth","Fifth"),20,replace=TRUE) Score<-sample(1:10,20,replace=TRUE) df2<-data.frame(Class,Score) split(df2$Score,df2$Class) $Second
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 2 3 8 7
To split the df2 into a list based on Class column on the above created data frame, add the following code to the above snippet −
Class<-sample(c("First","Second","Third","Fourth","Fifth"),20,replace=TRUE) Score<-sample(1:10,20,replace=TRUE) df2<-data.frame(Class,Score) split(df2$Score,df2$Class) $Third
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 6 10 5 4 6 5 10
- Related Articles
- How to remove rows from data frame in R based on grouping value of a particular column?
- Convert a numeric column to binary factor based on a condition in R data frame
- How to convert a data frame into two column data frame with values and column name as variable in R?
- How to assign a column value in a data frame based on another column in another R data frame?
- How to remove rows from an R data frame based on frequency of values in grouping column?
- How to select top rows of an R data frame based on groups of factor column?
- How to add a new column in an R data frame with count based on factor column?
- Replace each value in a column with the largest value based on a condition in R data frame.
- How to extract a data frame’s column value based on a column value of another data frame in R?
- Find the mean of multiple columns based on multiple grouping columns in R data frame.
- Convert list with varying number of elements into a data frame.
- Replace numerical column values based on character column values in R data frame.
- How to create bar chart based on two groups in an R data frame?
- How to remove rows based on blanks in a column from a data frame in R?
- How to convert first letter into capital in R data frame column?
