R: Fast Renaming and Relabelling Objects

frename {collapse} | R Documentation

## Fast Renaming and Relabelling Objects

### Description

A fast substitute for `dplyr::rename`. `setrename` renames objects by reference. These functions also work with objects other than data frames that have a 'names' attribute. `relabel` and `setrelabel` do that same for labels attached to list elements / data frame columns.

### Usage

    frename(.x, ..., cols = NULL)
    rnm(.x, ..., cols = NULL)     # Shorthand for frename()

    setrename(.x, ..., cols = NULL)

    relabel(.x, ..., cols = NULL, attrn = "label")

    setrelabel(.x, ..., cols = NULL, attrn = "label")

### Arguments

`.x` | for `(f/set)rename`: an R object with a `"names"` attribute. For `(set)relabel`: a named list.
`...` | either tagged vector expressions of the form `name = newname` / `name = newlabel`, or a single function (+ optional arguments to the function) applied to all names/labels (of columns/elements selected in `cols`).
`cols` | If `...` is a function, select a subset of columns/elements to rename/relabel using names, indices, a logical vector or a function applied to the columns if `.x` is a list (e.g. `is.numeric`).
`attrn` | character. Name of attribute to store labels or retrieve labels from.

### Value

`.x` renamed / relabelled. `setrename` and `setrelabel` return `.x` invisibly.

### Note

Note that both `relabel` and `setrelabel` modify `.x` by reference. This is because labels are attached to columns themselves, making it impossible to avoid permanent modification by taking a shallow copy of the encompassing list / data.frame. On the other hand `frename` makes a shallow copy whereas `setrename` also modifies by reference.

### See Also

Data Frame Manipulation, Collapse Overview

### Examples

    ## Using tagged expressions
    head(frename(iris, Sepal.Length = SL, Sepal.Width = SW,
                       Petal.Length = PL, Petal.Width = PW))
    head(frename(iris, Sepal.Length = "S L", Sepal.Width = "S W",
                       Petal.Length = "P L", Petal.Width = "P W"))

    ## Using a function
    head(frename(iris, tolower))
    head(frename(iris, tolower, cols = 1:2))
    head(frename(iris, tolower, cols = is.numeric))
    head(frename(iris, paste, "new", sep = "_", cols = 1:2))

    ## Renaming by reference
    # setrename(iris, tolower)
    # head(iris)
    # rm(iris)

    ## Relabelling (by reference)
    # namlab(relabel(wlddev, PCGDP = "GDP per Capita", LIFEEX = "Life Expectancy"))
    # namlab(relabel(wlddev, toupper))

[Package collapse version 1.8.6 Index]