View source: R/reverse_lookup.R
reverse_lookup | R Documentation |
Swap a key-value pair's key with value and vice-versa
reverse_lookup(x)
x |
a named vector, or something coercible to one, with a 1:1 key:value ratio |
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.
A vector of the same length as x, with x's keys as value(s), and x's values as key(s)
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.