Home > Enterprise >  converting list of data frames into single data frame
converting list of data frames into single data frame

Time:02-20

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
  }
  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

CodePudding user response:

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)

CodePudding user response:

I am sure there is a better solution out there. The problem you can't use rbind is because you have differing colnames (e.g. T1, T2, T3...)

So my idea was to name each df in the list with

  names(table_list1) <- banner
  Map(cbind, table_list1, SampleID = names(table_list1))

then add new names to each dataframe:

  colnames <- c("name","Median","Mean", "N") 
  
  for (i in seq_along(table_list1)){
    colnames(table_list1[[i]]) <- colnames
  }

Now you can use your code:

  t2 <- do.call(rbind,table_list1)
  t2
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
  }
  names(table_list1) <- banner
  Map(cbind, table_list1, SampleID = names(table_list1))
  
  
  colnames <- c("name","Median","Mean", "N") 
  
  for (i in seq_along(table_list1)){
    colnames(table_list1[[i]]) <- colnames
  }
  
  t2 <- do.call(rbind,table_list1)
  t2
}
  name Median       Mean  N
T1.1 klick      6     6.1875 32
T1.2 Nemar  196.3 230.721875 32
T1.3 Wingo    123   146.6875 32
T1.4                           
T2.1 klick      6     6.1875 32
T2.2 Nemar  196.3 230.721875 32
T2.3 Wingo    123   146.6875 32
T2.4                           
T3.1 klick      6     6.1875 32
T3.2 Nemar  196.3 230.721875 32
T3.3 Wingo    123   146.6875 32
T3.4             
  •  Tags:  
  • r
  • Related