首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >gsub -在字符之前/之后添加空格

gsub -在字符之前/之后添加空格
EN

Stack Overflow用户
提问于 2016-07-14 20:25:46
回答 6查看 816关注 0票数 2

stackoverflow上的第一篇文章,希望也是众多文章中的第一篇。

我正在清理一个数据集,该数据集的一个列中包含作者列表。当有多个作者时,这些作者之间用&符号分隔,例如。Smith & Banks。然而,间距并不总是一致的,例如。Smith & Banks,Smith &Banks。

为了解决这个问题,我尝试了:

代码语言:javascript
复制
     gsub('\\S&','\\S &', dataset[,author.col])

这就给了史密斯银行-> SmitS & Banks。我怎样才能联系到->史密斯银行?

EN

Stack Overflow用户

发布于 2016-07-14 20:35:20

使用stringi的过度杀伤力

代码语言:javascript
复制
v <- c("Smith & Banks", "Smith& Banks", "Smith &Banks", "Smith&Banks", "Smith Banks")

library(stringi)
#create an index of entries containing "&"
indx <- grepl("&", v)
#subset "v" using that index
amp  <- v[indx]
#perform the transformation on that subset and combine the result with the rest of "v"
c(sapply(stri_extract_all_words(amp), 
         function(x) { paste0(x, collapse = " & ") }), v[!indx])

这就给出了:

代码语言:javascript
复制
#[1] "Smith & Banks" "Smith & Banks" "Smith & Banks" "Smith & Banks" "Smith Banks" 
票数 2
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38374139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档