text: user4595: 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.
  
  user-1: Not strictly bioinformatics, but a common problem:
  grep assumes a regular expression, not a vector of patterns.
  Assuming your patterns are stored as a vector, so e.g.
  patt <- c("pattern1", "pattern2", "pattern3")
  
  you first have to write them as a regex:
  patt.regex <- paste(patt, collapse = "|")
  
  Output of patt.regex is then "pattern1|pattern2|pattern3" which is now ready for grep or grepl:
  grep(patt.regex, data$Sample)
  
  For exact matches it would be paste(paste0("^", patt, "$"), collapse = "|")
  to get "^pattern1$|^pattern2$|^pattern3$".
  
original_text: 4595: <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>
   -1: <p>Not strictly bioinformatics, but a common problem:</p>
  
  <p><code>grep</code> assumes a regular expression, not a vector of patterns.
  Assuming your patterns are stored as a vector, so e.g.</p>
  
  <pre><code>patt &lt;- c("pattern1", "pattern2", "pattern3")
  </code></pre>
  
  <p>you first have to write them as a regex:</p>
  
  <pre><code>patt.regex &lt;- paste(patt, collapse = "|")
  </code></pre>
  
  <p>Output of <code>patt.regex</code> is then <code>"pattern1|pattern2|pattern3"</code> which is now ready for <code>grep</code> or <code>grepl</code>:</p>
  
  <pre><code>grep(patt.regex, data$Sample)
  </code></pre>
  
  <p>For exact matches it would be <code>paste(paste0("^", patt, "$"), collapse = "|")</code>
  to get <code>"^pattern1$|^pattern2$|^pattern3$"</code>.</p>
  