R/gen_list-to-tib.R

Defines functions list_to_tib

Documented in list_to_tib

#' Make a tibble from a list - with names as a new column
#' 
#' This function just calls rbind on a whole list but will first
#' make a new column in the dataframe, corresponding to the names of the list
#' 
#' @param list the input list - should have all elements be a df
#' @param new_col_name the column name to correspond to list names
#' 
#' @export
#' @author Alex Barth
list_to_tib <- function(list, new_col_name = 'group') {
  r_list <- list
  for(i in 1:length(r_list)){
    r_list[[i]][[new_col_name]] <- names(r_list)[i]
  }
  rdf <- do.call(rbind, r_list)
  row.names(rdf) <- NULL
  return(as_tibble(rdf))
}
TheAlexBarth/EcotaxaTools documentation built on Nov. 14, 2024, 8:27 p.m.