How to for loop rows into a group based on value and create a dataframe that i can export individually?

Viewed 24

I have a dataset that looks like this

ID|Fruit|Color    |
 1 Apple Red
 2 Apple Red 
 3 Pear  Yellow
 4 Banana Yellow

I would like to loop through the values based on column "Fruit" and create three dataframes

#df1
ID|Fruit|Color
 1 Apple Red
 2 Apple Red

#df2
ID|Fruit|Color
 3  Pear Yellow

#df3
ID|Fruit|Color
 4 Banana Yellow
1 Answers

We can use split

lst1 <- split(df, df$Fruit)
names(lst1) <- paste0('df', seq_along(lst1))
list2env(lst1, .GlobalEnv)
Related