获取R中两个数据帧的近似子串的位置
Getting positions of approximate substrings accross two data frames in R
我有两个数据框。第一个 (word.library
) 包括应该与第二个数据帧 (targetframe
) 中的字符串大致匹配的字符串。
word.library <- data.frame(mainword = c("important word",
"crazy sayings"))
tragetframe <- data.frame(words= c("Important Words",
"I would also Importante worde of thes substring",
"No mention of this crazy sayingsys"))
我只是找到了一个一个的解决方案(循环也是如此),但这不能满足我的需求:
positions <- aregexec(word.library[1,1], tragetframe$words, max.distance = 0.1)
positions <- aregexec(word.library[2,1], tragetframe$words, max.distance = 0.1)
最后:我正在寻找一种解决方案来同时对 word.library$mainword
列中的所有字符串执行此操作。有人有好主意吗?谢谢。
find <- function(library.vec, frame.vec) {
aregexec(library.vec, frame.vec, max.distance = 0.1)
}
如果函数是根据您尝试的表达式创建的,您将能够将其包含在应用族函数中以在词库中重复。
mapply(find, word.library[,1], list(tragetframe[,1]))
# [,1] [,2]
#[1,] 1 -1
#[2,] 14 -1
#[3,] -1 20
进程中删除了属性。输出按每个单词的列排列。如果你想保留属性尝试:
lapply(word.library[,1], find, tragetframe[,1])
我有两个数据框。第一个 (word.library
) 包括应该与第二个数据帧 (targetframe
) 中的字符串大致匹配的字符串。
word.library <- data.frame(mainword = c("important word",
"crazy sayings"))
tragetframe <- data.frame(words= c("Important Words",
"I would also Importante worde of thes substring",
"No mention of this crazy sayingsys"))
我只是找到了一个一个的解决方案(循环也是如此),但这不能满足我的需求:
positions <- aregexec(word.library[1,1], tragetframe$words, max.distance = 0.1)
positions <- aregexec(word.library[2,1], tragetframe$words, max.distance = 0.1)
最后:我正在寻找一种解决方案来同时对 word.library$mainword
列中的所有字符串执行此操作。有人有好主意吗?谢谢。
find <- function(library.vec, frame.vec) {
aregexec(library.vec, frame.vec, max.distance = 0.1)
}
如果函数是根据您尝试的表达式创建的,您将能够将其包含在应用族函数中以在词库中重复。
mapply(find, word.library[,1], list(tragetframe[,1]))
# [,1] [,2]
#[1,] 1 -1
#[2,] 14 -1
#[3,] -1 20
进程中删除了属性。输出按每个单词的列排列。如果你想保留属性尝试:
lapply(word.library[,1], find, tragetframe[,1])