title: How to use a string as a formula in r
body: <p>I'm trying to do an ANOVA of all of my data frame columns against <code>time_of_day</code> which is a factor. The rest of my columns are all doubles and of equal length. </p>
  
  <pre><code>x = 0
  pdf("Time_of_Day.pdf")
  for (i in names(data_in)){
    if(x &gt; 9){
      test &lt;- aov(paste(i, "~ time_of_day"), data = data_in)
    }
    x = x+1
  }
  dev.off()
  </code></pre>
  
  <p>Running this code gives me this error:</p>
  
  <pre><code>Error: $ operator is invalid for atomic vectors
  </code></pre>
  
  <p>Where is my code calling <code>$</code>? How can I fix this? Sorry, I'm new to r and am quite lost.</p>
  
  <p>My research question is to see if time of day has an affect on brain volume at different ROIs in the brain. Time of day is divided into three categories of morning, afternoon or night.</p>
  
  <p>Edit: SOLVED
   treating the string as a formula will allow this to run although I have been advised to not have this many independent values as it will inflate the statistical results of the model. I am not removing this incase someone has a similar problem with the <code>aov()</code> call.</p>
  
  <pre><code>x = 0
  pdf("Time_of_Day.pdf")
  for (i in names(data_in)){
    if(x &gt; 9){
      test &lt;- aov(as.formula(paste(i, "~ time_of_day")), data = data_in)
    }
    x = x+1
  }
  dev.off()
  </code></pre>