R/addNameCol.R

Defines functions add_name_column

Documented in add_name_column

#' Add list name as a column to a list of data.frames.
#'
#' @param dlist named list of data.frames
#' @param destinationColumn character, name of new column to add
#' @return list of data frames, each of which as the new destinationColumn.
#'
#' @examples
#'
#' dlist <- list(a = data.frame(x = 1), b = data.frame(x = 2))
#' add_name_column(dlist, 'name')
#'
#' @export
#'
add_name_column <- function(dlist, destinationColumn) {
  res <- dlist
  # since res list is pre-allocated, re-assigning into it
  # should be fast.
  # Please see: https://win-vector.com/2015/07/27/efficient-accumulation-in-r/
  for(ni in names(dlist)) {
    vi <- dlist[[ni]]
    vi[[destinationColumn]] <- ni
    res[[ni]] <- vi
  }
  res
}
WinVector/wrapr documentation built on Aug. 29, 2023, 4:51 a.m.