I have many labels under a variable for which I want to filter on that label and create new separate data frames for each label. I’m interested in the best way of doing this. Currently I am trying to do this using a for loop. (In R, how do I create a df for each “i” and save it to a new variable, preferably by the name of “i”?)
I will then be using the output of each new df to run it through another function that creates charts and figures for each of these 8 separate programs.
In the MRE, I am filtering by each cylinder category (4,6,8) and creating a new data frame for each cylinder.
This would be cyl_4, cyl_6, cyl_8 would be the output I want.
filter_cyl = function(cylinder){
mtcars%>%filter(cyl == cylinder)
}
cyl_4 = filter_cyl(4)
cyl_6 = filter_cyl(6)
cyl_8 = filter_cyl(8)
Obviously, below only gets me the last data frame.
for (i in unique(mtcars$cyl)){
new_df = filter_cyl(i)
}