Search code examples
rfunctional-programmingapplypurrr

How can I apply list of functions consecutively to a variable?


I have a list of functions I want to apply to a string consecutively, changing the string. For example, a list of regular expressions I want to remove from a string, e.g.

to_remove = c('a','b')
original = 'abcabd'

In this case, I could use a simple regular expression, e.g. 'a|b'.

library(stringr)
str_remove( original, paste0( to_remove, collapse='|'))

The actual situation is more complex than this, and the regular expression gets a little hairy. Also, I am curious how to do this in a proper R way.

Another way to phrase the question is, "How can I implement the following for loop using a vector approach?"


for( rem in to_remove) {
      original = str_remove( original, rem )
}


'


Solution

  • stringi offers vectorized string replacement. The replacement argument can take on a vector of length 1, "", or a vector of the same length.

    to_remove = c('a','b')
    original = 'abcabd'
    
    stringi::stri_replace_all_fixed(
        str = original,
        pattern = to_remove,
        replacement = "",
        vectorize_all = F
    )
    #> [1] "cd"
    

    For repeated application of patterns, perhaps use an alternative pattern, per Stéphane's comment, replace_all_* with vectorize = FALSE is recursive, see for example the output of

    stri_replace_all_regex(
        "abc", 
        pattern = c("^a", "^b"),
        replacement = "", 
        vectorise_all = FALSE
    )
    #> [1] "c"