Replace ")" by space using str_replace_all() in R

Asked

Viewed 504 times

7

I got the following data.frame:

cadastros <- data.frame(
  email = c('[email protected]', '[email protected]', '[email protected]', '[email protected]'),
  telefone = c('(61)99831-9482', '32 8976 2913', '62-9661-1234', '15-40192.5812')
)

I need to replace all ")" and "(" with blank spaces. I have tried everything that is possible and imaginable. I will put next each of the attempts with the respective mistakes:

First attempt

  > library(stringr)
    > str_replace_all(string = cadastros,
    +                 pattern = ")",
    +                 replacement = "[[:blank:]]")
    Error in stri_replace_all_regex(string, pattern, fix_replacement(replacement),  : 
      Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)
    In addition: Warning message:
    In stri_replace_all_regex(string, pattern, fix_replacement(replacement),  :
      argument is not an atomic vector; coercingstr_replace(cadastros, 'c', " ")

Second attempt

> cadastros2<-cadastros%>%
> +     str_replace_all(cadastros,"6", replacement = "[[:blank:]]") Error in str_replace_all(., cadastros, "6", replacement =
> "[[:blank:]]") :    unused argument ("6")

Third attempt

> str_replace_all(cadastros, pattern = ")",replacement = "[[:blank:]]")
Error in stri_replace_all_regex(string, pattern, fix_replacement(replacement),  : 
  Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)
In addition: Warning message:
In stri_replace_all_regex(string, pattern, fix_replacement(replacement),  :
  argument is not an atomic vector; coercing

Fourth attempt

> str_replace_all(cadastros, pattern = ")","[[:blank:]]")
Error in stri_replace_all_regex(string, pattern, fix_replacement(replacement),  : 
  Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)
In addition: Warning message:
In stri_replace_all_regex(string, pattern, fix_replacement(replacement),  :
  argument is not an atomic vector; coercing

Any help is welcome.

2 answers

4

You can use the function gsub():

gsub("\\(|\\)", " ", cadastros$telefone)
# [1] " 61 99831-9482" "32 8976 2913"   "62-9661-1234"   "15-40192.5812"

The first argument you provide the pattern you want to replace and the second will be the substitute. Note that I used | to provide two patterns at the same time. This avoids writing the function for each parenthesis: gsub(" (", "", "entries$phone) gsub(" )", " ", cadastre$phone)

To maintain a consistent pattern with all the numbers you provided in the example, I would do so:

cadastros$telefone <- gsub("\\(", "", cadastros$telefone)
cadastros$telefone <- gsub("\\)|\\-|\\.", " ", cadastros$telefone)

cadastros
# email      telefone
# 1      [email protected] 61 99831 9482
# 2         [email protected]  32 8976 2913
# 3           [email protected]  62 9661 1234
# 4 [email protected] 15 40192 5812

3


In stringr you need to insert [] within the functions for the character to be recognized. This would look like this for the variable telefone:

library(stringr)

str_replace_all(cadastros$telefone,'[()]',' ') # o último argumento é o espaço a ser inserido: ' '

#[1] " 61 99831-9482" "32 8976 2913"   "62-9661-1234"   "15-40192.5812" 

To apply this function to all variables (as you tried), do:

library(magrittr)

cadastros %>%
    sapply(function(x){
        str_replace_all(x,'[()]',' ')
    })

    # email                        telefone        
#[1,] "[email protected]"      " 61 99831-9482"
#[2,] "[email protected]"         "32 8976 2913"  
#[3,] "[email protected]"           "62-9661-1234"  
#[4,] "[email protected]" "15-40192.5812" 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.