<p>You can save time and memory by preprocessing your file before loading it into R.</p>
<p>Use <a href="https://miller.readthedocs.io/" rel="nofollow noreferrer"><code>miller</code></a> toolkit to manipulate csv or tab-delimited files using column names.</p>
<p>Below is a one-liner that reads csv file, and writes columns specified in option <code>-f</code>, in the order (<code>-o</code>) listed on the command line:</p>
<pre><code>mlr --csv cut -o -f col3,col1 in.csv &gt; out.csv
</code></pre>
<p>To read the list of columns from a file, 1 column name per line:</p>
<pre><code>mlr --csv cut -o -f $(paste -s -d, columns.txt) in.csv &gt; out.csv
</code></pre>
<p><strong>See also:</strong></p>
<p>Note that <a href="https://github.com/johnkerl/miller" rel="nofollow noreferrer"><code>miller</code></a> can be easily installed, for example, using <a href="https://conda.io/projects/conda/en/latest/user-guide/install/index.html" rel="nofollow noreferrer"><code>conda</code></a>, specifically <code>miniconda</code>, like so:</p>
<pre><code>conda create --channel conda-forge --name miller miller
</code></pre>

CC BY-SA 4.0
2023-11-07T18:53:40.147Z
2023-11-07T18:53:40.147Z
Timur Shtatland
<p>It's really just <code>count[,col$V1]</code>. Please learn about basic subsetting of columns and rows, it's simple yet powerful.</p>

CC BY-SA 4.0
2023-11-08T13:02:17.227Z
thanks but it is not giving me CpG ID in first column.
bioinfonext
CC BY-SA 4.0
2023-11-09T06:16:59.120Z
There you see that you're not investing time into understanding an answer. What this here does is to provide names to the object to be filtered using the inbuilt R filtering operator. If you need the names plus something else then add this. Like `c(cpg_name, col$V1)`. Show some effort.
ATpoint
CC BY-SA 4.0
2023-11-12T10:02:54.750Z
Thanks @ATpoint, it is sorted now and with your command, I was also not getting column names.
bioinfonext
CC BY-SA 4.0
2023-11-08T05:51:01.347Z
2023-11-08T05:51:01.347Z
ATpoint
<p>Below code works perfectly of me.</p>
<pre class="lang-r prettyprint-override"><code>library(readr)
library(data.table)

df1 &lt;- read_csv(&quot;control.csv&quot;, col_types = cols(Chromosome = col_skip(), Start = col_skip(), End = col_skip(), Strand = col_skip()))

df1[1:5,1:5]
# A tibble: 5 × 5
  ID          DN54 DC146 DDN44 DC179
  &lt;chr&gt;      &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 cg26928153 0.848 0.879 0.872 0.897
2 cg16269199 0.731 0.732 0.759 0.775
3 cg13869341 0.842 0.863 0.819 0.896
4 cg24669183 0.824 0.866 0.822 0.910
5 cg26679879 0.425 0.363 0.420 0.358

col &lt;- unlist(fread(&quot;samples.csv&quot;, header=F))
col &lt;- c(&quot;ID&quot;, col)
extracted &lt;- df1[,col, with=F]

write.table(extracted, file=&quot;control.beta.txt&quot;,sep=&quot;,&quot;, quote=F, col.names=TRUE,row.names=FALSE)

df &lt;- read_csv(&quot;control.beta.txt&quot;)
df[1:5,1:5]
# A tibble: 5 × 5
  ID         DC103R DC110R DC115 DC120R
  &lt;chr&gt;       &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;
1 cg26928153  0.929  0.885 0.862  0.877
2 cg16269199  0.836  0.826 0.604  0.739
3 cg13869341  0.826  0.903 0.830  0.841
4 cg24669183  0.767  0.719 0.808  0.812
5 cg26679879  0.388  0.366 0.428  0.360
</code></pre>

CC BY-SA 4.0
2023-11-13T09:58:24.000Z
2023-11-13T16:37:24.150Z
2023-11-13T16:37:24.150Z
bioinfonext
<p>I have list of samples as a txt file and want to only keep these samples from this large file; column name is samples name in the large file.</p>
<pre class="lang-r prettyprint-override"><code>library(readr)
library(data.table)

df1 &lt;- read_csv(&quot;control.csv&quot;, col_types = cols(Chromosome = col_skip(), Start = col_skip(), End = col_skip(), Strand = col_skip()))

df1[1:5,1:5]
# A tibble: 5 × 5
  ID          DN54 DC146 DDN44 DC179
  &lt;chr&gt;      &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 cg26928153 0.848 0.879 0.872 0.897
2 cg16269199 0.731 0.732 0.759 0.775
3 cg13869341 0.842 0.863 0.819 0.896
4 cg24669183 0.824 0.866 0.822 0.910
5 cg26679879 0.425 0.363 0.420 0.358
</code></pre>

CC BY-SA 4.0
2023-11-07T17:53:41.720Z
What have you tried? This is a straightforward R operation.
Ram RS
CC BY-SA 4.0
2023-11-08T13:44:18.690Z
it is resolved now, I have updated my code above.
bioinfonext
CC BY-SA 4.0
2023-11-08T16:15:40.313Z
"I have updated my code above" - Don't do that, add an answer and accept it instead.
Ram RS
CC BY-SA 4.0
2023-11-12T10:04:04.963Z
Thanks Ram, I will add correct code as answer soon.
bioinfonext
<r><bash>
Filtering columns from large file based on a column list
# Filtering columns from large file based on a column list

(Asked by: username_0 on 2023-11-07)

I have list of samples as a txt file and want to only keep these samples from this large file; column name is samples name in the large file.
library(readr)
library(data.table)

df1 <- read_csv("control.csv", col_types = cols(Chromosome = col_skip(), Start = col_skip(), End = col_skip(), Strand = col_skip()))

df1[1:5,1:5]
# A tibble: 5 × 5
  ID          DN54 DC146 DDN44 DC179
           
1 cg26928153 0.848 0.879 0.872 0.897
2 cg16269199 0.731 0.732 0.759 0.775
3 cg13869341 0.842 0.863 0.819 0.896
4 cg24669183 0.824 0.866 0.822 0.910
5 cg26679879 0.425 0.363 0.420 0.358

**Tags:** r, bash
> **Comments:**
> **username_1**: What have you tried? This is a straightforward R operation.  
> **username_0**: it is resolved now, I have updated my code above.  
> **username_1**: "I have updated my code above" - Don't do that, add an answer and accept it instead.  
> **username_0**: Thanks Ram, I will add correct code as answer soon.  

---

## 3 answers

### Answer by username_2 on 2023-11-07. Score: 3

You can save time and memory by preprocessing your file before loading it into R.
Use miller toolkit to manipulate csv or tab-delimited files using column names.
Below is a one-liner that reads csv file, and writes columns specified in option -f, in the order (-o) listed on the command line:
mlr --csv cut -o -f col3,col1 in.csv > out.csv

To read the list of columns from a file, 1 column name per line:
mlr --csv cut -o -f $(paste -s -d, columns.txt) in.csv > out.csv

See also:
Note that miller can be easily installed, for example, using conda, specifically miniconda, like so:
conda create --channel conda-forge --name miller miller


---

### Answer by username_3 on 2023-11-08. Score: 3

It's really just `count[,col$V1]`. Please learn about basic subsetting of columns and rows, it's simple yet powerful.

> **Comments:**
> **username_0**: thanks but it is not giving me CpG ID in first column.  
> **username_3**: There you see that you're not investing time into understanding an answer. What this here does is to provide names to the object to be filtered using the inbuilt R filtering operator. If you need the names plus something else then add this. Like `c(cpg_name, col$V1)`. Show some effort.  
> **username_0**: Thanks @ATpoint, it is sorted now and with your command, I was also not getting column names.  

---

### Answer by username_0 on 2023-11-13. Score: 1

Below code works perfectly of me.
library(readr)
library(data.table)

df1 <- read_csv("control.csv", col_types = cols(Chromosome = col_skip(), Start = col_skip(), End = col_skip(), Strand = col_skip()))

df1[1:5,1:5]
# A tibble: 5 × 5
  ID          DN54 DC146 DDN44 DC179
           
1 cg26928153 0.848 0.879 0.872 0.897
2 cg16269199 0.731 0.732 0.759 0.775
3 cg13869341 0.842 0.863 0.819 0.896
4 cg24669183 0.824 0.866 0.822 0.910
5 cg26679879 0.425 0.363 0.420 0.358

col <- unlist(fread("samples.csv", header=F))
col <- c("ID", col)
extracted <- df1[,col, with=F]

write.table(extracted, file="control.beta.txt",sep=",", quote=F, col.names=TRUE,row.names=FALSE)

df <- read_csv("control.beta.txt")
df[1:5,1:5]
# A tibble: 5 × 5
  ID         DC103R DC110R DC115 DC120R
              
1 cg26928153  0.929  0.885 0.862  0.877
2 cg16269199  0.836  0.826 0.604  0.739
3 cg13869341  0.826  0.903 0.830  0.841
4 cg24669183  0.767  0.719 0.808  0.812
5 cg26679879  0.388  0.366 0.428  0.360


---

