How can I rename all columns of a data frame based on another data frame in R?
<p>I am very new to R.  I am trying to rename the columns of a data frame based on another dataframe. </p>

<p>Essentially My data looks like</p>

<p>DataFrame1</p>

<pre><code>A    B    C   D
1    2    3   4
</code></pre>

<p>I have another table that looks like this'
DataFrame2</p>

<pre><code>Col1    Col2
A       E
B       Q
C       R
D       Z
</code></pre>

<p>I want to rename the columns of my first data frame based on this table so that it will come out:</p>

<pre><code>E    Q    R    Z
1    2    3    4
</code></pre>

<p>I was trying a loop using the plyr library.  This is the command I tried:</p>

<pre><code>library(plyr)
for (i in names(DataFrame1[,3:336])) #renaming columns 3 to 336
    { 
        rename(DataFrame1,
         replace = c(i = DataFrame2[DataFrame2$Col1 == i, 2])) 
    }
</code></pre>

<p>My thinking was for each column in DataFrame1, rename the column with the lookup of the column in DataFrame2.</p>

<p>This produces N Rows of the error "The following <code>from</code> values were not present in <code>x</code>: i", where n is the number of rows present in DataFrame1</p>

<p>Thank you for any help you can offer!</p>