title: writing a function to calculate the mean of columns in a dataframe in R
body: <p>I have to calculate the mean of the columns in a dataframe by writing a function and then applying it.  I understand that this is easy to do with <code>mean</code> and <code>apply</code> but I need to write my own function.  I have made many attempts but cannot seem to get this right.  Below are 3 of my attempts.  I am a beginner at R.  I would greatly appreciate any suggestions.</p>
  
  <pre><code>mean_fun&lt;-function(x){
    mean_c[i]= sum(x[1:dim(x)],na.rm=TRUE)/length(x[1:dim(x)])
    return(mean_c[i])
  }
  
  
  mean_fun&lt;-function(x){
    for( i in 1:ncol(x)){
      s=sum(x[1:i],na.rm=TRUE)
      l=dim(x[1:i])
      mean_c=s/l
      return (mean_c)
    }
  
  
  mean_fun&lt;-function(x){
    x=rbind(x,newrow)
    for(i in 1:ncol(x)){
      x[newbottomrownumber,i]=sum[i]/length[i]}
    return(x[1303,])
  }
  </code></pre>