View source: R/small_helpers.R
| replace_except | R Documentation |
Replaces a provided pattern with another, while protecting exceptions. Exceptions can contain the given pattern, but won't be changed during replacement.
replace_except(vector, pattern, replacement, exceptions = NULL)
vector |
A vector containing the texts, where a pattern should be replaced. |
pattern |
The pattern that should be replaced. |
replacement |
The new pattern, which replaces the old one. |
exceptions |
A character vector containing exceptions, which should not be altered. |
Returns a vector with replaced pattern.
# Vector, where underscores hsould be replaced
underscores <- c("my_variable", "var_with_underscores", "var_sum", "var_pct_total")
# Extensions, where underscores shouldn't be replaced
extensions <- c("_sum", "_pct_group", "_pct_total", "_pct_value", "_pct", "_freq_g0",
"_freq", "_mean", "_median", "_mode", "_min", "_max", "_first",
"_last", "_p1", "_p2", "_p3", "_p4", "_p5", "_p6", "_p7", "_p8", "_p9",
"sum_wgt", "_sd", "_variance", "_missing")
# Replace
new_vector <- underscores |> replace_except("_", ".", extensions)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.