R - 如何从多个匹配中替换字符串(在数据框中)

时间:2017-03-24 11:42:38

标签: r string replace gsub

我需要用一些存储在数据帧中的匹配替换字符串的子集。

例如 -

input_string = "Whats your name and Where're you from"

我需要从数据框中替换此字符串的一部分。假设数据框是

matching <- data.frame(from_word=c("Whats your name", "name", "fro"),
            to_word=c("what is your name","names","froth"))

预期输出您的名字是什么?您来自哪里

注意 -

  1. 匹配最大字符串。在此示例中,名称名称不匹配,因为名称是较大匹配的一部分
  2. 它必须匹配整个字符串而不是部分字符串。来自“来自”不应该匹配为“泡沫”
  3. 我提到了下面的链接,但不知何故无法按照预期/如上所述进行此项工作

    Match and replace multiple strings in a vector of text without looping in R

    这是我在这里的第一篇文章。如果我没有提供足够的细节,请告诉我

3 个答案:

答案 0 :(得分:1)

toreplace =list("x1" = "y1","x2" = "y2", ..., "xn" = "yn")

函数有两个参数 xi yi

xi 是模式(找到什么), yi 是替换(替换为)。

input_string = "Whats your name and Where're you from"
toreplace<-list("Whats your name" = "what is your name", "names" = "name", "fro" = "froth")
gsubfn(paste(names(toreplace),collapse="|"),toreplace,input_string)

答案 1 :(得分:1)

修改

根据斯里兰卡评论的意见,我建议使用:

library(gsubfn)
# words to be replaced
a <-c("Whats your","Whats your name", "name", "fro")
# their replacements
b <- c("What is yours","what is your name","names","froth")
# named list as an input for gsubfn
replacements <- setNames(as.list(b), a)
# the test string
input_string = "fro Whats your name and Where're name you from to and fro I Whats your"
# match entire words
gsubfn(paste(paste0("\\w*", names(replacements), "\\w*"), collapse = "|"), replacements, input_string)

原始

我不会说这比简单的循环更容易阅读,但它可能需要更好地照顾重叠的替换:

# define the sample dataset
input_string = "Whats your name and Where're you from"
matching <- data.frame(from_word=c("Whats your name", "name", "fro", "Where're", "Whats"),
                       to_word=c("what is your name","names","froth", "where are", "Whatsup"))

# load used library
library(gsubfn)

# make sure data is of class character
matching$from_word <- as.character(matching$from_word)
matching$to_word <- as.character(matching$to_word)

# extract the words in the sentence
test <- unlist(str_split(input_string, " "))
# find where individual words from sentence match with the list of replaceble words
test2 <- sapply(paste0("\\b", test, "\\b"), grepl, matching$from_word)
# change rownames to see what is the format of output from the above sapply
rownames(test2) <- matching$from_word
# reorder the data so that largest replacement blocks are at the top
test3 <- test2[order(rowSums(test2), decreasing = TRUE),]
# where the word is already being replaced by larger chunk, do not replace again
test3[apply(test3, 2, cumsum) > 1] <- FALSE

# define the actual pairs of replacement
replacements <- setNames(as.list(as.character(matching[,2])[order(rowSums(test2), decreasing = TRUE)][rowSums(test3) >= 1]),
                         as.character(matching[,1])[order(rowSums(test2), decreasing = TRUE)][rowSums(test3) >= 1])

# perform the replacement
gsubfn(paste(as.character(matching[,1])[order(rowSums(test2), decreasing = TRUE)][rowSums(test3) >= 1], collapse = "|"),
       replacements,input_string)

答案 2 :(得分:0)

正在尝试不同的东西,以下代码似乎有效。

a <-c("Whats your name", "name", "fro")
b <- c("what is your name","names","froth")
c <- c("Whats your name and Where're you from")

for(i in seq_along(a)) c <- gsub(paste0('\\<',a[i],'\\>'), gsub(" ","_",b[i]), c)
c <- gsub("_"," ",c)
c

从以下链接Making gsub only replace entire words?

获得帮助

但是,如果可能的话,我想避免循环。有人可以在没有循环的情况下改进这个答案