creating function to convert list of tables into single data frame

I am trying to create a function which can produce a single dataframe from list of dataframes like here t1 is creating a sub tables and i want t_final to produce a single data frames from the list of tables created by t1.

table_list1[[i]] <- t1 is creating if list of tables and these tables should convert into list of tables with one blank row between them.

df <- mtcars

df1 <- subset(df, vs==1)
df2 <- subset(df, am==1)
df3 <- subset(df, gear==3)

df_list <- list(df1,df2,df3)
banner <- c("T1","T2","T3")


sub_fun<-function(db,var){
  var = rlang::parse_expr(var)
  
  df1<- db %>% filter(!is.na(!!var)) %>%   summarise(
    Median =quantile(!!var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[3],
    Mean =  mean(!! var, na.rm=TRUE),
    N = sum(!is.na(!!var)))
  df1
  
  }
  
func1<-function(db,list_var,....){
  
  for (d in 1:length(df_list)) {
    table_list1<-list()
    table_list<-list()
    for (i in 1:length(list_var)) {
      
      
      table_list[[i]]<-sub_fun(db, list_var[i])
      
      t1 <- do.call(rbind,table_list)
      
    }
 #after this i want to create a list of table produced by t1, and then #create single in final table "t_final" as list of tables    
    # #colnames(t1)[1] <- banner[[d]]
    # table_list1[[i]] <- t1
    # sep_line <- rep("", ncol(table_list1[[i]]))
    # table_list1 <- lapply(table_list1[[i]], function(x)x %>%
    #                         mutate(across(where(is.numeric), ~round(.x, 2)),
    #                                across(everything(), ~as.character(.x))) %>%
    #                         rbind(., sep_line))
    # 
    # t2 <- bind_rows(table_list1)
    # rownames(t2) <- NULL
    # t2 <- t2[-nrow(t2), ]
  t1 <- t_final  
  t_final
  
  }
}

debug(func1)
func1(db=df,list_var=c("cyl","disp","hp"))

The output should be look like

Perhaps you can adapt the solution I provided to when you asked a very similar question the other day ?
creating list of tables created by functions - General - RStudio Community

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