reverse_lookup: Swap key-value in lookup vector

View source: R/reverse_lookup.R

reverse_lookupR Documentation

Swap key-value in lookup vector

Description

Swap a key-value pair's key with value and vice-versa

Usage

reverse_lookup(x)

Arguments

x

a named vector, or something coercible to one, with a 1:1 key:value ratio

Details

This is a convenient way to "flip" the key-value relationship for a lookup vector. The specific use-case is for a simple key-value vector or list, which means a nested hierarchy or a 1:many relationship (key:value) will fail.

Value

A vector of the same length as x, with x's keys as value(s), and x's values as key(s)

Examples

lookup_list <- c(LETTERS[1:5])
names(lookup_list) <- c(letters[1:5])
reverse_lookup(lookup_list)
reverse_lookup(reverse_lookup(lookup_list)) #returns original

## Not run: 
reverse_lookup(LETTERS[1:5]) #missing names!
bad_list <- list(a = 1:2, b = 3, c = letters[1:5])
reverse_lookup(bad_list) #error
reverse_lookup(unlist(bad_list)) #no error, but probably not what you intended

## End(Not run)

slin30/wzMisc documentation built on Jan. 27, 2023, 1 a.m.