title: How do I create a string containing quotes and then parse and evaluate it?
body: <p>I have a data frame with 3 years worth of sales data that I'm trying to convert to a time series. Manually creating subsets for each of the 36 months:</p>
  
  <pre><code>mydfJan2011 &lt;- subset(myDataFrame, 
                        as.Date("2011-01-01") &lt;= myDataFrame$Dates &amp; 
                        myDataFrame$Dates &lt;= as.Date("2011-01-31"))
  </code></pre>
  
  <p>...</p>
  
  <pre><code>mydfDec2013 &lt;- subset(myDataFrame, 
                        as.Date("2013-12-01") &lt;= myDataFrame$Dates &amp; 
                        myDataFrame$Dates &lt;= as.Date("2013-12-31"))
  </code></pre>
  
  <p>and then summing them up and putting them into a vector</p>
  
  <pre><code>counts[1] &lt;- sum(mydfJan2011$itemsSold)
  </code></pre>
  
  <p>...</p>
  
  <pre><code>counts[36] &lt;- sum(mydfDec2013$itemsSold)) 
  </code></pre>
  
  <p>to get the values for the time series works fine, but I'd like to make it a little more automatic as I have to create more than one time series, so I'm trying to turn it into a loop. </p>
  
  <p>In order to do that, I need to create a string with a subset command like this:</p>
  
  <pre><code>"subset(myDataFrame, 
          as.Date("2011-01-01") &lt;= myDataFrame$Dates &amp; 
          myDataFrame$Dates &lt;= as.Date("2011-01-31"))"
  </code></pre>
  
  <p>But when I use paste, the result is this:</p>
  
  <pre><code>myString
  &gt;"subset(myDataFrame, as.Date(\"2011-02-01\") &lt;= myDataFrame$Dates &amp; myDataFrame$Dates &lt;= as.Date(\"2011-02-28\"))"
  </code></pre>
  
  <p>and </p>
  
  <pre><code> eval(parse(text = myString))
  </code></pre>
  
  <p>results in the following error message:</p>
  
  <pre><code>Error in charToDate(x) : 
    character string is not in a standard unambiguous format
  </code></pre>
  
  <p>whereas just typing in the command (without escapes) results in the subset I'm trying to create.</p>
  
  <p>I've tried playing around with single and double quotes, substitute and deparse, but none of it results in any kind of subset of my data frame.</p>
  
  <p>Any suggestions?</p>
  
  <p>Even another way of splitting up the data by month and summing it up would be welcome.  </p>
  
  <p>Thanks,
  Signe</p>