text: user4783: I have two files master file.txt and sample.txt
  master.txt
  Name    GTEX.1117F.3226.SM.5N9CT    GTEX.111FC.3126.SM.5GZZ2    GTEX.111FC.3326.SM.5GZYV    GTEX.1128S.2726.SM.5H12C    GTEX.1128S.2826.SM.5N9DI    GTEX.117XS.3026.SM.5N9CA
  ENSG00000223972 1   0   0   1   0   0
  ENSG00000227232 298 168 197 106 221 184
  ENSG00000278267 0   1   1   0   0   0
  ENSG00000243485 0   2   0   1   0   1
  ENSG00000237613 0   0   0   1   0   1
  ENSG00000268020 1   0   0   1   0   1
  ENSG00000240361 1   3   0   4   2   1
  ENSG00000186092 3   1   1   1   1   2
  
  Sample file.txt
  GTEX.1117F
  GTEX.111FC  
  GTEX.111XS
  
  Desired output
  Name    GTEX.1117F.3226.SM.5N9CT    GTEX.111FC.3126.SM.5GZZ2    GTEX.111FC.3326.SM.5GZYV    GTEX.117XS.3026.SM.5N9CA
  ENSG00000223972 1   0   0   0
  ENSG00000227232 298 168 197 184
  ENSG00000278267 0   1   1   0
  ENSG00000243485 0   2   0   1
  ENSG00000237613 0   0   0   1
  ENSG00000268020 1   0   0   1
  ENSG00000240361 1   3   0   1
  ENSG00000186092 3   1   1   2
  ENSG00000238009 2   2   1   1
  ENSG00000233750 0   3   3   0
  ENSG00000268903 103 44  76  24
  
  I tried using the R-code form this link
  Print specific columns in a matrix on the basis of sample id's in the header
  However, I am not able to figure out how to extract columns matching only half part of ID (like for GTEX.111FC I want both GTEX.111FC.3126.SM.5GZZ2 and   GTEX.111FC.3326.SM.5GZYV)
  Thank you
  
  user6437: Assuming your "master" files looks like this:
  df <- data.frame(Name = LETTERS[1:5],
                   Col1 = sample(1:10,5),
                   Col2 = sample(1:10,5),
                   Col11 = sample(1:10,5),
                   Col3 = sample(1:10,5))
  
    Name Col1 Col2 Col11 Col3
  1    A    5    4     3   10
  2    B    1    6     5    6
  3    C    3    1     9    4
  4    D    9    9     8    9
  5    E    8   10     1    1
  
  For a sequence of pattern you want to extract, you can use base r function such as grep:
  match_pat = c("Col1","Col2")
  
  m <- unlist(sapply(match_pat,function(x) grep(x,colnames(df))))
  
  Col11 Col12  Col2 
      2     4     3 
  
  And then, extract your columns of interest by doing:
  > df[,m]
    Col1 Col11 Col2
  1    5     3    4
  2    1     5    6
  3    3     9    1
  4    9     8    9
  5    8     1   10
  
  In your example, it will be something like:
  match_pat <- c("GTEX.1117F","GTEX.111FC","GTEX.111XS")
  m <- unlist(sapply(match_pat,function(x) grep(x,colnames(df))))
  
  master[,m]
  ```
  
  
original_text: 4783: <p>I have two files master file.txt and sample.txt</p>
  <p>master.txt</p>
  <pre><code>Name    GTEX.1117F.3226.SM.5N9CT    GTEX.111FC.3126.SM.5GZZ2    GTEX.111FC.3326.SM.5GZYV    GTEX.1128S.2726.SM.5H12C    GTEX.1128S.2826.SM.5N9DI    GTEX.117XS.3026.SM.5N9CA
  ENSG00000223972 1   0   0   1   0   0
  ENSG00000227232 298 168 197 106 221 184
  ENSG00000278267 0   1   1   0   0   0
  ENSG00000243485 0   2   0   1   0   1
  ENSG00000237613 0   0   0   1   0   1
  ENSG00000268020 1   0   0   1   0   1
  ENSG00000240361 1   3   0   4   2   1
  ENSG00000186092 3   1   1   1   1   2
  </code></pre>
  <p>Sample file.txt</p>
  <pre><code>GTEX.1117F
  GTEX.111FC  
  GTEX.111XS
  </code></pre>
  <p>Desired output</p>
  <pre><code>Name    GTEX.1117F.3226.SM.5N9CT    GTEX.111FC.3126.SM.5GZZ2    GTEX.111FC.3326.SM.5GZYV    GTEX.117XS.3026.SM.5N9CA
  ENSG00000223972 1   0   0   0
  ENSG00000227232 298 168 197 184
  ENSG00000278267 0   1   1   0
  ENSG00000243485 0   2   0   1
  ENSG00000237613 0   0   0   1
  ENSG00000268020 1   0   0   1
  ENSG00000240361 1   3   0   1
  ENSG00000186092 3   1   1   2
  ENSG00000238009 2   2   1   1
  ENSG00000233750 0   3   3   0
  ENSG00000268903 103 44  76  24
  </code></pre>
  <p>I tried using the R-code form this link
  <a href="https://bioinformatics.stackexchange.com/questions/15487/print-specific-columns-in-a-matrix-on-the-basis-of-sample-ids-in-the-header">Print specific columns in a matrix on the basis of sample id&#39;s in the header</a></p>
  <p>However, I am not able to figure out how to extract columns matching only half part of ID (like for GTEX.111FC I want both GTEX.111FC.3126.SM.5GZZ2 and   GTEX.111FC.3326.SM.5GZYV)</p>
  <p>Thank you</p>
   6437: <p>Assuming your &quot;master&quot; files looks like this:</p>
  <pre class="lang-r prettyprint-override"><code>df &lt;- data.frame(Name = LETTERS[1:5],
                   Col1 = sample(1:10,5),
                   Col2 = sample(1:10,5),
                   Col11 = sample(1:10,5),
                   Col3 = sample(1:10,5))
  
    Name Col1 Col2 Col11 Col3
  1    A    5    4     3   10
  2    B    1    6     5    6
  3    C    3    1     9    4
  4    D    9    9     8    9
  5    E    8   10     1    1
  </code></pre>
  <p>For a sequence of pattern you want to extract, you can use base r function such as <code>grep</code>:</p>
  <pre class="lang-r prettyprint-override"><code>match_pat = c(&quot;Col1&quot;,&quot;Col2&quot;)
  
  m &lt;- unlist(sapply(match_pat,function(x) grep(x,colnames(df))))
  
  Col11 Col12  Col2 
      2     4     3 
  </code></pre>
  <p>And then, extract your columns of interest by doing:</p>
  <pre class="lang-r prettyprint-override"><code>&gt; df[,m]
    Col1 Col11 Col2
  1    5     3    4
  2    1     5    6
  3    3     9    1
  4    9     8    9
  5    8     1   10
  </code></pre>
  <p>In your example, it will be something like:</p>
  <pre class="lang-r prettyprint-override"><code>match_pat &lt;- c(&quot;GTEX.1117F&quot;,&quot;GTEX.111FC&quot;,&quot;GTEX.111XS&quot;)
  m &lt;- unlist(sapply(match_pat,function(x) grep(x,colnames(df))))
  
  master[,m]
  <span class="math-container">```</span>
  </code></pre>
  