R/rename.R

Defines functions .rename rename.data.frame rename.grouped_tt rename.tidytable rename

Documented in rename

#' Rename variables by name
#'
#' @description
#' Rename variables from a data.table.
#'
#' @param .df A data.frame or data.table
#' @param ... `new_name = old_name` pairs to rename columns
#'
#' @export
#'
#' @examples
#' df <- data.table(x = 1:3, y = 4:6)
#'
#' df %>%
#'   rename(new_x = x,
#'          new_y = y)
rename <- function(.df, ...) {
  UseMethod("rename")
}

#' @export
rename.tidytable <- function(.df, ...) {
  .rename(.df, ...)
}

#' @export
rename.grouped_tt <- function(.df, ...) {
  # Ensure "groups" attribute has new names
  groups <- group_vars(.df)
  groups <- select(.df, all_of(groups))
  groups <- names(.rename(groups, ..., .strict = FALSE))

  out <- ungroup(.df)
  out <- rename(out, ...)

  group_by(out, all_of(groups))
}

#' @export
rename.data.frame <- function(.df, ...) {
  .df <- as_tidytable(.df)
  rename(.df, ...)
}

.rename <- function(.df, ..., .strict = TRUE) {
  locs <- eval_rename(expr(c(...)), .df, strict = .strict)

  names <- names(.df)
  names[locs] <- names(locs)

  set_col_names(.df, names)
}

Try the tidytable package in your browser

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

tidytable documentation built on Sept. 11, 2024, 8:05 p.m.