Why is this loop not working?

Why this loop doesn't works?

for (i in c(1,99)) {
  
  filtered_base <- 
    base %>%
    filter(triaje_anormal== i) 
    
    for (j in c("Anormales Grado I (menor)", 
                "Anormales Grado II (mayor)", 
                "Anormales No específico", 
                "Hallazgos normales", 
                "Hallazgos varios", 
                "NO", 
                "Sospecha de invasión") {
      
      filtered_base %>%
        filter(colpo_result_1== j) %>%
        select(biopsia_result_1 ) %>%
        tbl_summary()
    }
}

The output is this

> for (i in c(1,99)) {

It doesn't work because you overwrite filtered_base, but it's also unnecessary:

library(tidyverse)
library(gtsummary)

base %>% filter(triaje_anormal %in% c(1, 99)) %>% 
  filter(colpo_result_1 %in% c("Anormales Grado I (menor)", 
    "Anormales Grado II (mayor)", 
    "Anormales No específico", 
    "Hallazgos normales", 
    "Hallazgos varios", 
    "NO", 
    "Sospecha de invasión")) %>% 
  select(biopsia_result_1) %>% 
  tbl_summary()

Hi, these is not what I need. I need 14 tables, 7 of biopsia_result1 filter for each value of colpo_result_1 and filter by triaje_anormal=1 and 7 of biospia_result1 filter for each value of colpo_result1 and filter by triaje_anormal=99

In that case, group_split() is your friend::

base_list <- base %>%
  # filter only 1 or 99
  filter(triaje_anormal %in% c(1, 99)) %>%
  # make a list of two dataframes, one for triaje_anormal = 1 and the other for triaje_anormal = 99
  group_split(.by = triaje_anormal) %>%
  # group_split() every dataframe on baseList by colpo_result_1
 map(\(x) group_split(x, .by = colpo_result_1))

and then you can access each of the 14 dataframes as (for example)

base_list[[1]][[7]]
# or 
base_list[[2]][[5]]

This is a sign of a syntax error; in your case an unmatched open parenthesis in your second for loop.

However, fixing the syntax error won't make the tables visible since you're only creating them — and not printing them — inside a for loop. To see them, add print() after tbl_summary():

 filtered_base %>%
        filter(colpo_result_1== j) %>%
        select(biopsia_result_1 ) %>%
        tbl_summary()  %>%
        print()
2 Likes

Thanks! And after that how can I make the tables?
I try this and it doesn't work.

base_list <- 
  base %>%
  # filter only 1 or 99
  filter(triaje_anormal %in% c(1, 99)) %>%
  # make a list of two dataframes, one for triaje_anormal = 1 and the other for triaje_anormal = 99
  group_split(.by = triaje_anormal) %>%
  # group_split() every dataframe on baseList by colpo_result_1
  map(\(x) group_split(x, .by = colpo_result_1)) %>%
  select(biopsia_result_1 ) %>%
  tbl_summary()

Error in `select()`:
! `select()` doesn't handle lists.
Run `rlang::last_trace()` to see where the error occurred.

This topic was automatically closed 7 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.