ft_extract | R Documentation |
Extracts substrings from the levels of a factor vector based on a regular expression pattern and creates a new factor.
ft_extract(factor_vec, pattern, capture_group = 0)
factor_vec |
A factor vector from which substrings will be extracted. |
pattern |
A regular expression pattern to match. |
capture_group |
An integer specifying which capture group to extract if using capturing groups in the pattern. Default is |
A new factor vector containing the extracted substrings.
Kai Guo
# Example factor vector
factor_vec <- factor(c('item123', 'item456', 'item789'))
# Extract numeric part
ft_extract(factor_vec, pattern = '\\d+')
# Extract with capturing group
factor_vec <- factor(c('apple: red', 'banana: yellow', 'cherry: red'))
ft_extract(factor_vec, pattern = '^(\\w+):', capture_group = 1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.