title: What's the idiomatic way to batch update a data frame using a custom mapping?
body: <p>I have a data frame that looks as follows:</p>
  
  <pre><code>test &lt;- data.frame(x=c('a','b','a','c','a','b','d'),y=rep('test',7),stringsAsFactors = FALSE)
  test
    x    y
  1 a test
  2 b test
  3 a test
  4 c test
  5 a test
  6 b test
  7 d test
  </code></pre>
  
  <p>Now I'd like to define a mapping like e.g. </p>
  
  <pre><code>mapping &lt;- list(a=1,b=2,c=3,d=4)
  mapping
  $a
  [1] 1
  $b
  [1] 2
  $c
  [1] 3
  $d
  [1] 4
  </code></pre>
  
  <p>and what I want is to batch update my data frame creating a new variable <code>z</code> so that wherever an 'a' is found the variable <code>z</code> is set to the value 1 according to the mapping (the mapping can be defined in any other way if simpler or better for a possible solution). </p>
  
  <p>My end result should be:</p>
  
  <pre><code>  x    y  z
  1 a test  1 
  2 b test  2 
  3 a test  1
  4 c test  3
  5 a test  1
  6 b test  2
  7 d test  4
  </code></pre>