本文介绍了使用gsub()从R中的字母之间删除多余的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何从单词之间删除多余的空格有很多答案,这非常简单.但是,我发现在单词中删除多余的空格要困难得多.作为可重现的示例,假设我有一个数据载体,看起来像这样:

There are a slew of answers out there on how to remove extra whitespace from between words, which is super simple. However, I'm finding that removing extra whitespace within words is much harder. As a reproducible example, let's say I have a vector of data that looks like this:

x <- c("L L C", "P O BOX 123456", "NEW YORK")

我想做的是这样的:

y <- gsub("(\\w)(\\s)(\\w)(\\s)", "\\1\\3", x)

但是,这让我有了:

[1] "LLC" "POBOX 123456" "NEW YORK"

几乎是完美的,但我真的很想让第二个值说"PO BOX 123456".有比我正在做的更好的方法吗?

Almost perfect, but I'd really like to have that second value say "PO BOX 123456". Is there a better way to do this than what I'm doing?

推荐答案

您可以尝试一下,

> x <- c("L L C", "P O BOX 123456", "NEW YORK")
> gsub("(?<=\\b\\w)\\s(?=\\w\\b)", "", x,perl=T)
[1] "LLC"           "PO BOX 123456" "NEW YORK" 

它只是删除两个单词字符之间存在的空格.

It just removes the space which exists between two single word characters.

这篇关于使用gsub()从R中的字母之间删除多余的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:30