R/new_name.R

Defines functions new_name

Documented in new_name

#' Generate column names for a data frame
#'
# -------------------------------------------------------------------------
#' `new_name()` generates unique names for additional data frame variables
#' ensuring they are not already present.
#'
# -------------------------------------------------------------------------
#' @param x A data frame.
#'
#' @param n Number of unique names to generate.
#'
# -------------------------------------------------------------------------
#' @return A character vector of unique names not already found in `x`.
#'
# -------------------------------------------------------------------------
#' @examples
#'
#' new_name(mtcars)
#' new_name(mtcars, 2)
#'
# -------------------------------------------------------------------------
#' @export
new_name <- function(x, n = 1L) {

    assert_data_frame(x)
    assert_scalar_whole(n)
    assert_positive(n)

    # TODO - I'm 99% sure we do not need to use make.names here but ...
    pattern <- rep("new", n)
    .possible_names <- function() {
        names <- basename(tempfile(pattern = pattern))
        make.names(names)
    }

    # loop until names not in use are found
    possible <- .possible_names()
    while (any(possible %in% names(x))) {
        possible <- .possible_names()
    }

    possible

}

Try the ympes package in your browser

Any scripts or data that you put into this service are public.

ympes documentation built on April 15, 2025, 1:17 a.m.