title: Set attributes to multiple data.table columns in R
body: <p>This should be something easy but I cannot seem to get it right.</p>
  
  <p>I have a data table with N columns (let's say N=40K) and two character vectors with the same length (i.e. labelvector and unitvector) and I would like to add the attributes "label" and "units" to each column of the data.table to the value indicated by the relevant vectors for that column.</p>
  
  <p>Both vectors are also named, using the data.table column names.</p>
  
  <p>My efforts revolved around using setattr to all columns or including the .SD notation with lapply, which I use as a main workhorse when I have large tables but without any significant success. </p>
  
  <p>The latter failed because I could not access the name of the column being passed to the function call from within lapply, in order to set the attributes by reference.</p>
  
  <p>I can either make a function that sets the attributes by reference (having := data.table call within the function body) or an *apply/for loop that sets them but both take a lot of time.</p>
  
  <p>Do you think that this can be done faster or more elegantly?</p>
  
  <p><strong>*</strong> Edit*****</p>
  
  <p>Example:</p>
  
  <p>the table has 4 columns: Age, Hgt, Wgt and S</p>
  
  <p>labelvector has 4 values: "Age", "Height", "Weight" and "Sex". </p>
  
  <p>unitvecor has also 4 values: "Years", "cm", "kg", NA.</p>
  
  <p>both labelvector and unitvector values are named with table column names.</p>
  
  <p>So the goal is to set for data table:</p>
  
  <p>Column Age, label: "Age", units "Years".</p>
  
  <p>Column Hgt, label: "Height", units "cm".</p>
  
  <p>Column Wgt, label: "Weight", units "kg".</p>
  
  <p>Column S, label: "Sex", units NA.</p>
  
  <p>This has to be generalized to a data.table of tens of thousands of columns.</p>