Search code examples
rstringlistapplysapply

Iterating over words across vector of strings and applying change to single word


Given string:

words <- c("fauuucet water", "tap water")

I would like to apply toupper function to all words that contain u.

Desired results

res <- c("FAUUCET water", "tap water")

Function

change_u_case <- function(str) {
    sapply(
        X = str,
        FUN = function(search_term) {
            sapply(
                X = strsplit(search_term, split = "\\s", perl = TRUE),
                FUN = function(word) {
                    if (grepl(pattern = "u", x = word)) {
                        toupper(word)
                    }
                }
                ,
                USE.NAMES = FALSE
            )
        },
        USE.NAMES = FALSE
    )
}

Tests

change_u_case(words) -> tst_res
words
tst_res
unlist(tst_res)

Notes

  • In particular, I'm interested whether solution using single rapply call could be build
  • rlist::list.iter approach would be also interesting
  • The selection of words containing u character is an example, in practice I would be looking to apply various conditions reflecting length and so on

Solution

  • Here is a stringi-based solution:

    library(stringi);
    sapply(stri_extract_all_words(words),
        function(w) paste(ifelse(stri_detect(w, regex = "u"), toupper(w), w), collapse = " "))
    #[1] "FAUUUCET water" "tap water"