title: R data.table: referring to data table column with externally assigned column name
body: <p>I am reading in a large amount of files with monthly price information per product.</p>
  
  <p>I want to obtain a data table merging all these files.</p>
  
  <p>The key of this table is going to be the 2 columns with a product identifier and the date.</p>
  
  <p>A third column then contains the retail price.</p>
  
  <p>In the source files each price column has a name of the format RETAILPRICE_[dd.mm.yyyy].</p>
  
  <p>To prevent my final data table from containing a large amount of columns I need to rename the column with the retail price and to create a new column containing the date.</p>
  
  <p>The following code runs into an error because <code>data.table</code> does not understand the external reference to one of its columns.</p>
  
  <pre class="lang-r prettyprint-override"><code># this is how I obtain the list of files that have to be read in
  # list the files
  # files &lt;- list.files(path = "path",
  #                    pattern = "^Publications.*$",
  #                    full.names = T)
  
  # the data looks like this, although it is contained in an excel file.
  # sample data
  ProdID &lt;- list(836187, 2398159, 2398165, 2398171, 2398188, 1800180, 2320105, 2320128, 2320140, 2320163, 1714888, 2516340)
  RETAILPRICE_01.01.2003 &lt;- c(12.50, 43.50, 65.50, 45.60, 69.45, 21.30, 81.15, 210.70, 405.00, 793.60, 116.50, 162.60)
  Publications_per_2003.01.01 &lt;- data.table(ProdID,RETAILPRICE_01.01.2003)
  
  # uncomment if you want to write this to excel
  # using .xls on purpose, because that's what they used back in the days
  # xlsx::write.xlsx(Publications_per_2003.01.01,
  #    "Publications_per_2003.01.01.xls",
  #    row.names = F)
  # files &lt;- list.files(path = "path",
  #                    pattern = "^Publications.*$",
  #                    full.names = T)
  
  # create data table
  price_list &lt;- data.table(
                   prodID = character(),
                   date = character(),
                   retail_price = numeric())
  
  
  price_list &lt;- lapply(files, function(x){
  
    # obtain date from file name
    # date in file name has the structure yyyy_mm_dd
    # while in the column name date has the structure
    # dd.mm.yyyy
    date &lt;- substr(sapply(strsplit(x,"_"),"[",3),1,10)
  
    # obtain day, month and year separately
    day &lt;- substr(date,9,10)
    month &lt;- substr(date,6,7)
    year &lt;- substr(date,1,4)
  
    # store the name of the column containing the retail price
    priceVar &lt;- as.name(paste0("RETAILPRICE_",day,".",month,".",year))
  
    # read the xls file with the price info and in one go
    # keep only the relevant columns
    file &lt;- data.table(read_excel(x))[
      ,.(prodID= as.character(ProdID),
         retail_price = priceVar,
         date = as.character(gsub("\\.","-",date)))#,with = F
      ]
  
    # merge the new file with the existing data table
    price_list &lt;- merge(price_list,file,"ProdID")
  })
  </code></pre>
  
  <p>This results in the error message </p>
  
  <pre><code>Error in rep(x[[i]], length.out = mn) : 
    attempt to replicate an object of type 'symbol'
  </code></pre>
  
  <p>If I comment the part </p>
  
  <pre><code>retail_price = priceVar,
  </code></pre>
  
  <p>there's no error.</p>
  
  <p>So the problem lies in the reference to the column that somehow is not working.</p>
  
  <p>I also tried</p>
  
  <pre><code>priceVar &lt;- as.name(paste0("RETAILPRICE_",day,".",month,".",year))
  
  file &lt;- data.table(read_excel(x))
  
  setnames(file, priceVar, "retail_price")
  </code></pre>
  
  <p>but I get the error (column name modified to fit the example):</p>
  
  <pre><code>Error in setnames(file, priceVar, "retail_price") : 
    Items of 'old' not found in column names: RETAILPRICE_dd.mm.yyyy.
  </code></pre>
  
  <p>If anyone could enlighten me I would be eternally grateful.</p>