Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove extra blank characters between letters?

Tags:

r

my dataset

id   data
1    C H I C A G O  I L
2    M A D I S O N  W I
3    N E W  Y O R K  N Y

there is one blank character between letters in a word and 2 blank characters between words. im requiring to remove them

id   data
1    CHICAGO IL
2    MADISON WI
3    NEW YORK NY
like image 891
Andres Mora Avatar asked Dec 09 '22 23:12

Andres Mora


1 Answers

We may use

library(stringr)
library(dplyr)
df1 %>%
   mutate(data = str_replace_all(str_remove_all(data, 
      "(?<=\\S)\\s{1}(?=\\S)"), "\\s+", " "))

-output

id         data
1  1   CHICAGO IL
2  2   MADISON WI
3  3  NEW YORK NY

data

df1 <- structure(list(id = 1:3, data = c("C H I C A G O  I L", "M A D I S O N  W I", 
" N E W  Y O R K  N Y")), class = "data.frame", row.names = c(NA, 
-3L))
like image 185
akrun Avatar answered Jan 07 '23 10:01

akrun