Make 2 loops incorporating filtering by unique values of variables

I have this base:

structure(list(var1 = c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, 
TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE), 
    var2 = c(FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), cat1 = c("red", 
    "blue", "blue", "red", "blue", "red", "red", "blue", "blue", 
    "blue", "blue", "red", "red", "red", "red"), cat2 = c("car", 
    "bike", "bike", "moto", "car", "bike", "car", "moto", "bike", 
    "car", "moto", "bike", "moto", "bike", "car"), neutro = c("right", 
    "left", "left", "left", "left", "right", "left", "left", 
    "left", "right", "left", "left", "right", "left", "left")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -15L))

head(df)
# A tibble: 6 × 5
  var1  var2  cat1  cat2  neutro
  <lgl> <lgl> <chr> <chr> <chr> 
1 TRUE  FALSE red   car   right 
2 TRUE  TRUE  blue  bike  left  
3 FALSE TRUE  blue  bike  left  
4 FALSE FALSE red   moto  left  
5 TRUE  TRUE  blue  car   left  
6 FALSE FALSE red   bike  right 

And I need to create a loop where the variable "neutral" is crossed with the variables "var1" and "var2" separately but at the same time to get each table filtered by the unique values of the variables "cat1" and "cat2" separately.
I got to this point in the loop but now I don't know how to continue to incorporate the filtering for each of the values of the cat variables so I am doing it manually.

for(variable in c("var1", "var2")){
  
  print(variable)
  df %>%
    filter(cat1 == "blue" ) %>%
    select(neutro,
           variable) %>%
    mutate_all(as.factor) %>%
    tbl_summary(by= neutro,
                missing = "always",
                statistic = list(all_categorical() ~ "{n} ({p})")) %>% 
    add_p() %>%
    print()
}
df <- structure(list(var1 = c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, 
                        TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE), 
               var2 = c(FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, 
                        FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), cat1 = c("red", 
                                                                                   "blue", "blue", "red", "blue", "red", "red", "blue", "blue", 
                                                                                   "blue", "blue", "red", "red", "red", "red"), cat2 = c("car", 
                                                                                                                                         "bike", "bike", "moto", "car", "bike", "car", "moto", "bike", 
                                                                                                                                         "car", "moto", "bike", "moto", "bike", "car"), neutro = c("right", 
                                                                                                                                                                                                   "left", "left", "left", "left", "right", "left", "left", 
                                                                                                                                                                                                   "left", "right", "left", "left", "right", "left", "left")), class = c("tbl_df", 
                                                                                                                                                                                                                                                                         "tbl", "data.frame"), row.names = c(NA, -15L))



library(tidyverse)
library(gtsummary)


(filt_list <- list(cat1 = unique(df$cat1),
     cat2 = unique(df$cat2)))

for(variable in c("var1", "var2")){
  print(variable)
  
iwalk(filt_list,
     \(x,category){
       walk(x,\(filt){
         df |> filter(!!sym(category) == filt)  %>%
           select(neutro,
                  variable) %>%
           mutate_all(as.factor) %>%
           tbl_summary(by= neutro,
                       missing = "always",
                       statistic = list(all_categorical() ~ "{n} ({p})")) %>% 
           add_p() %>%
           modify_caption(paste(
                                variable,
                                category,
                                filt)) %>%
           print()
         })
     })
}

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.