用模式替换R中的字符串,并替换两个向量
假设我有两个向量,如下所示:用模式替换R中的字符串,并替换两个向量,r,string,vector,R,String,Vector,假设我有两个向量,如下所示: a <- c("this", "is", "test") b <- c("that", "was", "boy") a如果您愿意使用非基本包,stringi将在这里非常有效: stringi::stri_replace_all_fixed(string, a, b, vectorize_all = FALSE) #[1] "that was a story about a boy" 请注意,对于长度大于1的输入字符串,这种方法也同样适用 为了安全起见
a <- c("this", "is", "test")
b <- c("that", "was", "boy")
a如果您愿意使用非基本包,stringi
将在这里非常有效:
stringi::stri_replace_all_fixed(string, a, b, vectorize_all = FALSE)
#[1] "that was a story about a boy"
请注意,对于长度大于1的输入字符串,这种方法也同样适用
为了安全起见,您可以调整此选项(类似于RUser的答案),以在替换之前检查单词边界:
stri_replace_all_regex(string, paste0("\\b", a, "\\b"), b, vectorize_all = FALSE)
例如,这将确保您不会意外地将his
替换为hwas
。以下是一些解决方案。即使string
是字符串的字符向量,在这种情况下,将对字符串的每个组成部分进行替换,它们都将工作
> library(stringi)
> stri_replace_all_regex(string, "\\b" %s+% a %s+% "\\b", b, vectorize_all=FALSE)
#[1] "that was a story about a boy"
1)减少这不使用包
Reduce(function(x, i) gsub(paste0("\\b", a[i], "\\b"), b[i], x), seq_along(a), string)
## [1] "that was a story about a boy"
2)gsubfngsubfn
类似于gsub
,但替换参数可以是替换(或某些其他对象)的列表
3)循环这不是矢量化的,而是为了比较而添加的。没有使用任何软件包
out <- string
for(i in seq_along(a)) out <- gsub(paste0("\\b", a[i], "\\b"), b[i], out)
out
## [1] "that was a story about a boy"
out插入一个仅依赖于R base的小功能
:
repWords <- function(string,toRep,Rep,sep='\\s'){
wrds <- unlist(strsplit(string,sep))
ix <- match(toRep,wrds)
wrds[ix] <- Rep
return(paste0(wrds,collapse = ' '))
}
a <- c("this", "is", "test")
b <- c("that", "was", "boy")
string <- "this is a story about a test"
> repWords(string,a,b)
[1] "that was a story about a boy"
repWords谈到外部软件包,这里还有一个:
a <- c("this", "is", "test")
b <- c("that", "was", "boy")
x <- "this is a story about a test"
library(qdap)
mgsub(a,b,x)
美好的我还没有接触到减少。非常感谢。我添加了一个关于cyclesAlsoregmatches(string,gregexpr(粘贴(a,collapse=“|”)的注释,string))@G.Grothendieck-我也说过类似的话。当然,它也需要\\b
来指定单词边界,就像gsub
解决方案一样。@LateMail,它更长更复杂,它覆盖输入并修复它,使其工作方式类似于(2)正则表达式比(2)中的复杂得多。它的优点是不使用任何包。很好mgsub
可以让生活看起来更轻松一些如果a
out <- string
for(i in seq_along(a)) out <- gsub(paste0("\\b", a[i], "\\b"), b[i], out)
out
## [1] "that was a story about a boy"
a <- c("a", "A")
b <- rev(a)
# swap "a" and "A"
a <- c("a", "A")
b <- rev(a)
tmp <- gsubfn("\\w+", setNames(as.list(seq_along(a)), a), string)
gsubfn("\\w+", setNames(as.list(b), seq_along(a)), tmp)
## [1] "this is A story about A test"
repWords <- function(string,toRep,Rep,sep='\\s'){
wrds <- unlist(strsplit(string,sep))
ix <- match(toRep,wrds)
wrds[ix] <- Rep
return(paste0(wrds,collapse = ' '))
}
a <- c("this", "is", "test")
b <- c("that", "was", "boy")
string <- "this is a story about a test"
> repWords(string,a,b)
[1] "that was a story about a boy"
a <- c("this", "is", "test")
b <- c("that", "was", "boy")
x <- "this is a story about a test"
library(qdap)
mgsub(a,b,x)
"that was a story about a boy"