#Dictionary data frame
dict <- data.frame( original = c("word", "Hello","Good","Good Night"),
replace = c("replacement", "Hi", "Best", "Sweet Morning"),
stringsAsFactors=FALSE)
dict
# original replace
# 1 word replacement
# 2 Hello Hi
# 3 Good Best
# 4 Good Night Sweet Morning
# Data frame where the words need to be replaced
df <- data.frame ( col1 = c( "Hello", "World", "Good","coffee"),
col2 = c("Good Night","To all my friends","I have no","word"),
stringsAsFactors=FALSE)
df
# col1 col2
#1 Hello Good Night
#2 World To all my friends
#3 Good I have no
#4 coffee word
apply(df,
MARGIN=c(1,2),
FUN=function(x){ pos=which(dict[,1] == x);
if(length(pos)>0) return(dict[pos[1],2]) else return(x)})
# col1 col2
#[1,] "Hi" "Sweet Morning"
#[2,] "World" "To all my friends"
#[3,] "Best" "I have no"
#[4,] "coffee" "replacement"