<p>Example in R. If you have a file of column names, <code>names.txt</code>, such as:</p>
<pre><code>mpg 
cyl  
disp
</code></pre>
<p>and a data.frame like <code>mtcars</code></p>
<pre><code>&gt; mtcars
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
 [ reached 'max' / getOption(&quot;max.print&quot;) -- omitted 23 rows ]
</code></pre>
<p>Then you can select the columns by:</p>
<pre><code>library(dplyr)
list_names = read.table(&quot;names.txt&quot;, header=F) %&gt;% dplyr::select(V1)
&gt; mtcars %&gt;% dplyr::select(list_names$V1)
                     mpg cyl  disp
Mazda RX4           21.0   6 160.0
Mazda RX4 Wag       21.0   6 160.0
Datsun 710          22.8   4 108.0
Hornet 4 Drive      21.4   6 258.0
Hornet Sportabout   18.7   8 360.0
Valiant             18.1   6 225.0
Duster 360          14.3   8 360.0
Merc 240D           24.4   4 146.7
</code></pre>

CC BY-SA 4.0
2021-03-08T14:23:31.637Z
Thank you @user438383 for the help
Priya
CC BY-SA 4.0
2021-03-07T18:35:01.487Z
2021-03-07T18:35:01.487Z
user438383
<p>There's a million different ways to do simple taks like this. Pick one and learn it:) Python is my go to as it can do pretty much everything you'd ever want. I saved your example as csv with no spaces called expression.txt.</p>
<pre><code>import pandas as pd
df = pd.read_csv('expression.txt')
df[['SM-5GZZ7','SM-5GIEN','SM-5EGGH']]

SM-5GZZ7    SM-5GIEN    SM-5EGGH
0   0   0.0 0.0
1   187 109 143 251 113 139 199 NaN NaN
2   0   0.0 0.0
3   1   0.0 0.0
4   0   0.0 0.0
5   0   0.0 1.0
</code></pre>

CC BY-SA 4.0
2021-03-06T07:05:50.700Z
I too prefer Python :)
The Unfun Cat
CC BY-SA 4.0
2021-03-06T08:54:07.053Z
Thank you for your answer. But I prefer to use bash. I have used awk in past to do this task (but only too get 10-12 columns). In this case, I have to extract 5000 columns based on a list in another file named (brain.txt).
Priya
CC BY-SA 4.0
2021-03-07T18:10:28.253Z
@Liam McIntyre: Can you something in bash or R? and I have to extract nearly 5000 columns from the mastermatrix.txt file. All the columns to be extracted are in a file brain.txt.
Priya
CC BY-SA 4.0
2021-03-07T22:20:33.287Z
I don't know how to off the top of my head sorry. I see that you've been given solutions in both bash and r. Now it's up to you to do some work to make them fit Ur problem.
Liam McIntyre
CC BY-SA 4.0
2021-03-06T02:28:06.723Z
2021-03-06T02:28:06.723Z
Liam McIntyre
<p>I have a matrix file (expression.txt) in which the first column is a gene_id and from the second column the sample id's start. This matrix has 20,000 columns with sample id's corresponding to different tissue. I want to extract the samples of one tissue. Example:  SM-5GZZ7 SM-5GIEN SM-5EGGH are samples from brain tissue. So I want to separate the list of samples [(file=brain.txt;n=5000] corresponding to brain tissue from this matrix (with bash or R)</p>
<pre><code>Name    SM-5GZZ7    SM-5EGHI    SM-5EGHJ    SM-5N9CS    SM-5GIEN    SM-5EGHH    SM-5EGGH
ENSG00000223972.5   0   0   0   0   0   0   0
ENSG00000227232.5   187 109 143 251 113 139 199
ENSG00000278267.1   0   0   1   0   0   0   0
ENSG00000243485.5   1   0   0   1   0   0   0
ENSG00000237613.2   0   0   0   0   0   0   0
ENSG00000268020.3   0   1   0   0   0   1   1
</code></pre>
<p>The file brain.txt looks like (Number of lines in this files = 5000)</p>
<pre><code>SM-5GZZ7
SM-5GIEN 
SM-5EGGH
SM-5GZXC
SM-5GZXO
SM-5GZY1
SM-5EGHK
SM-5EGHL
</code></pre>

CC BY-SA 4.0
2021-03-06T18:13:28.477Z
This should answer the question https://stackoverflow.com/questions/11098189/extract-columns-from-a-file-based-on-header-selected-from-another-file
user438383
CC BY-SA 4.0
2021-03-06T18:44:27.010Z
Thank you soo much @user438383
Priya
CC BY-SA 4.0
2021-03-07T18:06:34.330Z
Hi @user438383. I tried the script by dogbane, it didn't work
Priya
<gene-expression><bash><data-retrieval><data-preprocessing>
Print specific columns in a matrix on the basis of sample id's in the header
# Print specific columns in a matrix on the basis of sample id's in the header

(Asked by: username_0 on 2021-03-05)

I have a matrix file (expression.txt) in which the first column is a gene_id and from the second column the sample id's start. This matrix has 20,000 columns with sample id's corresponding to different tissue. I want to extract the samples of one tissue. Example:  SM-5GZZ7 SM-5GIEN SM-5EGGH are samples from brain tissue. So I want to separate the list of samples [(file=brain.txt;n=5000] corresponding to brain tissue from this matrix (with bash or R)
Name    SM-5GZZ7    SM-5EGHI    SM-5EGHJ    SM-5N9CS    SM-5GIEN    SM-5EGHH    SM-5EGGH
ENSG00000223972.5   0   0   0   0   0   0   0
ENSG00000227232.5   187 109 143 251 113 139 199
ENSG00000278267.1   0   0   1   0   0   0   0
ENSG00000243485.5   1   0   0   1   0   0   0
ENSG00000237613.2   0   0   0   0   0   0   0
ENSG00000268020.3   0   1   0   0   0   1   1

The file brain.txt looks like (Number of lines in this files = 5000)
SM-5GZZ7
SM-5GIEN 
SM-5EGGH
SM-5GZXC
SM-5GZXO
SM-5GZY1
SM-5EGHK
SM-5EGHL

**Tags:** gene-expression, bash, data-retrieval, data-preprocessing
> **Comments:**
> **username_1**: This should answer the question https://stackoverflow.com/questions/11098189/extract-columns-from-a-file-based-on-header-selected-from-another-file  
> **username_0**: Thank you soo much @user438383  
> **username_0**: Hi @user438383. I tried the script by dogbane, it didn't work  

---

## 2 answers

### Answer by username_1 on 2021-03-07. Score: 1 | Accepted answer

Example in R. If you have a file of column names, names.txt, such as:
mpg 
cyl  
disp

and a data.frame like mtcars
> mtcars
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
 [ reached 'max' / getOption("max.print") -- omitted 23 rows ]

Then you can select the columns by:
library(dplyr)
list_names = read.table("names.txt", header=F) %>% dplyr::select(V1)
> mtcars %>% dplyr::select(list_names$V1)
                     mpg cyl  disp
Mazda RX4           21.0   6 160.0
Mazda RX4 Wag       21.0   6 160.0
Datsun 710          22.8   4 108.0
Hornet 4 Drive      21.4   6 258.0
Hornet Sportabout   18.7   8 360.0
Valiant             18.1   6 225.0
Duster 360          14.3   8 360.0
Merc 240D           24.4   4 146.7

> **Comments:**
> **username_0**: Thank you @user438383 for the help  

---

### Answer by username_2 on 2021-03-06. Score: 1

There's a million different ways to do simple taks like this. Pick one and learn it:) Python is my go to as it can do pretty much everything you'd ever want. I saved your example as csv with no spaces called expression.txt.
import pandas as pd
df = pd.read_csv('expression.txt')
df[['SM-5GZZ7','SM-5GIEN','SM-5EGGH']]

SM-5GZZ7    SM-5GIEN    SM-5EGGH
0   0   0.0 0.0
1   187 109 143 251 113 139 199 NaN NaN
2   0   0.0 0.0
3   1   0.0 0.0
4   0   0.0 0.0
5   0   0.0 1.0

> **Comments:**
> **username_3**: I too prefer Python :)  
> **username_0**: Thank you for your answer. But I prefer to use bash. I have used awk in past to do this task (but only too get 10-12 columns). In this case, I have to extract 5000 columns based on a list in another file named (brain.txt).  
> **username_0**: @Liam McIntyre: Can you something in bash or R? and I have to extract nearly 5000 columns from the mastermatrix.txt file. All the columns to be extracted are in a file brain.txt.  
> **username_2**: I don't know how to off the top of my head sorry. I see that you've been given solutions in both bash and r. Now it's up to you to do some work to make them fit Ur problem.  

---

