Search code examples
rstringreplacematchqdap

R: match and replace string. mgsub does not work


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?


Solution

  • 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