简体   繁体   中英

Selection of character vector elements containing regex from using purrr and stringr

I have a list of character vectors and I want to keep only those elements of the vectors that contains specific regex. Unfortunately I was not able to convince purrr to feed the vectors to stringr. General aim of my effort is extract sentences with regex from text in one column of a tibble and concatenate them to another column. Do somebody has a purrre tidyverse solution? Thanks for any help:)

This prototype solution works:) Not sure why it did not work the first time though;) Still will be glad for your comments.

# Prototype solution ----
library(purrr)
library(stringr)
pr_pattern <- "rr" 
pr_db <- list(
  A = c("Apples and oranges.", "Oranges and cherries.", "Nuts and grapes."), 
  B = c("Oranges and apples.", "Cherries and oranges.", "Grapes and nuts.")
  )
pr_res <- map(pr_db, ~ str_subset(.x, pr_pattern))

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