简体   繁体   中英

Matching a vector with a vector of patterns in R

Given a vector of urls i want to filter these URLs and puling out only URLs matching a set of patterns.

/pages/
/econo/*
/categ/sub 

I am currently using the data.table package. for Matching a vector with a single pattern i use

urls[! urls %like% "/pages" ]

for multiple patterns this does not work

urls[! urls %like% c("/pages/","/categ/sub")  ]

Use | to separate the patterns:

urls[! urls %like% paste(c("/pages/","/categ/sub"),collapse="|")  ]

Example:

DT <- data.table(a = paste0(letters,rev(letters)))
DT[a %like% paste(c("a","b","c"),collapse="|")]

Edit: This also works without the data.table package:

   vector <- paste0(letters,rev(letters))
   patterns <- c("a","b","c")
   matches <- unique(grep(paste(patterns,collapse="|"), 
                        vector, value=TRUE))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM