how to create variables based on regex matches?

Consider this simple example

mytib <- tibble(mytext = c('hello world',
                           'nice computer'))

> mytib %>% mutate(myregex1 = str_detect(mytext, 'hello'),
+                  myregex2 = str_detect(mytext, 'nice'))
# A tibble: 2 x 3
  mytext        myregex1 myregex2
  <chr>         <lgl>    <lgl>   
1 hello world   TRUE     FALSE   
2 nice computer FALSE    TRUE 

Essentially, I would like to automate this variable creation and I am not sure what is the most efficient way to do so. The problem is that in my real dataset, I have many regexes I need to use that are stored in a list(myregex1 = 'hello', myregex2 = 'nice')
so creating a mutate for each of them is not good. Is purrr the solution?

Any ideas? Thanks!!

You can achieve that using tidyeval and !!!

library(tidyverse)

mytib <- tibble(mytext = c('hello world',
                           'nice computer'))

# your regex list
regex <- list(myregex1 = "hello",
              myregex2 = "nice")

# build the expressions for inside mutate
vars <- map(regex, ~ quo(str_detect(mytext, !!.x)))

# splice the vars list
mytib %>% 
  mutate(!!!vars)
#> # A tibble: 2 x 3
#>   mytext        myregex1 myregex2
#>   <chr>         <lgl>    <lgl>   
#> 1 hello world   TRUE     FALSE   
#> 2 nice computer FALSE    TRUE

Created on 2019-02-18 by the reprex package (v0.2.1)

6 Likes

thanks that is really neat. Could you please just explain what the !!! is doing here? I was aware of the quo() and !! but I dont quite understand why your code works here... Why do the variables get the right name? thanks!

!!! is the "unquote splice" operator

The !!! operator unquotes and splices its argument. The argument should represent a list or a vector. Each element will be embedded in the surrounding call, i.e. each element is inserted as an argument. If the vector is named, the names are used as argument names.

https://rlang.r-lib.org/reference/quasiquotation.html

3 Likes

thanks Mara! although I have to admit all these esoteric !! and !!! are sometimes a bit confusing to me. is there a good tutorial where I can get the big picture view of the tidyeval?

1 Like

Still a work in progress, but (it also has links to shorter resources in there):

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.