gpt4 book ai didi

在 R 中重新定位和复制字符串

转载 作者:行者123 更新时间:2023-12-04 12:37:07 25 4
gpt4 key购买 nike

我的目标是重新定位单词并以特定模式复制和粘贴它们。

a = 'blahblah (Peter|Sally|Tom)'
b = 'word (apple|grape|tomato) vocabulary (rice|mice|lice)'
c = 'people person (you|me|us) do not know how (it|them) works'

我可以使用 gsub

重新定位放置在 '(' 之前的字符串
gsub('\\s*(\\S+)\\s*\\(', '(\\1 ', a)

有了这个函数,我可以在下面制作字符串集。

a
[1]'(blahblah Peter|Sally|Tom)'
b
[1]'(word apple|grape|tomato) (vocabulary rice|mice|lice)'
c
[1]'people (person you|me|us) do not know (how it|them) works'

但是,我不知道如何复制 '\\1' 并像这样将其粘贴到 '|' 之后

a
[1]'(blahblah Peter|blahblah Sally|blahblah Tom)'
b
[1]'(word apple|word grape|word tomato) (vocabulary rice|vocabulary mice|vocabulary lice)'
c
[1]'people (person you|person me|person us) do not know (how it|how them) works'

有什么办法可以做到这一点吗?

最佳答案

我们可以使用strsplit

sapply(strsplit(a, "[| ]|\\(|\\)"), function(x) {
x1 <- x[nzchar(x)]
paste0("(", paste(x1[1], x1[-1], collapse="|"), ")")})
#[1] "(blahblah Peter|blahblah Sally|blahblah Tom)"

针对多种情况

paste(sapply(strsplit(b, "(?<=\\))\\s+", perl = TRUE)[[1]],
function(x) sapply(strsplit(x, "[| ]|\\(|\\)"), function(y) {
x1 <- y[nzchar(y)]
paste0("(", paste(x1[1], x1[-1], collapse="|"), ")") })), collapse=' ')
#[1] "(word apple|word grape|word tomato) (vocabulary rice|vocabulary mice|vocabulary lice)"

另一个选项是str_extract

library(stringr)
m1 <- matrix(str_extract_all(b, "\\w+")[[1]], ncol=2)
do.call(sprintf, c(do.call(paste, c(as.data.frame(matrix(paste(m1[1,][col(m1[-1,])],
m1[-1,]), nrow=2, byrow=TRUE)), sep="|")), list(fmt = "(%s) (%s)")))
#[1] "(word apple|word grape|word tomato) (vocabulary rice|vocabulary mice|vocabulary lice)"

更新

根据 OP 帖子中显示的新模式,我们创建了一个更通用的方法

funPaste <- function(str1){
v1 <- strsplit(str1, "\\s+")[[1]]
i1 <- grep("\\(", v1)
v1[i1] <- mapply(function(x,y) paste0("(", paste(x, y, collapse="|"), ")"),
v1[i1-1], str_extract_all(v1[i1], "\\w+"))
paste(v1[-(i1-1)], collapse=" ")
}

funPaste(a)
#[1] "(blahblah Peter|blahblah Sally|blahblah Tom)"
funPaste(b)
#[1] "(word apple|word grape|word tomato) (vocabulary rice|vocabulary mice|vocabulary lice)"
funPaste(c)
#[1] "people (person you|person me|person us) do not know (how it|how them) works"

更新2

我们还可以利用gsubfn

library(gsubfn)
funPaste2 <- function(str1){
gsubfn("(\\w+)\\s+[(]([^)]+)[)]", function(x,y)
paste0("(", paste(x, unlist(strsplit(y, "[|]")), collapse="|"), ")"), str1)
}

funPaste2(c(a, b, c))
#[1] "(blahblah Peter|blahblah Sally|blahblah Tom)"
#[2] "(word apple|word grape|word tomato) (vocabulary rice|vocabulary mice|vocabulary lice)"
#[3] "people (person you|person me|person us) do not know (how it|how them) works"

关于在 R 中重新定位和复制字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42041291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com