title: Evaluate mode function on column with time-of-day class stored data in R data.table
body: <p>The intention is to summarize the Duration column of the datatable by applying: sum, max, mode, min, and count
  The mode function I use is the one shown in <a href="https://stackoverflow.com/a/8189441/8409550">How to find the statistical mode?
  </a> and the one on the package DescTools.</p>
  <p>the data used</p>
  <pre><code>library(data.table)
  dt&lt;-data.table(
    stringsAsFactors = FALSE,
    ODUFault = c(&quot;NO&quot;,&quot;SI&quot;,&quot;NO&quot;,&quot;SI&quot;,&quot;NO&quot;,
                 &quot;SI&quot;,&quot;NO&quot;,&quot;SI&quot;,&quot;NO&quot;,&quot;SI&quot;,&quot;NO&quot;,&quot;SI&quot;,&quot;NO&quot;,&quot;SI&quot;,&quot;NO&quot;,
                 &quot;SI&quot;,&quot;NO&quot;,&quot;SI&quot;,&quot;NO&quot;,&quot;SI&quot;),
    LastFault = c(&quot;sA&quot;,&quot;sB&quot;,&quot;sB&quot;,&quot;sB&quot;,&quot;sB&quot;,
                  &quot;sB&quot;,&quot;sB&quot;,&quot;sB&quot;,&quot;sB&quot;,&quot;sB&quot;,&quot;sB&quot;,&quot;sC&quot;,&quot;sC&quot;,&quot;sB&quot;,&quot;sB&quot;,
                  &quot;sB&quot;,&quot;sB&quot;,&quot;sB&quot;,&quot;sB&quot;,&quot;sB&quot;),
    SubFlt = c(&quot;A&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,
               &quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;C&quot;,&quot;C&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,
               &quot;B&quot;,&quot;B&quot;),
    Duration = c(&quot;00:09:40&quot;,&quot;00:03:01&quot;,
                 &quot;00:06:58&quot;,&quot;00:03:00&quot;,&quot;00:06:59&quot;,&quot;00:03:00&quot;,&quot;00:06:58&quot;,
                 &quot;00:03:01&quot;,&quot;00:06:59&quot;,&quot;00:02:59&quot;,&quot;00:07:29&quot;,&quot;00:03:01&quot;,
                 &quot;00:06:29&quot;,&quot;00:05:03&quot;,&quot;00:04:56&quot;,&quot;00:03:00&quot;,&quot;00:06:59&quot;,
                 &quot;00:02:59&quot;,&quot;00:07:00&quot;,&quot;00:15:33&quot;)
  )
  </code></pre>
  <p>When performing the summary using the median function, all outputs have the format: &quot;H: M: S&quot;</p>
  <pre><code>dt[, Duration:=as.ITime(Duration)]
  Summarize_SubFlt=dt[,list(g = sum(Duration),m=max(Duration),md=median(Duration),n=min(Duration),c=.N),by=.(ODUFault,SubFlt)][
    which(ODUFault ==&quot;SI&quot;), .SD, by=SubFlt]
  
     SubFlt ODUFault        g        m       md        n c
  1:      B       SI 00:41:36 00:15:33 00:03:00 00:02:59 9
  2:      C       SI 00:03:01 00:03:01 00:03:01 00:03:01 1
  </code></pre>
  <p>When using the mode function, all outputs lose the format: &quot;H: M: S&quot;, except the output of the mode function.</p>
  <pre><code>getmode &lt;- function(v) {
    uniqv &lt;- unique(v)
    uniqv[which.max(tabulate(match(v, uniqv)))]
  }
  Summarize_SubFlt2=dt[,list(g = sum(Duration),m=max(Duration),md=getmode(Duration),n=min(Duration),c=.N),by=.(ODUFault,SubFlt)][
    which(ODUFault ==&quot;SI&quot;), .SD, by=SubFlt]
     
  SubFlt ODUFault    g   m       md   n c
  1:      B       SI 2496 933 00:03:00 179 9
  2:      C       SI  181 181 00:03:01 181 1
  </code></pre>
  <pre><code>#Structure of Summarize_SubFlt
  Classes ‘data.table’ and 'data.frame':  2 obs. of  7 variables:
   $ SubFlt  : chr  &quot;B&quot; &quot;C&quot;
   $ ODUFault: chr  &quot;SI&quot; &quot;SI&quot;
   $ g       : 'ITime' int  00:41:36 00:03:01
   $ m       : 'ITime' int  00:15:33 00:03:01
   $ md      : 'ITime' num  00:03:00 00:03:01
   $ n       : 'ITime' int  00:02:59 00:03:01
   $ c       : int  9 1
   - attr(*, &quot;.internal.selfref&quot;)=&lt;externalptr&gt;
  
  #Structure of Summarize_SubFlt2
  Classes ‘data.table’ and 'data.frame':  2 obs. of  7 variables:
   $ SubFlt  : chr  &quot;B&quot; &quot;C&quot;
   $ ODUFault: chr  &quot;SI&quot; &quot;SI&quot;
   $ g       : int  2496 181
   $ m       : int  933 181
   $ md      : 'ITime' int  00:03:00 00:03:01
   $ n       : int  179 181
   $ c       : int  9 1
   - attr(*, &quot;.internal.selfref&quot;)=&lt;externalptr&gt; 
  
  #Structure of Summarize_SubFlt3 using Mode from library(DescTools)
  Classes ‘data.table’ and 'data.frame':  2 obs. of  7 variables:
   $ SubFlt  : chr  &quot;B&quot; &quot;C&quot;
   $ ODUFault: chr  &quot;SI&quot; &quot;SI&quot;
   $ g       : 'ITime' int  00:41:36 00:03:01
   $ m       : 'ITime' int  00:15:33 00:03:01
   $ md      : 'ITime' num  00:03:00 00:03:01
   $ n       : 'ITime' int  00:02:59 00:03:01
   $ c       : int  9 1
   - attr(*, &quot;.internal.selfref&quot;)=&lt;externalptr&gt; 
  </code></pre>
  <p><strong>How to keep the Format &quot;H% M% S&quot; for all summary outputs?</strong></p>