title: Rename columns of newly formed database in R
body: <p>How do I rename columns DR01 to DR07 of my SUM dataset? As you can see for the SUM dataset I am adding values from my df dataset together with the PV dataset. So, I would like the SUM database columns to be DR01_DR01_PV instead of DR01, DR02_DR02_PV instead of DR02, and so on. This would make it easier for me to understand that a sum was made.</p>
  <p>Thank you very much!</p>
  <p>Code below.</p>
  <pre><code>library(dplyr)
  library(tidyverse)
  
  df &lt;- structure(
    list(Id=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),
         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;,&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;,&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;,&quot;2021-06-28&quot;,&quot;2021-06-28&quot;),
         date2 = c(&quot;2021-07-01&quot;,&quot;2021-07-01&quot;,&quot;2021-07-01&quot;,&quot;2021-07-01&quot;,&quot;2021-04-02&quot;,
                   &quot;2021-04-02&quot;,&quot;2021-04-02&quot;,&quot;2021-04-02&quot;,&quot;2021-04-02&quot;,&quot;2021-04-02&quot;,&quot;2021-04-03&quot;,
                   &quot;2021-04-03&quot;,&quot;2021-04-03&quot;,&quot;2021-04-03&quot;,&quot;2021-04-03&quot;,&quot;2021-04-08&quot;,&quot;2021-04-08&quot;,
                   &quot;2021-04-09&quot;,&quot;2021-04-09&quot;,&quot;2021-04-10&quot;,&quot;2021-04-10&quot;,&quot;2021-07-02&quot;,&quot;2021-07-02&quot;,
                   &quot;2021-07-02&quot;,&quot;2021-07-03&quot;,&quot;2021-07-03&quot;),
         Week= c(&quot;Thursday&quot;,&quot;Thursday&quot;,&quot;Thursday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Friday&quot;,&quot;Friday&quot;,&quot;Friday&quot;,
                 &quot;Friday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;,&quot;Saturday&quot;,&quot;Saturday&quot;,&quot;Saturday&quot;,&quot;Saturday&quot;,&quot;Thursday&quot;,
                 &quot;Thursday&quot;,&quot;Friday&quot;,&quot;Friday&quot;,&quot;Friday&quot;,&quot;Friday&quot;,&quot;Friday&quot;,&quot;Friday&quot;,&quot;Friday&quot;,&quot;Monday&quot;,
                 &quot;Monday&quot;),
         DTPE = c(&quot;Ho&quot;,&quot;Ho&quot;,&quot;Ho&quot;,&quot;Ho&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;Ho&quot;,&quot;Ho&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;),
         D1 = c(8,1,9, 3,5,4,7,6,3,8,2,3,4,6,7,8,4,2,6,2,3,4,3,2,4,8), DR01 = c(4,1,4,3,3,4,3,6,3,7,2,3,4,6,7,8,4,2,6,7,3,3,4,5,6,4),
         DR02= c(4,2,6,7,3,2,7,4,2,1,2,3,4,6,7,8,4,2,6,4,3,4,3,2,4,3),DR03 = c(9,5,4,3,3,2,1,5,3,7,2,3,4,7,7,8,4,2,6,4,3,4,3,4,5,4),
         DR04 = c(5,4,3,3,6,2,1,9,3,7,2,3,4,7,7,8,4,2,6,4,3,3,4,5,3,4),DR05 = c(5,4,5,3,6,2,1,9,3,7,5,3,4,3,4,5,6,2,6,4,3,3,4,5,3,4),
         DR06 = c(2,4,3,3,5,6,7,8,3,7,2,3,4,7,7,8,4,2,6,4,3,3,4,5,3,4),DR07 = c(2,5,4,4,9,4,7,8,3,7,2,3,4,7,7,8,4,2,6,4,3,3,4,5,3,4)),
    class = &quot;data.frame&quot;, row.names = c(NA, -26L))
  
  
  df&lt;-df %&gt;%
    group_by(date2, Week) %&gt;%
    select(D1:DR07) %&gt;%
    summarise_all(sum)
  
  df&lt;-data.frame(df)  
  
  x&lt;-subset(df, select = DR01:DR07)
  x&lt;-cbind(df, setNames(df$D1 - x, paste0(names(x), &quot;_PV&quot;)))
  PV&lt;-select(x, date2,Week, D1, ends_with(&quot;PV&quot;))
  
  PV&lt;-PV %&gt;%
    group_by(Week) %&gt;%
    summarize(across(ends_with(&quot;PV&quot;), median))
  PV
    Week     DR01_PV DR02_PV DR03_PV DR04_PV DR05_PV DR06_PV DR07_PV
    &lt;chr&gt;      &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;
  1 Friday      -1.5       0      -1      -1    -1      -2.5    -2.5
  2 Monday       2         5       3       5     5       5       5  
  3 Saturday     0         0      -1      -1     3      -1      -1  
  4 Thursday     4.5       1       0       3     2.5     4.5     3 
  
  SUM&lt;-df %&gt;%
    pivot_longer(-c(date2:Week)) %&gt;%
    left_join(PV %&gt;% rename_with( ~str_remove(., &quot;_PV&quot;)) %&gt;% 
                pivot_longer(-Week, values_to = &quot;PV&quot;)) %&gt;%
    mutate(new_value = value + PV) %&gt;%
    select(-c(value:PV)) %&gt;%
    pivot_wider(names_from = name, values_from = new_value)
  
   SUM
   date2      Week        D1  DR01  DR02  DR03  DR04  DR05  DR06  DR07
    &lt;chr&gt;      &lt;chr&gt;    &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  1 2021-04-02 Friday      NA  24.5    19    20    27  27    33.5  35.5
  2 2021-04-03 Saturday    NA  22      22    22    22  22    22    22  
  3 2021-04-08 Thursday    NA  16.5    13    12    15  13.5  16.5  15  
  4 2021-04-09 Friday      NA   6.5     8     7     7   7     5.5   5.5
  5 2021-04-10 Friday      NA   8.5     7     6     6   6     4.5   4.5
  6 2021-07-01 Thursday    NA  16.5    20    21    18  19.5  16.5  18  
  7 2021-07-02 Friday      NA  10.5     9    10    11  11     9.5   9.5
  8 2021-07-03 Monday      NA  12      12    12    12  12    12    12  
  </code></pre>