How to assign a subset from a data frame `a' to a subset of data frame `b'
<p>It might be a trivial question (I am new to R), but I could not find a answer for my question, either here in SO or anywhere else. My scenario is the following.</p>

<p>I have an data frame <code>df</code> and i want to update a subset <code>df$tag</code> values. <code>df</code> is similar to the following:</p>

<pre><code>id = rep( c(1:4), 3)
tag = rep( c("aaa", "bbb", "rrr", "fff"), 3)
df = data.frame(id, tag)
</code></pre>

<p>Then, I am trying to use <code>match()</code> to update the column <code>tag</code> from the subsets of the data frame, using a second data frame (e.g., aux) that contains two columns, namely, <code>key</code> and <code>value</code>. The subsets are defined by id = n, according to <code>n in unique(df$id)</code>. <code>aux</code> looks like the following:</p>

<pre><code> &gt; aux 
     key      value
   "aaa"  "valueAA"
   "bbb"  "valueBB"
   "rrr"  "valueRR"
   "fff"  "valueFF"
</code></pre>

<p>I have tried to loop over the data frame, as follows:</p>

<pre><code>for(i in unique(df$id)){
   indexer = df$id == i

   # here is how I tried to update the dame frame:
   df[indexer,]$tag &lt;- aux[match(df[indexer,]$tag, aux$key),]$value
}
</code></pre>

<p>The <strong>expected</strong> result was the <code>df[indexer,]$tag</code> updated with the respective values from <code>aux$value</code>.
The <strong>actual</strong> result was <code>df$tag</code> fulfilled with NA's. I've got no errors, but the following warning message:</p>

<blockquote>
  <p>In '[&lt;-.factor'('<em>tmp</em>', df$id == i, value = c(NA,  :
    invalid factor level, NA generated</p>
</blockquote>

<p>Before, I was using <code>df$tag &lt;- aux[match(df$tag, aux$key),]$value</code>, which worked properly, but some duplicated <code>df$tags</code> made the <code>match()</code> produce the misplaced updates in a number of rows. I also simulate the subsetting and it works fine. Can someone suggest a solution for this update?</p>

<p>UPDATE (how the final dataset should look like?):</p>

<pre><code> &gt; df
      id       tag
       1  "valueAA"
       2  "valueBB"
       3  "valueRR"
       4  "valueFF"
    (...)     (...)
</code></pre>

<p>Thank you in advance.</p>