title: Unsplit reduced data table based on two factors in R
body: <p>Suppose I have a data frame in R where I would like to use 2 columns "factor1" and "factor2" as factors and I need to calculate mean value for all other columns per each pair of the above mentioned factors. After running the code below, the last line gives the following warnings:  </p>
  
  <pre><code>Warning messages:
  1: In split.default(seq_along(x), f, drop = drop, ...) :
    data length is not a multiple of split variable
  </code></pre>
  
  <p>...</p>
  
  <p>Why is it happening and what should I do to make it right?
  Thanks.</p>
  
  <p>Here is my code:</p>
  
  <pre><code># Create data frame
  myDataFrame &lt;- data.frame(factor1=c(1,1,1,2,2,2,3,3,3), factor2=c(3,3,3,4,4,4,5,5,5), val1=c(1,2,3,4,5,6,7,8,9), val2=c(9,8,7,6,5,4,3,2,1))  
  
  # Split by 2 columns (factors)
  splitDataFrame &lt;- split(myDataFrame, list(myDataFrame$factor1, mydataFrame$factor2))
  
  # Calculate mean value for each column per each pair of factors
  splitMeanValues &lt;- lapply(splitDataFrame, function(x) apply(x, 2, mean))
  
  # Combine back to reduced table whereas there is only one value (mean) per each pair of factors
  MeanValues &lt;- unsplit(splitMeanValues, list(unique(myDataFrame$factor1), unique(mydataFrame$factor2)))
  </code></pre>
  
  <p>EDIT1: Added data frame creation (see above)</p>