'converting list of data frames into single data frame

I have created a function which is calculating mean and median for list of columns and converting it into list of tables for multiple column variables.

I have tried rbind, rbindlist, but nothing is working.

t1 <- do.call(rbind, table_list)
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_name){
  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)))
  df<- df1 %>% mutate(" "=!!var_name,
                                       Median = Median,
                                       Mean = Mean,)
  df <- df %>% select(" ",everything(),N)
  df
  
  }
  
func1<-function(db,list_var,var_name_list,....){
  table_list1<-list()
  for (d in 1:length(df_list)) {
  
    table_list<-list()
    for (i in 1:length(list_var)) {
      
      
      table_list[[i]]<-sub_fun(db, list_var[i],var_name_list[i])
      
      t1 <- do.call(rbind,table_list)
      
    }
    
    colnames(t1)[1] <- banner[[d]]
    t1 <- t1 %>%
      add_row() %>%
      mutate_all(~replace(., is.na(.), ""))
    
    table_list1[[d]] <- t1
  }
#here the actual question is how i can convert the list of dataframes in table_list1 to single dataframe.
  t2 <- do.call(rbind,table_list1)
  t2
  
}

debug(func1)
func1(db=df,list_var=c("cyl","disp","hp"),var_name_list=c("klick","Nemar","Wingo"))

The output table t2 should be look like

enter image description here

r


Solution 1:[1]

Use do.call :

df <- mtcars

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

df_list <- list(df1,df2,df3)
do.call(rbind, df_list)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 slowowl