Search code examples
rdata-manipulationdata-wrangling

How to apply the same functions to multiple datasets generated by different filter statements?


I have one df that will generate a few tibbles with the filter statement. Then I would like to apply a function, specifically rstatix::get_summary_stats to each tibble. I would also need to export them to an excel with unique names for the summary output.

I can do the below code for each filtered tibble. But need a quicker way to automate instead of doing it 4 times.

library(rstatix)
library(dplyr)
df <- as.data.frame(var1 = c("f","m","f","f"), var2 = c(2018,2019,2019,2018))

df_2018_f<-df%>%
   filter(var1 == "f",var2 == 2018)

df_2018_f_summary<-df_2018_f%>%
   get_summary_stats(type = "full")

write_xlsx(df_2018_f_summary,"df 2018 summary f.xlsx")

I would like to have four summary stats. First one is filtered by all the data from 2018 and f. Second one filtered by 2018, m. Third one is filtered by 2019, f. Fourth is filtered by 2019,m.


Solution

  • We may use

    library(purrr)
    library(dplyr)
    library(rstatix)
    library(stringr)
    library(writexl)
    df %>% 
      split(., .)%>% 
       imap(~ .x %>% get_summary_stats(type = "full") %>% 
        write_xlsx(., str_c("df", .y, ".xlsx")))