invert: Invert a structure by swapping keys and values

Description Usage Arguments Details Value Note Examples

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)

Example output

  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
"a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" 
 21  22  23  24  25  26 
"u" "v" "w" "x" "y" "z" 

searchable documentation built on May 1, 2019, 9:45 p.m.