title: How to map list elements to colnames of matrix in R
body: <p>I have a matrix without colnames that looks like this:  </p>
  
  <pre><code>&gt;my_matrix
                          [,1]         [,2]         [,3]                  
  ENSG00000000345 1.324266e+03 1.071842e+03 1.024920e+03 
  ENSG00000000105 3.750147e+01 8.081121e+02 4.736242e+01 
  ENSG00000000719 3.216755e+03 2.001329e+03 1.934501e+03 
  </code></pre>
  
  <p>There are 280 columns in total and thousands of rows.</p>
  
  <p>I also have a list of two-element vectors that looks like this:</p>
  
  <pre><code>&gt; head(mapping)
  [[1]]
  [1] "100"        "bob"
  
  [[2]]
  [1] "10"         "alice"
  
  [[3]]
  [1] "1"          "mark"
  
  [[4]]
  [1] "101"        "simon"
  
  [[5]]
  [1] "102"        "jeff"
  
  [[6]]
  [1] "103"        "alexander"
  </code></pre>
  
  <p>There are 280 vectors in the list. The first element in each of the vectors are numbers 1 through 280. The second element is a unique string.</p>
  
  <p>I would like to make the colnames of the matrix the unique strings in the mapping list (the second element of each vector), using the first element of each vector to correspond to the column indices of the matrix. </p>
  
  <p>For example, column 1 would be "mark". </p>
  
  <p>I'm struggling with the fact that the order of the vectors does not correspond to the matrix column indices. if that were the case something like this would work: </p>
  
  <pre><code> colnames(my_matrix) &lt;- lapply(mapping, `[[`, 2)
  </code></pre>
  
  <p>Can anyone help with this? Thank you in advance.</p>