I want to extract specific string of favor
column in target
data which is matched by a dictionary
. Here is my data:
dictionary <- c("apple", "banana", "orange", "grape")
target <- data.frame("user" = c("A", "B", "C"),
"favor" = c("I like apple and banana", "grape and kiwi", "orange, banana and grape are the best"))
target
user favor
1 A I like apple and banana
2 B grape and kiwi
3 C orange, banana and grape are the best
And below is my expected outcome result
. I want to automatically create the column based on the most favors I matched in dictionary(in my case, 3), and extract the string I match in dictionary.
result <- data.frame("user" = c("A", "B", "C"),
"favor_1" = c("apple", "grape", "orange"),
"favor_2" = c("banana", "", "banana"),
"favor_3" = c("", "", "grape"))
result
user favor_1 favor_2 favor_3
1 A apple banana
2 B grape
3 C orange banana grape
Any help will be thankful.