<p><code>sub()</code> is the way to go.</p>

<p>Your pattern is your regular expression: Check for "_vs_" followed by any character (provided with <code>.</code>) for an undefined length (provided with <code>*</code>).</p>

<pre><code>my_text &lt;- "LP2000107-DNA_A01_vs_LP2000102-DNA_A01"

sub(pattern = "_vs_.*",
    replacement = "",
    x = my_text)

[1] "LP2000107-DNA_A01"
</code></pre>

CC BY-SA 4.0
2020-01-20T13:43:13.543Z
Thank you, how I execute this on my data frame?
Zizogolu
CC BY-SA 4.0
2020-01-20T13:50:34.797Z
`your_df$your_col <- sub(your_pattern, replacement_string, your_df$your_col)`
haci
CC BY-SA 4.0
2020-01-20T20:37:15.867Z
For learners, it is always better to use parameters in the `name = value` format. I'd recommend `sub(pattern = your_pattern, replacement = replacement_string, x = your_df$your_col)`, so everything is explicit and the order of the parameters doesn't matter.
Ram RS
CC BY-SA 4.0
2020-01-20T20:47:12.743Z
@Ram RS, I agree and that is why the `sub()` bit is written that way in the original answer. And actually I try to follow Hadley Wickham's style guide, in which the `name = value` format is used from the second parameter on. `sub()`, however, unconventionally (at least for me) takes `x` as the third argument, hence I used the `name = value` pairs for all of the arguments used. For those who are interested, here is the link for the tidyverse style guide: https://style.tidyverse.org
haci
CC BY-SA 4.0
2020-01-21T15:13:04.650Z
You're right, `sub`, `gsub`, `grep`, etc have weird parameter ordering and hence highly benefit from this format.
Ram RS
CC BY-SA 4.0
2020-01-20T13:32:21.160Z
2020-01-20T13:32:21.160Z
haci
<p>I have a too big data frame</p>

<pre><code>&gt; dim(anno_maf)
[1] 2635299     132
&gt;

&gt; head(anno_maf[1:3,1:4])
                     Tumor_Sample_Barcode Chromosome Start_Position End_Position
1: LP2000107-DNA_A01_vs_LP2000102-DNA_A01       chr5      137536800    137536803
2: LP2000330-DNA_A01_vs_LP2000316-DNA_A01      chr11       64012229     64012231
3: LP2000330-DNA_A01_vs_LP2000316-DNA_A01      chr17       41720917     41720917
&gt; 

&gt; unique(anno_maf$Tumor_Sample_Barcode)
  [1] "LP2000107-DNA_A01_vs_LP2000102-DNA_A01" "LP2000330-DNA_A01_vs_LP2000316-DNA_A01" "LP2000332-DNA_A01_vs_LP2000323-DNA_A01"
  [4] "LP2000333-DNA_A01_vs_LP2000324-DNA_A01" "LP6005334-DNA_A02_vs_LP6005333-DNA_A02" "LP6005334-DNA_C01_vs_LP6005333-DNA_C01"
</code></pre>

<p>I want to remove everything thing from <code>vs</code> onward in <code>anno_maf$Tumor_Sample_Barcode</code>, for example leaving <code>LP2000107-DNA_A01</code> from <code>LP2000107-DNA_A01_vs_LP2000102-DNA_A01</code> but I don't know how to do that</p>

<p>Any help?</p>

<r>
Chaning string in rows
# Chaning string in rows

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

I have a too big data frame
> dim(anno_maf)
[1] 2635299     132
>

> head(anno_maf[1:3,1:4])
                     Tumor_Sample_Barcode Chromosome Start_Position End_Position
1: LP2000107-DNA_A01_vs_LP2000102-DNA_A01       chr5      137536800    137536803
2: LP2000330-DNA_A01_vs_LP2000316-DNA_A01      chr11       64012229     64012231
3: LP2000330-DNA_A01_vs_LP2000316-DNA_A01      chr17       41720917     41720917
> 

> unique(anno_maf$Tumor_Sample_Barcode)
  [1] "LP2000107-DNA_A01_vs_LP2000102-DNA_A01" "LP2000330-DNA_A01_vs_LP2000316-DNA_A01" "LP2000332-DNA_A01_vs_LP2000323-DNA_A01"
  [4] "LP2000333-DNA_A01_vs_LP2000324-DNA_A01" "LP6005334-DNA_A02_vs_LP6005333-DNA_A02" "LP6005334-DNA_C01_vs_LP6005333-DNA_C01"

I want to remove everything thing from vs onward in anno_maf$Tumor_Sample_Barcode, for example leaving LP2000107-DNA_A01 from LP2000107-DNA_A01_vs_LP2000102-DNA_A01 but I don't know how to do that
Any help?

**Tags:** r

---

## 1 answers

### Answer by username_1 on 2020-01-20. Score: 4 | Accepted answer

sub() is the way to go.
Your pattern is your regular expression: Check for "_vs_" followed by any character (provided with .) for an undefined length (provided with *).
my_text <- "LP2000107-DNA_A01_vs_LP2000102-DNA_A01"

sub(pattern = "_vs_.*",
    replacement = "",
    x = my_text)

[1] "LP2000107-DNA_A01"

> **Comments:**
> **username_0**: Thank you, how I execute this on my data frame?  
> **username_1**: `your_df$your_col <- sub(your_pattern, replacement_string, your_df$your_col)`  
> **username_2**: For learners, it is always better to use parameters in the `name = value` format. I'd recommend `sub(pattern = your_pattern, replacement = replacement_string, x = your_df$your_col)`, so everything is explicit and the order of the parameters doesn't matter.  
> **username_1**: @Ram RS, I agree and that is why the `sub()` bit is written that way in the original answer. And actually I try to follow Hadley Wickham's style guide, in which the `name = value` format is used from the second parameter on. `sub()`, however, unconventionally (at least for me) takes `x` as the third argument, hence I used the `name = value` pairs for all of the arguments used. For those who are interested, here is the link for the tidyverse style guide: https://style.tidyverse.org  
> **username_2**: You're right, `sub`, `gsub`, `grep`, etc have weird parameter ordering and hence highly benefit from this format.  

---

