<p>You can use <code>strsplit</code>.</p>

<pre><code>ciao &lt;- "LP2000104-DNA_A01_vs_LP2000101-DNA_A01"
sampleIneed &lt;- "LP6008334-DNA_A03"   
sampleIneed %in% strsplit(ciao,"_vs_")[[1]]
[1] FALSE

"LP2000104-DNA_A01" %in% strsplit(ciao,"_vs_")[[1]]
[1] TRUE
</code></pre>

<p>You can loop (e.g. <code>sapply</code>or <code>lapply</code>) through the element of the first column to extract only the rows you need.</p>

<p>You can also simply use <code>grep</code> on the first column</p>

<pre><code>ciao &lt;_ your_csv
grep("LP2000104-DNA_A01", ciao$pair)
</code></pre>

CC BY-SA 4.0
2020-03-05T13:03:09.640Z
2020-03-05T13:03:09.640Z
fra
<p>I have a .csv file like this</p>

<pre><code>pair,S17A,S2..APOBEC.,S1..age.,S18.like,S3..BRCA.,S17B
LP2000104-DNA_A01_vs_LP2000101-DNA_A01,0.448027080410302,0,0,0,0,0.410602648202438
LP2000325-DNA_A01_vs_LP2000317-DNA_A01,0.343096214261112,0,0.111190317814775,0.171937435363758,0.187395051556141,0.156209735124657
LP2000327-DNA_A01_vs_LP2000319-DNA_A01,0.436249785689912,0,0.066118729867486,0.077392575088635,0.116384601626772,0.263251836199875
</code></pre>

<p>I have a list of samples which are a part of first column for example</p>

<pre><code>LP6005334-DNA_H01
LP6008334-DNA_A03
LP6008334-DNA_A04
LP6008334-DNA_B02
LP6008334-DNA_C02
LP6008334-DNA_D02
LP6008336-DNA_H01
</code></pre>

<p>I want to extract .csv for this list of samples</p>

<p>I can not think of intersect function as these samples are a part of sample name not full name</p>

<p>Can you help?</p>

<r>
Extracting part of a csv by a list
# Extracting part of a csv by a list

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

I have a .csv file like this
pair,S17A,S2..APOBEC.,S1..age.,S18.like,S3..BRCA.,S17B
LP2000104-DNA_A01_vs_LP2000101-DNA_A01,0.448027080410302,0,0,0,0,0.410602648202438
LP2000325-DNA_A01_vs_LP2000317-DNA_A01,0.343096214261112,0,0.111190317814775,0.171937435363758,0.187395051556141,0.156209735124657
LP2000327-DNA_A01_vs_LP2000319-DNA_A01,0.436249785689912,0,0.066118729867486,0.077392575088635,0.116384601626772,0.263251836199875

I have a list of samples which are a part of first column for example
LP6005334-DNA_H01
LP6008334-DNA_A03
LP6008334-DNA_A04
LP6008334-DNA_B02
LP6008334-DNA_C02
LP6008334-DNA_D02
LP6008336-DNA_H01

I want to extract .csv for this list of samples
I can not think of intersect function as these samples are a part of sample name not full name
Can you help?

**Tags:** r

---

## 1 answers

### Answer by username_1 on 2020-03-05. Score: 1

You can use strsplit.
ciao <- "LP2000104-DNA_A01_vs_LP2000101-DNA_A01"
sampleIneed <- "LP6008334-DNA_A03"   
sampleIneed %in% strsplit(ciao,"_vs_")[[1]]
[1] FALSE

"LP2000104-DNA_A01" %in% strsplit(ciao,"_vs_")[[1]]
[1] TRUE

You can loop (e.g. sapplyor lapply) through the element of the first column to extract only the rows you need.
You can also simply use grep on the first column
ciao <_ your_csv
grep("LP2000104-DNA_A01", ciao$pair)


---

