I have an expression as a string, like the following:
orig <- "mean(Sepal.Length, na.rm = TRUE)"
orig
#> [1] "mean(Sepal.Length, na.rm = TRUE)"
I would like to rearrange this string so that I get the following output:
"Sepal.Length$mean(na.rm = TRUE)"
#> [1] "Sepal.Length$mean(na.rm = TRUE)"
I know that I can use capture groups like this:
gsub("(Sepal.Length)", "\\1\\$", orig)
#> [1] "mean(Sepal.Length$, na.rm = TRUE)"
but this doesn’t work to move text in the string:
gsub("(Sepal.Length)(.*)", "\\1\\$\\2", orig)
#> [1] "mean(Sepal.Length$, na.rm = TRUE)"
This question was helpful but the solution there is hardcoded, whereas here I don’t know the expression I will have at all, just that it will contain Sepal.Length. The expression above could be "sum(Sepal.Length)" for example.
I’m looking for a solution in base R only.
>Solution :
You can use the following pattern:
gsub("(.+)\\(Sepal\\.Length, *(.+)\\)", "Sepal.Length$\\1(\\2)", orig)
(.+)matches anything before the first parenthesis\\(;- Then, we always have "Sepal.Length". Note that
.is a special character, so to use a literal dot you need\\.; - We then have a
,, maybe a space also*(*means "0 or more times"); - After that we have the other arguments
(.+), followed by the closing parenthesis\\).