sub_ | R Documentation |
The first argument of sub_ and gsub_ is the object to
be modified instead of the pattern to be matched. Thus sub_
and gsub_
can be used as generic functions that dispatch on
the class of the first argument. This allows them to recognize factors
and work with the levels attribute instead of coercing the
factor to a character vector. Two consequences are that a
factor, instead of a character vector, is returned, and that the
original order of the levels is preserved unless the number of
levels is reduced.
sub_(x, pattern, replacement, ...)
## Default S3 method:
sub_(x, pattern, replacement, ...)
## S3 method for class 'factor'
sub_(x, pattern, replacement, ...)
x |
object to change. If x is a factor, the substitution is performed on its levels attribute. |
pattern |
the 'from' regular expression |
replacement |
the 'to' regular expression |
... |
other arguments for |
Value of sub(pattern, replacement, x, ...)
or gsub(pattern, replacement, x, ...)
sub_(default)
: default method
sub_(factor)
: method for factor objects
sub
sub_
## Not run:
library(magrittr)
x <- as.list(1:4)
x %>% name(letters[1:4])
x %>% name(letters[1:4]) %>% name(toupper)
x %>% name(letters[1:4]) %>%
name(sub_, '(.*)', 'Time_\\1') %>% unlist
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.