View source: R/single_replace.R
ft_replace_pattern | R Documentation |
Replaces parts of the factor levels that match a specified pattern with a new string.
ft_replace_pattern(factor_vec, pattern, replacement, replace_all = TRUE)
factor_vec |
A factor vector to be modified. |
pattern |
A regular expression pattern to match. |
replacement |
A string to replace the matched parts. |
replace_all |
Logical. If |
A factor vector with levels modified.
Kai Guo
# Example factor vector
factor_vec <- factor(c('apple_pie', 'banana_bread', 'cherry_cake'))
# Replace '_pie', '_bread', '_cake' with '_dessert' (all occurrences)
ft_replace_pattern(factor_vec, pattern = '_.*', replacement = '_dessert')
# Replace only the first occurrence of '_' with '-'
ft_replace_pattern(factor_vec, pattern = '_', replacement = '-', replace_all = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.