title: How can I overwrite an object in R if I only know the name of the object as a character string?
body: <p>In my R function, I am dealing with a character string containing the name of an object that is sitting somewhere in the workspace. I would like to overwrite the object (e.g., convert the object to a matrix).</p>
  
  <p>However, I only know the name of the object as a character string, and I don't have the object reference. I know I can convert the character string to an object reference either by using the <code>get(x)</code> function (where <code>x</code> is the string referring to the object), or by something like <code>eval(as.name(x))</code>. However, this works only for accessing the object, not for overwriting the object.</p>
  
  <p>How can I achieve this? Here is some code:</p>
  
  <pre><code>myvector &lt;- 1:5              # my object
  x &lt;- "myvector"              # text representation of the object
  get(x) &lt;- as.matrix(get(x))  # my first attempt
  eval(as.name(x)) &lt;- as.matrix(eval(as.name(x)))  # second attempt
  </code></pre>
  
  <p>Note that the first line is not part of the function from which I want to overwrite this object in the workspace, so I cannot just write <code>myvector &lt;- as.matrix(myvector)</code>.</p>