Update Character Variables based on String Pattern in R

Viewed 67

I currently have the below vector and am trying to use stringr to find a pattern and update.

string_vector <- c("1_lasso", "_lasso", "1_lasso_olsps", "_lasso_olsps")

string_vector_new <- string_vector %>%
str_replace("^[1_]_lasso", "Lasso")

[1] "Lasso"                 "_lasso"                "Lasso_olsps"          
 [4] "_lasso_olsps"

I'm not sure how, but I am hoping to update my code so that I can detect a pattern like 1_lasso and _lasso, and change them both to Lasso simultaneously. Is this possible using stringr? I'm not sure what regular expressions I would need to make that update and have many more variables like these ones.

Thanks in advance.

4 Answers

simply use the | character to set an "or" in the pattern.

str_replace_all(string_vector, "1_lasso|_lasso", "Lasso")

I would use sub here with the regex pattern [^_]*_lasso:

string_vector <- c("1_lasso", "_lasso", "1_lasso_olsps", "_lasso_olsps")
output <- sub("[^_]*_lasso", "Lasso", string_vector)
output

[1] "Lasso"       "Lasso"       "Lasso_olsps" "Lasso_olsps"

The pattern used here matches _lasso which may or may not be preceded by some number of non underscore characters.

Use

str_replace_all(string_vector, "\\b1?_lasso(?![^\\W_])", "Lasso")

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  1?                       '1' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  _lasso                   '_lasso'
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    [^\W_]                   any character except: non-word
                             characters (all but a-z, A-Z, 0-9, _),
                             '_'
--------------------------------------------------------------------------------
  )                        end of look-ahead

R code snippet:

library(stringr)
string_vector <- c("1_lasso", "_lasso", "1_lasso_olsps", "_lasso_olsps")
str_replace_all(string_vector, "\\b1?_lasso(?![^\\W_])", "Lasso")

Results: [1] "Lasso" "Lasso" "Lasso_olsps" "Lasso_olsps"

If you like to use regular expression then this my solution.

You can directly tackle it this way.

    string_vector <- c("1_lasso", "_lasso", "1_lasso_olsps", "_lasso_olsps")
    gsub("1_lasso|_lasso","Lasso",string_vector)

[1] "Lasso"       "Lasso"       "Lasso_olsps" "Lasso_olsps"

The code searches for the two patterns and replaces it with "Lasso"

To make it more generalized we can use the below code which looks for any pattern with "something_lasso"

gsub("\\S*_lasso","Lasso",string_vector)
[1] "Lasso"       "Lasso"       "Lasso_olsps" "Lasso_olsps"

The code looks for \S*_lasso where \S* is any non space item, 0 or infinite number of times.

I hope it helps.

Related