title: Unsure how to pass column variable to mean() in r
body: <p>Am a newb.  Trying to code for a program.   I have a multi-column data frame.  I want to calc the mean of a column.   I want to pass to the mean() function the name of the column that i want to use for mean calc.   I have tried to pass it a character string that uses the $ symbol.  It seems R doesnt allow the string passed to be a character and wants a logical or numeric when $ is used to define the column name.   Net, am stuck.  Is there another way to do this?  Am suggestions would be appreciated.  Code and results are below</p>
  
  <pre><code>&gt; ## df.final is the name of the dataframe
  
  &gt; car.type        &lt;- "ford"
  &gt; col.name        &lt;- paste("df.final","$", car.type, sep = "")
  
  &gt; print(col.name)
  [1] "df.final$ford"
  
  &gt; mean(col.name, na.rm = TRUE)
  [1] NA
  Warning message:
  In mean.default(col.name, na.rm = TRUE) :
  argument is not numeric or logical: returning NA
  
  &gt; mean(df.final$ford, na.rm = TRUE)
  [1] 3.14
  </code></pre>