title: Calculating Average and median on Monthly data and convert in htmltable R
body: <p>I have a DF:</p>
  
  <pre><code>my_data &lt;- read.table(text = 
                        "ID     Date1                     T1     Date2     Val1
                        A-1    '2018-01-10 15:05:24'       A    2018-01-15  10
                        A-2    '2018-01-05 14:15:22'       B    2018-01-14  12
                        A-3    '2018-01-04 13:20:21'       A    2018-01-13  15
                        A-4    '2018-01-01 18:35:45'       B    2018-01-12  22
                        A-5    '2017-12-28 19:45:10'       A    2018-01-11  18
                        A-6    '2017-12-10 08:03:29'       A    2018-01-10  21
                        A-7    '2017-12-06 20:55:55'       A    2018-01-09  28
                        A-8    '2018-01-10 10:02:12'       A    2018-01-15  10
                        A-9    '2018-01-05 17:15:14'       B    2018-01-14  12
                        A-10   '2018-01-04 18:35:58'       A    2018-01-13  15
                        A-11   '2018-01-01 21:09:25'       B    2018-01-12  22
                        A-12   '2017-12-28 02:12:22'       A    2018-01-11  18
                        A-13   '2017-12-10 03:45:44'       A    2018-01-10  21
                        A-14   '2017-12-06 07:15:25'       A    2018-01-09  28 
                        A-18   '2017-10-07 08:02:84        B    2017-11-05  20
                        A-21   '2017-10-01 06:04:04        A    2017-10-20  15
                        A-51   '2017-09-20 08:07:06        A    2017-09-28  10
                        A-35   '2017-09-14 08:02:45        A    2017-09-25  20
                        A-30   '2017-08-10 15:03:08        A    2017-08-30  25", 
  header = TRUE, stringsAsFactors = FALSE)
  </code></pre>
  
  <p>and running below mentioned code i get output as shown in below:</p>
  
  <pre><code>    table_2 &lt;- merge(  
    my_data %&gt;% 
      mutate(Date2 = ymd(Date2)) %&gt;% 
      arrange(Date2) %&gt;% 
      mutate(Month = paste(month(ymd_hms(Date1), label = TRUE), year(Date1), sep = "-")) %&gt;% 
      filter(T1 == "A") %&gt;%
      group_by(Month) %&gt;% 
      summarise("# of A" = n(),
                "sum of A" = sum(Val1)) %&gt;%
      mutate("MOM Growth # of A" = round(apply(cbind(`# of A`, lag(- `# of A`)), 
                                         1, sum, na.rm = TRUE) / lag(`# of A`), 2),
             "MOM Growth sum of A" = round(apply(cbind(`sum of A`, lag(- `sum of A`)), 
                                           1, sum, na.rm = TRUE) / lag(`sum of A`) * 100, 2)) %&gt;% 
      select(Month, `# of A`, `MOM Growth # of A`, `sum of A`, `MOM Growth sum of A`),
    my_data %&gt;% 
      mutate(Date2 = ymd(Date2)) %&gt;% 
      arrange(Date2) %&gt;% 
      mutate(Month = paste(month(ymd_hms(Date1), label = TRUE), year(Date1), sep = "-")) %&gt;% 
      filter(T1 == "B") %&gt;%
      group_by(Month) %&gt;% 
      summarise("# of B" = n(),
                "sum of B" = sum(Val1)) %&gt;%
      mutate("MOM Growth # of B" = round(apply(cbind(`# of B`, lag(- `# of B`)), 
                                         1, sum, na.rm = TRUE) / lag(`# of B` * 100), 2),
             "MOM Growth sum of B" = round(apply(cbind(`sum of B`, lag(- `sum of B`)), 
                                           1, sum, na.rm = TRUE) / lag(`sum of B`) * 100), 2) %&gt;%
      select(Month, `# of B`, `MOM Growth # of B`, `sum of B`, `MOM Growth sum of B`),
    by = "Month",
    all = TRUE)
  
  table_2[is.na(table_2)] &lt;- ""
  </code></pre>
  
  <p>Output (table_2):</p>
  
  <p><a href="https://i.stack.imgur.com/bVfV1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bVfV1.png" alt="enter image description here"></a></p>
  
  <p>Now i want to add two more column <code>Median of A</code> , <code>Avg Time of A</code> under <code>Status of A</code> head and <code>Median of B</code>, <code>Avg Time of B</code> in <code>Status of B</code> head. and convert those output in htmltable format.</p>
  
  <p>Just wanted to know how to tweak code in <code>summarise</code> to calculate these value on monthly data.</p>
  
  <p>in addition, month should be in sequence order in output data &amp; if there is any month missing between <code>Max</code> month and <code>Min</code> month than that month should come with all value 0 except <code>MOM Growth</code> for both <code>Status of A</code> and <code>Status of B</code> because that should be than -100%.</p>