title: R: If there are consecutive columns that are equal, how to keep just one and assign a new column name
body: <p>I have a data set of customers, what stores they shopped at, what they purchased at each store, and on what day.</p>
  <pre><code>Shop_list &lt;- data.frame (Names  = c('Adam','Eve','Lucy','Ricky','Gomez','Morticia','Adam','Eve','Lucy','Ricky',
                          'Adam','Eve','Ricky','Gomez','Adam','Eve','Lucy','Adam','Eve','Lucy','Adam','Eve','Lucy'),
                    Day = c(1,1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,5,5,5,6,6,6),
                    Store= c('None','None','None','None','None','None','Lowes',
                            'Home Depot','Lowes','Home Depot','Lowes',
                            'Home Depot','Home Depot','Lowes','None',
                            'Home Depot','None','None','None','Home Depot',
                            'Home Depot','None','None'),
                    Item= c('None','None','None','None','None','None','Wood','Soil','Nails','Pots','Nails',
                          'Pots','Soil','Wood','None','Seeds','None','None','None','Seeds','Seeds','None','None'),
                stringsAsFactors=FALSE
  )
  </code></pre>
  <p>I have written a function that summarizes this data ,</p>
  <pre><code>library(dplyr)
  library(flextable)
  
  Shop_fcn &lt;- function(data){
    data %&gt;%
      group_by(Day) %&gt;%
      mutate(N_nam = n_distinct(Names)) %&gt;%
      group_by(Names, Day, N_nam, Store, Item) %&gt;%
      summarize(n_item = n()) %&gt;%
      group_by(Day, N_nam, Store, Item) %&gt;%
      summarize(n_nam = n(),
                n_item = sum(n_item))%&gt;%
      mutate(pct = round(n_nam/N_nam*100,digits = 1),
             txt = paste0( n_nam, &quot; (&quot;, pct, &quot;%)&quot;),
             Day_n = (paste0(&quot;Day &quot;, Day,&quot; (N=&quot;,  N_nam, &quot;)&quot;)))%&gt;%
      ungroup %&gt;% select(Day_n , Store, Item, txt) %&gt;%
      pivot_wider(values_from = txt, names_from = Day_n) %&gt;%
      mutate_at(vars(starts_with(c(&quot;Day&quot;))), ~if_else(is.na(.), &quot;&quot;, .)) %&gt;%
      arrange(Store, Item) %&gt;% 
      group_by(store2 = Store) %&gt;% 
      mutate(Store = if_else(row_number() != 1, &quot;&quot;, Store))%&gt;%
      ungroup() %&gt;% select(-store2)
  }
  
  Shop_day &lt;- Shop_list %&gt;%
    bind_rows(Shop_list) %&gt;%
    Shop_fcn ()
  
  flextable(Shop_day)
  </code></pre>
  <p>and I get the following output.</p>
  <p><a href="https://i.stack.imgur.com/EGR8s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EGR8s.png" alt="enter image description here" /></a></p>
  <p>The columns for Day 2 and 3 are equal, as are the columns for Days 4, 5 , and 6.I am trying to make it so that the column titles for the columns with the same info read as  Day 2 - 3 (N=4) and Day 4 - 6 (N=3).</p>
  <p>So far, I've tried to remove the columns that are duplicated</p>
  <pre><code>Shop_nodup &lt;- Shop_day[!duplicated(as.list(Shop_day))]
  flextable(Shop_nodup)
  </code></pre>
  <p>Which gives me</p>
  <p><a href="https://i.stack.imgur.com/RRd9i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RRd9i.png" alt="enter image description here" /></a></p>
  <p>The duplicate columns are gone, but I can't figure out a way to specify in the column titles to specify the range of Days that the column covers (Day 2 - 3 (N=4) and Day 4 - 6 (N=3) )</p>