title: Change column names in a loop
body: <p>I have a table set, on which I perform some operations in a loop:</p>
  
  <pre><code>for (i in 1:24){
      assign(paste0("set.", i), ts(subset(set, set$hour==i)))
      assign(paste0("lag_set.", i),
          cbind(get(paste0("set.", i))[, 1:3],
          lag(get(paste0("set.", i))[, 4:ncol(set)], 3))[-1:-3, ])
  
      assign(paste0("train.set.", i),
          get(paste0("lag_set.", i))[1:train_obs, ])
      assign(paste0("validation.set.", i),
          get(paste0("lag_set.", i))[(train_obs+1):(nrow(set.1)-test_obs+1), ])
      assign(paste0("test.set.", i),
          get(paste0("lag_set.", i))[(nrow(set.1)-test_obs+1):(nrow(set.1)), ])
  }
  </code></pre>
  
  <p>At the end, I end up with uniformative column names.</p>
  
  <p>I tried to add the following line to the loop which simply obtains the column names from the first table (they do not change):</p>
  
  <pre><code>colnames(get(paste0("lag_set.", i)))&lt;-colnames(set)
  </code></pre>
  
  <p>As a result, I received the following error:</p>
  
  <blockquote>
    <p>Error in colnames(get(paste0("lag_set.", i))) &lt;- colnames(set) : 
                target of assignment expands to non-language object</p>
  </blockquote>
  
  <p>However, when I inserted the name of the table (e.g. <code>lag_set.1</code>):</p>
  
  <pre><code>colnames(lag_set.1)&lt;-colnames(set)
  </code></pre>
  
  <p>the code worked perfectly.</p>
  
  <p>Where did I make a mistake and how to deal with it?</p>