I have a dataframe with 3 columns ;Type, year, and number. I need to create dataframe for each type. There are 4 types altogether in the main dataframe.
df<-data_frame(type=c('a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d'),
year=c('2021','2022','2021','2022','2021','2022','2021','2022','2021','2022','2021','2022','2021','2022','2021','2022'),
number=c(12,10,52,65,78,65,84,1,6,2,8,3,5,32,45,96))
I need to filter from the type and create a sub dataframe for each type. I tried using a for loop but it is not working.
type_list<-as.character(df %>%
distinct(type) %>%
pull()
)
for (i in seq_along(type_list)) {
type_list[i]<-df %>%
filter(type==type_list[i])
}