title: Kruskal - Wallis p-value matrix for data subsets with R
body: <p>Consider a dataset <code>Data</code> which has several factor and several numerical continuous variables. Some of these variables, let's say <code>slice_by_1</code> (with classes "Male", "Female") and <code>slice_by_2</code> (with classes "Sad", "Neutral", "Happy"), are used to 'slice' data into subsets. For every subset Kruskal-Wallis test should be run on variables <code>length</code>, <code>preasure</code>,<code>pulse</code> each grouped by the other factor variable called <code>compare_by</code>. Is there a quick way in R to accomplish this task and put calculated p values to a matrix?</p>
  
  <p>I used <code>dplyr</code> package to prepare data.</p>
  
  <p><strong>Sample dataset:</strong></p>
  
  <pre><code>library(dplyr)
  set.seed(123)
  Data &lt;- tbl_df(
     data.frame(
         slice_by_1 = as.factor(rep(c("Male", "Female"), times = 120)),
         slice_by_2 = as.factor(rep(c("Happy", "Neutral", "Sad"), each = 80)),
         compare_by = as.factor(rep(c("blue", "green", "brown"), times = 80)),
         length   = c(sample(1:10, 120, replace=T), sample(5:12, 120, replace=T)),
         pulse    = runif(240, 60, 120),
         preasure = c(rnorm(80,1,2),rnorm(80,1,2.1),rnorm(80,1,3))
     )
     ) %&gt;%
  group_by(slice_by_1, slice_by_2)
  </code></pre>
  
  <p><strong>Let's look at data:</strong></p>
  
  <pre><code>Source: local data frame [240 x 6]
  Groups: slice_by_1, slice_by_2
  
     slice_by_1 slice_by_2 compare_by length     pulse     preasure
  1        Male      Happy       blue     10  69.23376  0.508694601
  2      Female      Happy      green      1  68.57866 -1.155632020
  3        Male      Happy      brown      8 112.72132  0.007031799
  4      Female      Happy       blue      3 116.61283  0.383769524
  5        Male      Happy      green      7 110.06851 -0.717791526
  6      Female      Happy      brown      8 117.62481  2.938658488
  7        Male      Happy       blue      9 105.59749  0.735831389
  8      Female      Happy      green      2  83.44101  3.881268679
  9        Male      Happy      brown      5 101.48334  0.025572561
  10     Female      Happy       blue     10  62.87331 -0.715108893
  ..        ...        ...        ...    ...       ...          ...
  </code></pre>
  
  <p><strong>An example of desired output:</strong></p>
  
  <pre><code>    Data_subsets    length  preasure     pulse
  1     Male_Happy &lt;p-value&gt; &lt;p-value&gt; &lt;p-value&gt;
  2   Female_Happy &lt;p-value&gt; &lt;p-value&gt; &lt;p-value&gt;
  3   Male_Neutral &lt;p-value&gt; &lt;p-value&gt; &lt;p-value&gt;
  4 Female_Neutral &lt;p-value&gt; &lt;p-value&gt; &lt;p-value&gt;
  5       Male_Sad &lt;p-value&gt; &lt;p-value&gt; &lt;p-value&gt;
  6     Female_Sad &lt;p-value&gt; &lt;p-value&gt; &lt;p-value&gt;
  </code></pre>