rename: Rename columns

View source: R/rename.R

renameR Documentation

Rename columns

Description

rename() changes the names of individual variables using new_name = old_name syntax. rename_with() renames columns using a function.

Usage

rename(.data, ...)

rename_with(.data, .fn, .cols = everything(), ...)

Arguments

.data

A data.frame

...

For rename(): comma separated key-value pairs in the form of new_name = old_name to rename selected variables.

For rename_with(): additional arguments passed onto .fn.

.fn

A ⁠function()⁠ used to transform the selected .cols. Should return a character vector the same length as the input.

.cols

Columns to rename; defaults to all columns.

Value

A data.frame with the following properties:

  • Rows are not affected.

  • Column names are changed; column order is preserved.

  • data.frame attributes are preserved.

  • Groups are updated to reflect new names.

Examples

rename(mtcars, MilesPerGallon = mpg)
rename(mtcars, Cylinders = cyl, Gears = gear)
mtcars %>% rename(MilesPerGallon = mpg)

rename_with(mtcars, toupper)
rename_with(mtcars, toupper, starts_with("c"))


poorman documentation built on Nov. 2, 2023, 5:27 p.m.