Learn R Programming

container (version 1.0.5)

rename: Rename Elements Safely

Description

Search for old name and replace it by new name. If either the old name does not exist or the name would result in a name-clash with an already existing name, an error is signaled.

Usage

rename(.x, old, new)

ref_rename(.x, old, new)

# S3 method for Container rename(.x, old, new)

# S3 method for dict.table rename(.x, old, new)

# S3 method for dict.table ref_rename(.x, old, new)

# S3 method for default rename(.x, old, new)

Value

For standard R vectors renames old to new and returns the renamed vector.

For Container, an object of class Container (or one of the respective derived classes).

For dict.table renames key old to new in place (i.e. by reference) and invisibly returns the dict.table() object.

Arguments

.x

dict.table object

old

character old name

new

character new name

Details

The passed old and new names can be vectors but always must have the same length and must be unique to prevent double-renaming.

rename uses copy semantics while ref_rename works by reference, that is, it renames elements in place.

Examples

Run this code

# Container
co = container(a = 1, b = 2, 3)
rename(co, c("a", "b"), c("a1", "y"))
print(co)
ref_rename(co, c("a", "b"), c("a1", "y"))
print(co)

# dict.table
dit = dict.table(a = 1, b = 2, c = 3)
rename(dit, c("a", "b"), c("a1", "y"))
print(dit)
ref_rename(dit, c("a", "b"), c("a1", "y"))
print(dit)

Run the code above in your browser using DataLab