I have a vector of strings, in the following format:
strings <- c("UUDBK", "KUVEB", "YVCYE")
I also have a data frame like this:
replacewith <- c(8, 4, 2)
searchhere <- c("UUDBK, YVCYE, KUYVE, IHVYV, IYVEK", "KUVEB, UGEVB", "KUEBN, IHBEJ, KHUDN")
dataframe <- data.frame(replacewith, searchhere)
I want the strings vector to be replaced with the value in its corresponding "replacewith" column in this data frame. Currently the way I am doing it is:
final <- sapply(as.character(strings), function(x)
as.numeric(dataframe[grep(x, dataframe$searchhere), 1]))
However, this is very computationally heavy to be doing this with a character vector with length 10^9.
What is a better way to do this?
Thanks!