删除 R 中的可变长度空格
Removing variable length whitespaces in R
我有一列产品,其中产品描述中的字符串之间有可变长度的白色 spaces。
以下是其中一款产品的示例:
"This is a product from the product column"
"Here is another example from the product column"
我希望字符串看起来像这样,其中字符串之间只有一个白色 space:
"This is a product from the product column"
可以实现吗
要替换任何重复的 spaces \s
(连续超过一个),匹配任何时候有两个或更多 spaces \s{2,}
并替换它们只有一个 space。 {2,}
表示匹配重复次数大于两次。我们可以像这样设置最大重复次数 {2,7}
.
gsub("\s{2,}", " ", "string with many spaces")
要对列名执行此操作,只需将要替换的字符串替换为 colnames(df)
。
我有一列产品,其中产品描述中的字符串之间有可变长度的白色 spaces。
以下是其中一款产品的示例:
"This is a product from the product column"
"Here is another example from the product column"
我希望字符串看起来像这样,其中字符串之间只有一个白色 space:
"This is a product from the product column"
可以实现吗要替换任何重复的 spaces \s
(连续超过一个),匹配任何时候有两个或更多 spaces \s{2,}
并替换它们只有一个 space。 {2,}
表示匹配重复次数大于两次。我们可以像这样设置最大重复次数 {2,7}
.
gsub("\s{2,}", " ", "string with many spaces")
要对列名执行此操作,只需将要替换的字符串替换为 colnames(df)
。