Using the GREP Function

3

Hello,

I have a .csv file with the zip code in the middle of the address, in square brackets. In some cases there is no zip, but there is always the [] set, possibly empty, for example:

RUA ESTEVAM DE ARAÚJO DE ALMEIDA 521 L 17 Q. 15 [23028730] GUARATIBA

I want this information in a new variable. For this, I wrote an R code to extract the zip through the grep function, but it is generating an error:

hans$cep <- grep("\[*?\d{8}\]", hans$endereco.do.domicilio, value = T)

Error in '$<-.data.frame'('*tmp*', "cep", value = c("RUA ESTEVAM DE ARAÚJO DE ALMEIDA 521 L 17 Q. 15 [23028730] GUARATIBA",  : 
  replacement has 59940 rows, data has 61674
    
asked by anonymous 17.12.2015 / 19:20

2 answers

0

Use this function. CEP=$(echo $endereco_completo | TR "\[" "\n" | grep ']' | cut -c1-8)

Pay attention to the quotation marks, as it may give an error if the grep is "or winter."

    
12.02.2016 / 17:10
0

Good morning, try using the following code

library(tidyverse)
hans <- hans  %>%
   mutate(CEP = str_extract(endereco.do.domicilio, "\[*?\d{8}\]")
    
26.04.2018 / 14:38