Search code examples
rstring-matchingpartial

Compare row values to column in dataframe for partial match using R


I have a dataframe

Name
1.Started
2.Home
3.Signout

I want to compare with another column in another dataframe

Df
1.help/services/Home 
2./msoffice/home 
3./windows.support

Expected output:

False
True
False

This shows Home from name exists in df. It's matching partially with another entire column.


Solution

  • We can compare Name from df1 and use basename to get last part of the string.

    df1$Name %in% basename(df2$col)
    #[1] FALSE  TRUE FALSE
    

    data

    Assuming the dataframes are df1 and df2 as below.

    df1 <- structure(list(Name = c("Started", "Home", "Signout")), row.names = c(NA, 
          -3L), class = "data.frame")
    
    df2 <- structure(list(col = c("help/services/Home", "/msoffice/home", 
    "/windows.support")), class = "data.frame", row.names = c(NA, -3L))