title: Make a lookup table from a data.frame
body: <p>I have a <code>data.frame</code> which has only one unique non-<code>NA</code> value in all columns but one, which only has <code>NA</code>. </p>
  
  <pre><code>data &lt;- data.frame(A = c("egg", "egg"), B = c(NA, "bacon"), C = c("ham", "ham"), D = c(NA, NA))
  </code></pre>
  
  <p>How can I use it to create a lookup table of the form below?</p>
  
  <pre><code>lookup &lt;- make_lookup(key=unique_values(data), value=names(data))
  lookup[["egg"]] # returns "A"
  lookup[["bacon"]] # returns "B"
  lookup[["ham"]] # returns "C"
  lookup[["NA"]] # returns "D"
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Based on Frank's answer below, I'm trying to have my lookup table refer to multiple values.</p>
  
  <pre><code>keys &lt;- lapply(data, function(x) if(is.factor(x)) levels(x) else "bacon")
  vals &lt;- names(data)
  </code></pre>
  
  <blockquote>
    <p>keys</p>
  </blockquote>
  
  <pre><code>$A
  [1] "egg"
  
  $B
  [1] "bacon"
  
  $C
  [1] "ham"
  
  $D
  [1] "bacon"
  </code></pre>
  
  <blockquote>
    <p>vals</p>
  </blockquote>
  
  <pre><code>[1] "A" "B" "C" "D"
  </code></pre>
  
  <blockquote>
    <p>tapply(vals, keys, c)</p>
  </blockquote>
  
  <pre><code>Error in tapply(vals, keys, c) : arguments must have same length
  </code></pre>