Align the two vectors and replace them in the line

The next problem: I have two data frames where I want to map one vector from data data to data1 with a vector from data from data frame2.

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

If the character string in data2 matches, it should be replaced with the corresponding v2 value from data1. The result is as follows:

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

However, is there an idea to use a vectorized solution instead of a for loop to create better performance with huge data sets?

Thanks in advance!

- Updated:

What happens when I get that both data files do not have the same length?

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

When I use this solution:

data2$v1 <- mapply(sub, data1$v1, data1$v2, data2$v1)

Then I get the following warning message:

1: mapply (sub, data1 $v1, data1 $v2, data2 $v1): 2: mapply (sub, data1 $v1, data1 $v2, data2 $v1):

mgsub ! !

+4
2

data2. nrows data1 data2 . , v1 v2 data1.

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

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"  

mgsub . mgsub , . / .

+4

"stringi" , srti_replace_all, :

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

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
+5

All Articles