title: Order df columns according to a target vector (but the names match only partially)
body: <p>I have a <strong>data.frame (PC)</strong> that looks like this:</p>
  
  <p><a href="http://i.stack.imgur.com/NWJKe.png" rel="nofollow">http://i.stack.imgur.com/NWJKe.png</a></p>
  
  <p>which has 1000+ columns with similar names.</p>
  
  <p>And I have a <strong>vector of those column names</strong> that looks like this:</p>
  
  <p><a href="http://i.stack.imgur.com/vQ48u.png" rel="nofollow">http://i.stack.imgur.com/vQ48u.png</a></p>
  
  <p>I want to sort the columns (beginning with "GTEX.") in the data.frame such that they are ordered by the age indicated in the <strong>age matrix</strong>.</p>
  
  <pre><code>PC &lt;- read.csv("protein_coding.csv")
  age &lt;- read.table("Annotations_SubjectPhenotypes_DS.txt")
  </code></pre>
  
  <p>I started by changing the names in the <strong>age matrix</strong> to replace the '-' by '.':</p>
  
  <pre><code>new_SUBJID &lt;- gsub("-", ".", age$SUBJID, fixed = TRUE)
  age[, "SUBJID"] &lt;- new_SUBJID
  </code></pre>
  
  <p>Then, I ordered the row names (SUBJUD) of the <strong>age matrix</strong> by age:</p>
  
  <pre><code>sort.age &lt;- with(age,  age[order(AGE) , ])
  sort.age &lt;- na.omit(sort.age)
  </code></pre>
  
  <p>I then created a vector <em>age.ID</em> containing the SUBJIDs in the right order (= how I want to order the columns from the <strong>PC matrix</strong>). </p>
  
  <pre><code>age.id &lt;- sort.age$SUBJID
  </code></pre>
  
  <p>But then I am blocked since the names on the <strong>PC matrix</strong> and the <strong>age matrix</strong> are not the same... Could someone please help me? </p>
  
  <p>Thank you very much in advance! 
  Svalf</p>