4
votes

Having the following list:

dat <- list(words = c("foo", "bar", "howdy"), 
        pattern=c(foobar="foo|bar", cowboy="howdy"), 
        furterdat=1)

I would like to do the following in a pipe-style way

require(purrr)
require(stringr)
map(dat$pattern, ~str_detect(dat$words, .))

I tried thinks like

dat %>% map(.$pattern, ~str_detect, string=.$words)
dat %>% lmap(.$pattern, ~str_detect, string=.$words)

But couldn't get the result i want. Any ideas?

1
stringi (the underpinning of stringr) is vectorized over both str and pattern if that helps at al. - hrbrmstr
@hrbrmstr That is true, but i am not sure how this could/should help. The target is to apply each pattern to all words. str_detect(dat$words, dat$pattern) is not what i am looking for. - Rentrop

1 Answers

3
votes

The following is an option:

library(purrr)
library(stringr)

dat <- list(words = c("foo", "bar", "howdy"), 
        pattern=c(foobar="foo|bar", cowboy="howdy"), 
        furterdat=1)

dat$pattern %>% map(str_detect, dat$words)

#> $foobar
#> [1]  TRUE  TRUE FALSE
#> 
#> $cowboy
#> [1] FALSE FALSE  TRUE