in R: replace values in a data frame with indices from a matching vector
<p>Sorry if this title is confusing, but I can't quite figure out how to word this question, which might be why I can't seem to find the right function. </p>

<p>Basically, say I have this:</p>

<pre><code>articles &lt;- c(237, 278, 354, 600)
ind &lt;- seq(1, length(articles))
DF &lt;- data.frame(x=c(237, 237, 278, 278, 278, 354, 600, 600, 600),
             y=rnorm(9))
</code></pre>

<p>I want to replace all of the values in DF$x with the corresponding <em>index</em> from the <code>articles</code> vector. As in , I would like <code>237</code> to become <code>1</code> and <code>278</code> to become <code>2</code> etc.</p>

<p>I have built a for loop that does it, but my real data.frame is much much larger and I feel like there must be a more efficient way to accomplish this. Here is my for loop, so that you can see the end result that I want:</p>

<pre><code>for (i in 1:length(articles)) {
  DF[DF$x==articles[i], 1] &lt;- ind[i]
}
</code></pre>

<p>I looked at the <code>replace</code> function, but that doesn't seem to do it. Also, in reality, this is a data.table (from the {data.table} package), not a data.frame. I can obviously convert it to a data.frame if necessary, but if there is a more efficient way to do this within the data.table package that would be awesome.</p>

<p>Thanks so much.
Seth</p>