<p><em>Answer from @stupidwolf, converted from comment:</em></p>
<p>You cannot use grepl with a vector, it must be a pattern.</p>
<p>I can suggest two things and you just put them together i hope, to know if something is found in a vector, you can use <code>data$Sample %in% c(&quot;TCGA-IC-A6RE&quot;, &quot;TCGA-IG-A4QS&quot;)</code> etc ..</p>
<p>Of course <code>data$Sample</code> will not be in there, you need a bit of manipulation, so since your samples follow a certain pattern, you can use <code>substr(data$Sample,....)</code> . If you do <code>substr(data$Sample,....) %in% c(.....) </code>, this will give you the selection.</p>

CC BY-SA 4.0
2021-05-23T13:17:53.187Z
2021-05-23T13:17:53.187Z
gringer
<p>When the string follows certain format there is no need for partial match, see this example:</p>

<pre><code># example data
data &lt;- read.table(text = "
Sample
TCGA-VR-A8EX-01A-11D-A36I-01
TCGA-L7-A56G-01A-21D-A25X-01
TCGA-VR-A8ER-01A-11D-A36I-01
TCGA-JY-A6FB-01A-11D-A33D-01
", header = TRUE, stringsAsFactors = FALSE)

x &lt;- c("TCGA-VR-A8EX", "TCGA-VR-A8ER")

data[ substr(data$Sample, 1, 12) %in% x, "Sample", drop = FALSE]
#                         Sample
# 1 TCGA-VR-A8EX-01A-11D-A36I-01
# 3 TCGA-VR-A8ER-01A-11D-A36I-01
</code></pre>

<p>Or when sample IDs lengths are not the same we can split on "-" then paste it back first 3 items, then subset as usual with <code>%in%</code>:</p>

<pre><code>data[ sapply(strsplit(data$Sample, split = "-"), 
             function(i) paste(i[1:3], collapse = "-")
             ) %in% x, "Sample", drop = FALSE]
#                         Sample
# 1 TCGA-VR-A8EX-01A-11D-A36I-01
# 3 TCGA-VR-A8ER-01A-11D-A36I-01
</code></pre>

CC BY-SA 4.0
2020-05-22T07:56:01.127Z
2020-05-22T07:56:01.127Z
zx8754
<p>I have a data frame one column is sample name like</p>
<pre><code>Sample


TCGA-VR-A8EX-01A-11D-A36I-01
TCGA-L7-A56G-01A-21D-A25X-01
TCGA-VR-A8ER-01A-11D-A36I-01
TCGA-JY-A6FB-01A-11D-A33D-01
TCGA-2H-A9GO-01A-11D-A37B-01
TCGA-Z6-AAPN-01A-11D-A402-01
TCGA-L5-A4OP-01A-11D-A25X-01
TCGA-L5-A8NV-01A-11D-A37B-01
TCGA-R6-A8W5-01B-11D-A37B-01
TCGA-S8-A6BW-01A-11D-A31T-01
TCGA-R6-A6Y0-01B-11D-A33D-01
TCGA-R6-A8WC-01A-11D-A37B-01
TCGA-S8-A6BV-01A-21D-A31T-01
TCGA-L5-A4OJ-01A-11D-A25X-01
TCGA-LN-A7HV-01A-21D-A350-01
</code></pre>
<p>I want to subset samples which have partial match with these name</p>
<pre><code>TCGA-IC-A6RE
TCGA-IG-A4QS
TCGA-JY-A6F8
TCGA-JY-A6FB
TCGA-L5-A43E
TCGA-L5-A4OG
TCGA-L5-A4OH
TCGA-L5-A4OJ
</code></pre>
<p>I have used</p>
<pre><code>data[grepl(c(&quot;TCGA-IC-A6RE&quot;, &quot;TCGA-IG-A4QS&quot;, &quot;TCGA-JY-A6F8&quot;, &quot;TCGA-JY-A6FB&quot;, &quot;TCGA-L5-A43E&quot;, &quot;TCGA-L5-A4OG&quot;, &quot;TCGA-L5-A4OH&quot;, &quot;TCGA-L5-A4OJ&quot;, &quot;TCGA-L5-A4ON&quot;, &quot;TCGA-L5-A4OS&quot;, &quot;TCGA-L5-A4OW&quot;, &quot;TCGA-L5-A4OX&quot;, &quot;TCGA-L5-A88T&quot;, &quot;TCGA-Q9-A6FW&quot;),data$Sample),]
</code></pre>
<p>But says that</p>
<pre><code>Warning message:
In grepl(c(&quot;TCGA-IC-A6RE&quot;, &quot;TCGA-IG-A4QS&quot;, &quot;TCGA-JY-A6F8&quot;, &quot;TCGA-JY-A6FB&quot;,  :
  argument 'pattern' has length &gt; 1 and only the first element will be used
</code></pre>
<p>Likely only the first entry from the vector being grepped</p>
<p>Edit: As @StupidWolf suggested, I have tried:</p>
<pre><code>&gt; data[substr(data$Sample, 1, 12) %in% 
    c(&quot;TCGA-IC-A6RE&quot;, &quot;TCGA-IG-A4QS&quot;, &quot;TCGA-JY-A6F8&quot;, &quot;TCGA-JY-A6FB&quot;,
      &quot;TCGA-L5-A43E&quot;, &quot;TCGA-L5-A4OG&quot;, &quot;TCGA-L5-A4OH&quot;, &quot;TCGA-L5-A4OJ&quot;,
      &quot;TCGA-L5-A4ON&quot;, &quot;TCGA-L5-A4OS&quot;, &quot;TCGA-L5-A4OW&quot;, &quot;TCGA-L5-A4OX&quot;,
      &quot;TCGA-L5-A88T&quot;, &quot;TCGA-Q9-A6FW&quot;),]
</code></pre>
<p>This works, thanks.</p>

<r>
Selecting rows by partial match
# Selecting rows by partial match

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

I have a data frame one column is sample name like
Sample


TCGA-VR-A8EX-01A-11D-A36I-01
TCGA-L7-A56G-01A-21D-A25X-01
TCGA-VR-A8ER-01A-11D-A36I-01
TCGA-JY-A6FB-01A-11D-A33D-01
TCGA-2H-A9GO-01A-11D-A37B-01
TCGA-Z6-AAPN-01A-11D-A402-01
TCGA-L5-A4OP-01A-11D-A25X-01
TCGA-L5-A8NV-01A-11D-A37B-01
TCGA-R6-A8W5-01B-11D-A37B-01
TCGA-S8-A6BW-01A-11D-A31T-01
TCGA-R6-A6Y0-01B-11D-A33D-01
TCGA-R6-A8WC-01A-11D-A37B-01
TCGA-S8-A6BV-01A-21D-A31T-01
TCGA-L5-A4OJ-01A-11D-A25X-01
TCGA-LN-A7HV-01A-21D-A350-01

I want to subset samples which have partial match with these name
TCGA-IC-A6RE
TCGA-IG-A4QS
TCGA-JY-A6F8
TCGA-JY-A6FB
TCGA-L5-A43E
TCGA-L5-A4OG
TCGA-L5-A4OH
TCGA-L5-A4OJ

I have used
data[grepl(c("TCGA-IC-A6RE", "TCGA-IG-A4QS", "TCGA-JY-A6F8", "TCGA-JY-A6FB", "TCGA-L5-A43E", "TCGA-L5-A4OG", "TCGA-L5-A4OH", "TCGA-L5-A4OJ", "TCGA-L5-A4ON", "TCGA-L5-A4OS", "TCGA-L5-A4OW", "TCGA-L5-A4OX", "TCGA-L5-A88T", "TCGA-Q9-A6FW"),data$Sample),]

But says that
Warning message:
In grepl(c("TCGA-IC-A6RE", "TCGA-IG-A4QS", "TCGA-JY-A6F8", "TCGA-JY-A6FB",  :
  argument 'pattern' has length > 1 and only the first element will be used

Likely only the first entry from the vector being grepped
Edit: As @StupidWolf suggested, I have tried:
> data[substr(data$Sample, 1, 12) %in% 
    c("TCGA-IC-A6RE", "TCGA-IG-A4QS", "TCGA-JY-A6F8", "TCGA-JY-A6FB",
      "TCGA-L5-A43E", "TCGA-L5-A4OG", "TCGA-L5-A4OH", "TCGA-L5-A4OJ",
      "TCGA-L5-A4ON", "TCGA-L5-A4OS", "TCGA-L5-A4OW", "TCGA-L5-A4OX",
      "TCGA-L5-A88T", "TCGA-Q9-A6FW"),]

This works, thanks.

**Tags:** r

---

## 2 answers

### Answer by username_1 on 2021-05-23. Score: 1 | Accepted answer

*Answer from @stupidwolf, converted from comment:*

You cannot use grepl with a vector, it must be a pattern.

I can suggest two things and you just put them together i hope, to know if something is found in a vector, you can use `data$Sample %in% c("TCGA-IC-A6RE", "TCGA-IG-A4QS")` etc ..

Of course `data$Sample` will not be in there, you need a bit of manipulation, so since your samples follow a certain pattern, you can use `substr(data$Sample,....)` . If you do `substr(data$Sample,....) %in% c(.....)` , this will give you the selection.


---

### Answer by username_2 on 2020-05-22. Score: 0

When the string follows certain format there is no need for partial match, see this example:
# example data
data <- read.table(text = "
Sample
TCGA-VR-A8EX-01A-11D-A36I-01
TCGA-L7-A56G-01A-21D-A25X-01
TCGA-VR-A8ER-01A-11D-A36I-01
TCGA-JY-A6FB-01A-11D-A33D-01
", header = TRUE, stringsAsFactors = FALSE)

x <- c("TCGA-VR-A8EX", "TCGA-VR-A8ER")

data[ substr(data$Sample, 1, 12) %in% x, "Sample", drop = FALSE]
#                         Sample
# 1 TCGA-VR-A8EX-01A-11D-A36I-01
# 3 TCGA-VR-A8ER-01A-11D-A36I-01

Or when sample IDs lengths are not the same we can split on "-" then paste it back first 3 items, then subset as usual with %in%:
data[ sapply(strsplit(data$Sample, split = "-"), 
             function(i) paste(i[1:3], collapse = "-")
             ) %in% x, "Sample", drop = FALSE]
#                         Sample
# 1 TCGA-VR-A8EX-01A-11D-A36I-01
# 3 TCGA-VR-A8ER-01A-11D-A36I-01


---

