title: Use data.table instead of pivot_longer
body: <p>I would like some help to adjust the output called <code>adjusted</code> that I generate. My idea is to optimize somehow to generate faster. Notice that I'm using <code>pivot_longer</code>, which takes longer. One idea would be to continue using <code>data.table</code> as I did to generate <code>SPV</code>. However, I don't know how to do that in this case for <code>adjusted</code>. Can you help me?</p>
  <p>I would like to generate the same output table as in the question.</p>
  <pre><code>library(dplyr)
  library(tidyr)
  library(lubridate)
  library(data.table)
  
  df1 &lt;- structure(
    list(date1= c(&quot;2021-06-28&quot;,&quot;2021-06-28&quot;,&quot;2021-06-28&quot;,&quot;2021-06-28&quot;,&quot;2021-06-28&quot;,
                  &quot;2021-06-28&quot;,&quot;2021-06-28&quot;,&quot;2021-06-28&quot;),
         date2 = c(&quot;2021-06-25&quot;,&quot;2021-06-25&quot;,&quot;2021-06-27&quot;,&quot;2021-07-07&quot;,&quot;2021-07-07&quot;,&quot;2021-07-09&quot;,&quot;2021-07-09&quot;,&quot;2021-07-09&quot;),
         Code = c(&quot;FDE&quot;,&quot;ABC&quot;,&quot;ABC&quot;,&quot;ABC&quot;,&quot;CDE&quot;,&quot;FGE&quot;,&quot;ABC&quot;,&quot;CDE&quot;),
         Week= c(&quot;Wednesday&quot;,&quot;Wednesday&quot;,&quot;Friday&quot;,&quot;Wednesday&quot;,&quot;Wednesday&quot;,&quot;Friday&quot;,&quot;Friday&quot;,&quot;Friday&quot;),
         DR1 = c(4,1,4,3,3,4,3,5),
         DR01 = c(4,1,4,3,3,4,3,6), DR02= c(4,2,6,7,3,2,7,4),DR03= c(9,5,4,3,3,2,1,5),
         DR04 = c(5,4,3,3,6,2,1,9),DR05 = c(5,4,5,3,6,2,1,9),
         DR06 = c(2,4,3,3,5,6,7,8),DR07 = c(2,5,4,4,9,4,7,8),
         DR08 = c(0,0,0,1,2,0,0,0),DR09 = c(0,0,0,0,0,0,0,0),DR010 = c(0,0,0,0,0,0,0,0),DR011 = c(4,0,0,0,0,0,0,0), 
         DR012 = c(0,0,0,3,0,0,0,5),DR013 = c(0,0,1,0,0,0,2,0),DR014 = c(0,0,0,0,0,2,0,0)),
    class = &quot;data.frame&quot;, row.names = c(NA, -8L))
  
  selection = startsWith(names(df1), &quot;DRM&quot;)
  
  df1[selection][is.na(df1[selection])] = 0
  
  dt1 &lt;- as.data.table(df1)
  
  cols &lt;- grep(&quot;^DR0&quot;, colnames(dt1), value = TRUE)
  
  medi_ana &lt;- 
    dt1[, (paste0(cols, &quot;_PV&quot;)) := DR1 - .SD, .SDcols = cols
    ][, lapply(.SD, median), by = .(Code, Week), .SDcols = paste0(cols, &quot;_PV&quot;) ]
  
  f1 &lt;- function(nm, pat) grep(pat, nm, value = TRUE)
  nm1 &lt;- f1(names(df1), &quot;^DR0\\d+$&quot;)
  nm2 &lt;- f1(names(medi_ana), &quot;_PV&quot;)
  nm3 &lt;- paste0(&quot;i.&quot;, nm2)
  setDT(df1)[medi_ana,  (nm2) := Map(`+`, mget(nm1), mget(nm3)), on = .(Code, Week)]
  SPV &lt;- df1[, c('date1', 'date2', 'Code', 'Week', nm2), with = FALSE]
  
  dmda&lt;-&quot;2021-07-09&quot;
  code&lt;-&quot;CDE&quot;
  
  adjusted&lt;-SPV %&gt;%
  filter(date2==dmda,Code == code) %&gt;%
  group_by(Code) %&gt;%
  summarize(across(starts_with(&quot;DR0&quot;), sum),.groups = 'drop') %&gt;%
  pivot_longer(cols= -Code, names_pattern = &quot;DR0(.+)&quot;, values_to = &quot;val&quot;) %&gt;%
  mutate(name = readr::parse_number(name))
      
      &gt; adjusted
      # A tibble: 14 x 3
         Code   name   val
         &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
       1 CDE       1     5
       2 CDE       2     5
       3 CDE       3     5
       4 CDE       4     5
       5 CDE       5     5
       6 CDE       6     5
       7 CDE       7     5
       8 CDE       8     5
       9 CDE       9     5
      10 CDE      10     5
      11 CDE      11     5
      12 CDE      12     5
      13 CDE      13     5
      14 CDE      14     5
  </code></pre>