Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-09T23:59:18.527Z
Answers.Text: Thank you for your answer. However, my actual sample list has more than 100 id's and matrix has 2000 rows. I want match_pat to read a list of id's? Is it possible?
Answers.UserDisplayName: Megha
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-10T12:02:05.560Z
Answers.Text: @Megha where does your list of ID's come from ? a file ?
Answers.UserDisplayName: user324810
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-10T15:22:30.547Z
Answers.Text: @Law: I have a GTEX brain sample file (which have id's in the header like  GTEX.111FC.3126.SM.5GZZ2. I want to extract the age-related samples(20-60 yrs) from this file. The age-related data file is a list of files which says all id's starting with GTEX.111FC are 20-30 year old and so on. The issue is the id's in the list is the half part of ID in the main master file making it difficult for me to extract it
Answers.UserDisplayName: Megha
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-10T15:23:45.023Z
Answers.Text: And yes age id file is a separated file with the list of ID's
Answers.UserDisplayName: Megha
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-10T16:53:46.203Z
Answers.Text: Hi, I did it with bash. I got the full id's from the partial match.
  
  xargs -i grep ^{} matched.Brain.txt.2nd.txt < 2ndcol.20_30 > new.file
Answers.UserDisplayName: Megha
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-10T18:13:56.820Z
Answers.Text: Glad you succeed to solve your issue with Bash. To answer your question, you can define match_pat to be a vector of the column containing id files like `match_pat <- as.vector(list_id_file$NameOfTheColumWithID). Then, you can extract column form your GTEX brain sample file.
Answers.UserDisplayName: dc37
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-09T22:40:52.877Z
Answers.LastActivityDate: 2021-03-09T22:40:52.877Z
Answers.UserDisplayName: dc37
Body: <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>
  
Tags: <r><data-preprocessing><gtex>
Title: How to extract column from a matrix matching the another file (sample file)
ThreadText: # How to extract column from a matrix matching the another file (sample file)
  
  (Asked by: username_0 on 2021-03-09)
  
  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](https://bioinformatics.stackexchange.com/questions/15487/print-specific-columns-in-a-matrix-on-the-basis-of-sample-ids-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
  
  **Tags:** r, data-preprocessing, gtex
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-03-09. Score: 1 | Accepted answer
  
  Assuming your "master" files looks like this:
  
  ```r
  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`:
  
  ```r
  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:
  
  ```r
  > 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:
  
  ```r
  match_pat <- c("GTEX.1117F","GTEX.111FC","GTEX.111XS")
  m <- unlist(sapply(match_pat,function(x) grep(x,colnames(df))))
  
  master[,m]
  ```
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you for your answer. However, my actual sample list has more than 100 id's and matrix has 2000 rows. I want match_pat to read a list of id's? Is it possible?  
  > **username_2**: @username_0 where does your list of ID's come from ? a file ?  
  > **username_0**: @Law: I have a GTEX brain sample file (which have id's in the header like  GTEX.111FC.3126.SM.5GZZ2. I want to extract the age-related samples(20-60 yrs) from this file. The age-related data file is a list of files which says all id's starting with GTEX.111FC are 20-30 year old and so on. The issue is the id's in the list is the half part of ID in the main master file making it difficult for me to extract it  
  > **username_0**: And yes age id file is a separated file with the list of ID's  
  > **username_0**: Hi, I did it with bash. I got the full id's from the partial match.  xargs -i grep ^{} matched.Brain.txt.2nd.txt < 2ndcol.20_30 > new.file  
  > **username_1**: Glad you succeed to solve your issue with Bash. To answer your question, you can define match_pat to be a vector of the column containing id files like `match_pat <- as.vector(list_id_file$NameOfTheColumWithID). Then, you can extract column form your GTEX brain sample file.  
  
  ---
  
  