replace double or tripple space with single space

How to replace space in string if string have more than one space. for example i have data frame on names column, name is like last name then "," and first name and if there is space more than one then it should replace by single space.

df7 <- data.frame(name = c("ttt, hbue","mna, huna","vga,  pahk","hja, dhyn","cct, indo","nhu,   nhuv"))

the data frame have two or three spaces in between first name and second name and i want to replace spaces with single space


Try with str_replace() (in stringr), and use a regular expression with + to say any number of spaces:

str_replace("a,        ss", " +", " ")
#> [1] "a, ss"

also if i want to truncate like if the cell have more than 25 character then i need to truncate it to max 25.

substr(xxx, 1, 25)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.