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

Updated on: 01-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements