R: match and replace string. mgsub does not work

919 views Asked by At

I have two data frames. The first data frame lib is a library with words in two columns. The second data frame data1 should be transformed as follows: The strings matched in column data1$V1 against lib$V2 should be replaced with the string in the corresponding row in column lib$V1.

lib <- data.frame(
  v1 = c("car", "great", "huge", "car", "great", "huge"),
  v2 = c("cars", "awesome", "tall", "truck", "super", "very huge")
  )



data1 <- data.frame(
  values = c("cars", "awesome", "tall", "truck", "super", "very huge")
)

The final data frame data1.final should look like as follows:

data1.final <- data.frame(
  values = c("car", "great", "huge", "car", "great", "huge")
)

I tried this with the mgsub function from the qdap package:

data1$values <- mgsub(as.character(lib$V2), lib$V1, data1$values, fixed=T)

But nothing has changed. Any ideas?

2

There are 2 answers

0
Cath On BEST ANSWER
data1.final<- data.frame(values=lib$v1[match(data1$values,lib$v2)])

> data1.final
  values
1    car
2  great
3   huge
4    car
5  great
6   huge
0
Tyler Rinker On

mgsub is for text substitutions within strings. What you're after is a dictionary lookup. The lookup (%l%) from the qdapTools package is one of many ways to tackle this:

library(qdapTools)
data1$values <- data1$values %lc% lib[, 2:1]

##   values
## 1    car
## 2  great
## 3   huge
## 4    car
## 5  great

Note that %lc% (stands for lookup character) forces a character output.

To accomplish this with mgsub you'd have used:

mgsub(as.character(lib$v2), as.character(lib$v1), data1$values)

But this is not the best approach for this case.