title: renaming the columns in each dataframe in a list with, while the number of columns vary
body: <p>I am hoping to create a short if-else (or other if this is a stupid approach) function for renaming the columns of all dataframes within a list. I do see many similar questions, which have helped me get my code this far, but they deviate in some specifics leaving me with an outstanding problem. I am really hoping to do this without a package (as silly as that seems given tidyverse is already loaded...)</p>
  <p>Libraries- should be unnecessary.</p>
  <pre><code>library(raster)
  library(tidyverse)
  library(sf)
  library(data.table)
  </code></pre>
  <p>Example data:</p>
  <pre><code>player &lt;- list(`10` = structure(list(ID = &quot;10&quot;, integer_val = 1028, coords.x1 = 266781.960994547, 
      coords.x2 = 4208220.00292058, X1979_08 = 18.1568756103516, 
      X1979_09 = 21.3082904815674, X1979_10 = 14.6396112442017, 
      X1979_11 = 5.12351417541504, X1979_12 = 6.30002880096436, 
      X1980_01 = 4.20031881332397, X1980_02 = 5.70116329193115, 
      X1980_03 = 5.33258485794067, X1980_04 = 8.69955348968506, 
      X1980_05 = 9.88166332244873, X1980_06 = 22.8935871124268, 
      X1980_07 = 24.7441787719727), row.names = 1L, class = &quot;data.frame&quot;), 
      `10015` = structure(list(ID = &quot;10015&quot;, integer_val = 811, 
          coords.x1 = 432912.236471687, coords.x2 = 4418957.6628215, 
          X1961_09 = 9.60138702392578, X1961_10 = 9.8080587387085, 
          X1961_11 = 3.80465173721313, X1961_12 = 2.24590229988098, 
          X1962_01 = 2.89834785461426, X1962_02 = 4.26397132873535, 
          X1962_03 = 4.47179555892944, X1962_04 = 8.64332294464111, 
          X1962_05 = 11.5554437637329, X1962_06 = 15.2697010040283), row.names = 891L, class = &quot;data.frame&quot;))
  </code></pre>
  <p>Now I have already created names for each list (generally 6k-7k baby dataframes per list)</p>
  <pre><code>column_names &lt;- c('ID', 'integer_val', 'longitude', 'latitude', '12', '11', '10', '09', '08', '07', '06', '05', '04', '03', '02', '01')
  colnames_vpd &lt;- c(column_names[1:4], paste(&quot;vpd&quot;, column_names[5:16], sep = &quot;-&quot;))
  </code></pre>
  <p>Some may argue this approach is unnecessary, but the renaming occurs on some lists that are output from a function that takes a night to run, so I just try to hit run and go to sleep...</p>
  <pre><code>Names_func &lt;- function(x, y){
    if(ncol(x) == 16)
      {colnames(x) &lt;- y}
    else{colnames(x) &lt;- y[1:(ncol(x)-1)]
    }
  }
  
  test &lt;- lapply(X = listabc, FUN =  Names_func, y = colnames_vpd)
  </code></pre>
  <p>So the problem with my function is that it overwrites not just the column names, but all of the data in each dataframe. I have tried many permutations on this, but to no avail.
  Afterwards these data are converted to a d.t. with rbindlist and fill.</p>
  <p>I do know that a very similar results can be done on the fly with dplyr's rename, but I would prefer a base solution if possible. Thanks.</p>
  <p>EDIT: I am quite convinced I am dumber than a rock. Apologies if you have read to here.</p>
  <pre><code>Names_func &lt;- function(x, y){
    if(ncol(x) == 16)
      {names(x) &lt;- y}
    else{names(x) &lt;- y[1:(ncol(x))]
    }
    return(x)
  }
  </code></pre>