flow_prog<-function(df,sngl,sngl_keep,mltp=NULL){
  # df is the data frame
  # sngl is a vector of variable names to exclude from the data frame one by one, prioritised
  # sngl_keep is a variable of names of the same length as sngl containing the vector names of cases to keep for flow analysis
  # mltp is a vector of variable names to count, can be one or more
  # first sngl, then mltp
  
  # all specified variables should be factors or "factorisable"

  # If categorical variables are decided, define a new variable. More elegant solution is desirable, but this works (!).
  
  # Dependencies
  library(dplyr)
  
  # Small, home made, general summary function with naming
  summary_list<-function(var){
    v<-factor(var)
    x<-levels(v)
    y<-summary(v)
    lst<-list(data.frame(matrix(y,nrow=length(x),byrow = TRUE,dimnames = list(x,deparse(substitute(var))))))  # deparse and substitue is used to get the name of the vector for naming the matrix. Nice!
    return(lst)
  }
  
  slist<-list(nrow(df))
  names(slist)[1]<-"nrow of provided data frame"

  # Handling sngl first
  for (i in 1:length(sngl)){
    z<-summary_list(df[,sngl[i]])
    colnames(z[[1]])<-sngl[i]
    slist[length(slist)+1] <- z
    names(slist)[length(slist)]<-sngl[i]
    df <- df %>% filter(df[,sngl[i]]==sngl_keep[i])
  }
  
  # Handling mltp
  
  if (!is.null(mltp)){
    for (i in 1:length(mltp)){
      z<-summary_list(df[,mltp[i]])
      colnames(z[[1]])<-mltp[i]
      slist[length(slist)+1] <- z
      names(slist)[length(slist)]<-paste0(mltp[i],"_for_",last(sngl),"==",last(sngl_keep))
    }
  }
  
  return(slist)
}


df<-dta
sngl<-c("mors_v1","drop")
sngl_keep<-c("no","yes")
mltp<-c("open_treat","wants_out","side_effect","side_effect2")
