匹配两个向量并替换为字符串

OAM*_*OAM 4 replace r match

存在以下问题:我有两个数据帧,我希望将数据帧data1中的一个向量与来自数据帧data2的向量进行匹配.

data1 <- data.frame(v1 = c("horse", "duck", "bird"), v2 = c(1,2,3))
data2 <- data.frame(v1 = c("car, horse, mouse", "duck, bird", "bird"))
Run Code Online (Sandbox Code Playgroud)

如果data2中的字符串匹配,则应将其替换为data1中对应的值v2.结果如下:

for(i in 1:nrow(data1)) data2[,1] <- gsub(data1[i,1], data1[i,2], data2[,1], fixed=T)
data2
Run Code Online (Sandbox Code Playgroud)

但是,有没有想法使用矢量化解决方案而不是for循环来创建具有大型数据集的更好性能?

提前致谢!

- 更新:

当我遇到这种情况时会发生什么,两个数据帧的长度不一样?

data2 <- data.frame(v1 = c("car, horse, mouse", "duck, bird","bird", "bird"))

当我使用此解决方案时:

data2$v1 <- mapply(sub, data1$v1, data1$v2, data2$v1)
Run Code Online (Sandbox Code Playgroud)

然后我收到以下警告消息:

1:在mapply中(sub,data1 $ v1,data1 $ v2,data2 $ v1):较长的参数不是长度较短的倍数2:在mapply中(sub,data1 $ v1,data1 $ v2,data2 $ v1):更长参数不是短的长度的倍数

但是,mgsub解决方案非常完美!谢谢!

A5C*_*2T1 5

"stringi"包中的大多数参数都接受矢量化输入,因此您应该能够使用srti_replace_all,如下所示:

library(stringi)
stri_replace_all_fixed(data2$v1, data1$v1, data1$v2)
# [1] "car, 1, mouse" "2, bird"       "3"         
Run Code Online (Sandbox Code Playgroud)

得到你的data.frame:

data.frame(v1 = stri_replace_all_fixed(data2$v1, data1$v1, data1$v2))
#              v1
# 1 car, 1, mouse
# 2       2, bird
# 3             3
Run Code Online (Sandbox Code Playgroud)


akr*_*run 5

使用更新data2.的nrows之间data1data2是不同的,在此,我们假设之间的任何匹配v1两个数据集的列应的相应值来代替v2data1.

library(qdap)
mgsub(as.character(data1$v1), data1$v2, data2$v1)
#[1] "car, 1, mouse" "2, 3"          "3"             "3"    
Run Code Online (Sandbox Code Playgroud)

注意 mgsub有一些错误处理,处理在较大字符串中找到子字符串并且两者都在" 待替换 "列表中的情况.这是一个带有horse和的例子horses:

data1 <- data.frame(v1 = c("horse", "duck", "bird", "horse", "horses"), v2 = 1:5)
data2 <- data.frame(v1 = c("car, horses, mouse", "duck, bird, horse", "bird"))

library(stringi)
stri_replace_all_fixed(data2$v1, data1$v1, data1$v2)

## [1] "car, 1s, mouse"    "2, bird, horse"    "3"                 "car, 4s, mouse"    "duck, bird, horse"
## Warning message:
## In stri_replace_all_fixed(data2$v1, data1$v1, data1$v2) :
##   longer object length is not a multiple of shorter object length

library(qdap)
mgsub(as.character(data1$v1), data1$v2, data2$v1)

## [1] "car, 5, mouse" "2, 3, 4"       "3"  
Run Code Online (Sandbox Code Playgroud)

mgsub确保首先替换较长的单词.这使得mgsub速度更慢但更安全.根据您的数据类型/需求,这里的解决方案可能有用.