stackoverflow上的第一篇文章,希望也是众多文章中的第一篇。
我正在清理一个数据集,该数据集的一个列中包含作者列表。当有多个作者时,这些作者之间用&符号分隔,例如。Smith & Banks。然而,间距并不总是一致的,例如。Smith & Banks,Smith &Banks。
为了解决这个问题,我尝试了:
gsub('\\S&','\\S &', dataset[,author.col])这就给了史密斯银行-> SmitS & Banks。我怎样才能联系到->史密斯银行?
发布于 2016-07-14 20:35:20
使用stringi的过度杀伤力
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])这就给出了:
#[1] "Smith & Banks" "Smith & Banks" "Smith & Banks" "Smith & Banks" "Smith Banks" https://stackoverflow.com/questions/38374139
复制相似问题