Invert a structure by swapping keys and values

Share:

Description

Invert a structure by swapping keys and values

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
invert(x)

## S4 method for signature 'vector'
invert(x)

## S4 method for signature 'Searchable'
invert(x)

## S4 method for signature 'list'
invert(x)

Arguments

x

object to invert

Details

Inverts named vectors

Value

A character vector in which the names are the former valeus.

Note

- currently applies to atomic vectors only - apply to list (recursive structures), data.frames, matrices and arrays - invert might be an ambiguous name ... call it swap_kv? kvswap? swapKV? swapNV?

Examples

1
2
3
4
5
6
v <- 1:26
 names(v) <- letters

 invert(v)

 l <- as.list(v)