Search code examples
rlistsubset

How to subset values from a list that are similar to values from a second list?


List A contains values

c("Data 2018.xlsx","Data 2014.xlsx","Regular","Data 2019.xlsx","Data 2020.xlsx")

List B contains values

c("2022","2021","2020","2019","2018")

Is there a way to pull c("Data 2018.xlsx","Data 2019.xlsx","Data 2020.xlsx") from List A since "2018","2019" and "2020" were in list B?


Solution

  • You may expand b by adding "Data" and ".xlsx" and find the common intersection with a.

    a <- c("Data 2018.xlsx","Data 2014.xlsx","Regular",
           "Data 2019.xlsx","Data 2020.xlsx")
    b <- c("2022","2021","2020","2019","2018")
    
    intersect(sprintf('Data %s.xlsx', b), a)
    #[1] "Data 2020.xlsx" "Data 2019.xlsx" "Data 2018.xlsx"
    

    Another option extracting the numeric value from a

    a[sub('Data (\\d+)\\.xlsx', '\\1', a) %in% b]
    

    Or the same using stringr::str_extract

    a[stringr::str_extract(a, "\\d+") %in% b]