I hope this does not get marked as duplicated. I have seen similar stackoverflow posts but I cannot make it work for me.

My goal:
1st: I want to detect in main\_df , if the variable 'Code' in auxiliary\_df is there.
2nd: After having detected I want to create a column, with the identified codes. For example, for the text 'School Performance, I want to have a line like 'A1, A6, A7'.

```
main_df <- read.table(header = TRUE, 
                   stringsAsFactors = FALSE, 
                   text="Title Text
'School Performance' 'Students A1, A6 and A7 are great'
'Groceries Performance' 'Students A9, A3 are ok'
'Fruit Performance' 'A5 and A7 will be great fruit pickers'
'Jedi Performance' 'A3, A6, A5 will be great Jedis'
'Sith Performance' 'No one is very good. We should be happy.'")

auxiliary_df <- read.table(header = TRUE, 
                   stringsAsFactors = FALSE, 
                   text="FirstName Code
'Alex' 'A1'
'Figo' 'A6'
'Rui' 'A7'
'Deco' 'A5'
'Cristiano' 'A9'
'Ronaldo' 'A3'")

```

What I tried:

```
toMatch <- auxiliary_df$Code

matches <- grep(paste(toMatch, collapse = "|"), 
                main_df$Title, value=TRUE)

matches #returns character(0)

```

I did not manage to identify any of the codes and move them to the new variable.

The desired output would look like this:

'School Performance' 'Students A1, A6 and A7 are great' 'A1, A6, A7'

Any help is welcome!