Purrr - use a complete list of items in a map call

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 pipe style

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

I tried to think how

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

But I could not get the result that I want. Any ideas?

+4
source share
1 answer

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
+3
source

Source: https://habr.com/ru/post/1653456/


All Articles