Floo0 Floo0 - 2 months ago 16
R Question

purrr - use full list-elements in 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 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?

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
Comments