gsub_: Methods providing versions of sub and gsub for pipelines and...

View source: R/strings.R

gsub_R Documentation

Methods providing versions of sub and gsub for pipelines and factors

Description

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.

Usage

gsub_(x, pattern, replacement, ...)

## Default S3 method:
gsub_(x, pattern, replacement, ...)

## S3 method for class 'factor'
gsub_(x, pattern, replacement, ...)

Arguments

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 sub or gsub

Value

Value of sub(pattern, replacement, x, ...) or gsub(pattern, replacement, x, ...)

Methods (by class)

  • gsub_(default): default method

  • gsub_(factor): method for factors

See Also

sub gsub_

Examples

## 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(gsub_, '(.*)', 'Time_\\1')  %>% unlist

## End(Not run)

gmonette/spida2 documentation built on Aug. 20, 2023, 7:21 p.m.