Create list from column and filter from the resulting list

Viewed 20

The below code allows simple filtering of a list:

#Filter to applicable codes only

ICS_List <- "QMM|QJG|QH8|QM7|QUE|QHG"

EofEMSOAs <- EofEMSOAs[grep(ICS_List,  EofEMSOAs$Code),]

What I am looking to do instead is take all data from the column from another dataframe within a project and use the grep function to filter for values contained within that column - there could be hundreds so typing a manual list is not practical.

I have tried the below but it results in error 'argument 'pattern' has length > 1 and only the first element will be used. Seems using dplyr in this way does not create the same output as manually typing in a list which is throwing the error so I only get one result.

    #To filter from required dataframe 'EofEMSOAsIMD' 
    EofEMSOAsCodeListOnly <- dplyr::pull(EofEMSOAsIMD, "Area Code")
    
    EofEMSOAsFinalList <- EofEMSOAs[grep(EofEMSOAsCodeListOnly,  EofEMSOAs$msoa11cd),]

Could anyone please amend the above so it does work using similar logic to the code at top of this question, namely 1. List created from column 2. Dataframe filtered for matches to that list? Thank you.

0 Answers
Related