We can use the second dataset to extract all words into a list
and then unnest
the list
column to expand the rows
library(dplyr)
library(tidyr)
library(stringr)
person %>%
mutate(hobbies = str_extract_all(hobbies,
str_c(all_hobbies$all_hobbies, collapse="|"))) %>%
unnest(c(hobbies))
-output
# A tibble: 5 x 2
# name hobbies
# <chr> <chr>
#1 Jeff games
#2 Jeff basketball
#3 Linda hiking
#4 Linda tennis
#5 Linda reading
data
person <- data.frame(name = c("Jeff", "Linda"),
hobbies = c("gamesbasketball", "hikingtennisreading"))
all_hobbies <- data.frame(all_hobbies = c("games", "basketball",
"hiking", "tennis", "reading"))
CLICK HERE to find out more related problems solutions.