Noobie Noobie -4 years ago 56
R Question

How to remove multiple pipes in data frame?

Here is simple example

library(dplyr)
library(stringr)

> dataf <- data_frame(text = c('this is a pip||e |' ,
+ 'this is |||'))
> dataf
# A tibble: 2 x 1
text
<chr>
1 this is a pip||e |
2 this is |||


I want to replace all the pipes in the data with an empty string. Basically I want them to disappear. However, I am only able to get rid of one of them at a time:

> dataf %>% mutate(text = str_replace(text, '\\|+', ""))
# A tibble: 2 x 1
text
<chr>
1 this is a pipe |
2 this is


What is wrong here?
Thanks!

Answer Source

You can use str_replace_all from stringr to remove all the matched patterns:

dataf %>% mutate(text = str_replace_all(text, '\\|', ""))

# A tibble: 2 × 1
#            text
#           <chr>
#1 this is a pipe
#2       this is 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download