title: R function applied on data frame grouped by multiple factors
body: <p>I have a data frame called subdata, with a dimension of 10299 x 81. Column 1 called "Subject" and column 2 called "Activity". I want to calculate the average of each column grouped by "Subject" and "Activity". </p>
  
  <p>Here are the functions I tried and none of them seems work so far. Finally I used colwise(mean) function, it seems work. I am new to R and just learned <code>sapply</code>, <code>lapply</code>, <code>tapply</code> functions and it seems mean function works in columns. </p>
  
  <p>Can anyone help me explain what does these error or warning message mean and if there a way to make theses functions work?</p>
  
  <p><strong>Use lapply function:</strong></p>
  
  <pre><code>newdata&lt;- subdata[, lapply(.SD, mean), by = c("Subject","Activity")]
  </code></pre>
  
  <p>The error message: </p>
  
  <pre><code>Error in `[.data.frame`(subdata, , lapply(.SD, mean), by = c("Subject",  : 
  unused argument (by = c("Subject", "Activity"))
  </code></pre>
  
  <p><strong>Use by function:</strong></p>
  
  <pre><code>newdata&lt;-by(subdata, list(subdata$Subject, subdata$Activity), mean)
  </code></pre>
  
  <p>I got warning message:</p>
  
  <pre><code>Warning messages:
  1: In mean.default(data[x, , drop = FALSE], ...) :
     argument is not numeric or logical: returning NA
  </code></pre>
  
  <p><strong>Then I tried ddply in plyr package</strong></p>
  
  <pre><code>ddply(subdata, .(Subject, Activity), mean)
  </code></pre>
  
  <p>I got the same warning message:</p>
  
  <pre><code>Warning messages:
  1: In mean.default(piece, ...) : argument is not numeric or logical: returning NA 0
  </code></pre>
  
  <p><strong>Finally I used colwise(mean)function, it seems work</strong></p>
  
  <pre><code>newdata&lt;-ddply(subdata, .(Subject, Activity), colwise(mean))
  </code></pre>