用字符串中的单个空格替换所有双空格

时间:2022-05-17 16:48:27

I would like to reduce all spaces longer than a single space in a string to a single space.

我想将比字符串中的单个空格更长的所有空间减少到单个空格。

foo(" 1   2    3     4                 5 ")
[1] " 1 2 3 4 5 "

1 个解决方案

#1


5  

That would work:

这会奏效:

gsub(' +',' ',foo) 

:)

(edited to take into account Zach's comment)

(编辑以考虑扎克的评论)

#1


5  

That would work:

这会奏效:

gsub(' +',' ',foo) 

:)

(edited to take into account Zach's comment)

(编辑以考虑扎克的评论)