Search code examples
rsapplygrepl

How to apply grepl to match values in multiple columns across a data frame?


I would like to check if a string in one column in my data frame is found within the string in another column. I am able to do this for individual rows correctly, but when I apply it across the whole data frame the results for some rows are not correct.

Example data df:

col1   col2
XYZ    XYZXYZ
ABC    BCABCA
XX     XYXY

I used the following code to pattern match for individual rows:

grepl(df[1,1], df[1,2], fixed = TRUE)

I use the following code to apply across the data frame:

df$col3 <- sapply(df$col1, grepl, df$col2)

Column 3 in this case should be: TRUE, TRUE, FALSE.


Solution

  • Use mapply, which applies grepl to the first elements of each col1, col2.

    df$col3 <- mapply(grepl, df$col1, df$col2)
    df
    #   col1   col2  col3
    # 1  XYZ XYZXYZ  TRUE
    # 2  ABC BCABCA  TRUE
    # 3   XX   XYXY FALSE