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

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

 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)


## Output

If you execute all the above given snippets as a single program, it generates the following Output −

 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)


## Output

If you execute all the above given snippets as a single program, it generates the following Output −

 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)

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

 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)


## Output

If you execute all the above given snippets as a single program, it generates the following Output −

 6 10 5 4 6 5 10