View source: R/single_replace.R
ft_replace | R Documentation |
Replaces a specified level in a factor vector with a new level. If a position is provided, the new level is inserted at the specified position among the levels; otherwise, the original level order is preserved.
ft_replace(factor_vec, old_level, new_level, position = NULL)
factor_vec |
A factor vector in which a level will be replaced. |
old_level |
A character string specifying the level to be replaced. |
new_level |
A character string specifying the new level to replace the old level. |
position |
Optional. A positive integer specifying the position to insert the new level in the levels vector. If |
A factor vector with the level replaced and the new level optionally inserted at the specified position.
Kai Guo
#
factor_vec <- factor(c('apple', 'banana', 'cherry', 'date', 'fig', 'grape'))
# replace 'banana' as 'blueberry', and keep original order
ft_replace(factor_vec, old_level = 'banana', new_level = 'blueberry')
# replace 'banana' as 'blueberry'
ft_replace(factor_vec, old_level = 'banana', new_level = 'blueberry', position = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.