Learn R Programming

gsubfn (version 0.1-3)

gsubfn: Pattern Matching and Replacement

Description

Like gsub except instead of a replacement string one uses a function which accepts the matched text as input and emits replacement text for it.

Usage

gsubfn(pattern, replacement, x, backref, env = parent.frame(), ...)

Arguments

pattern
Same as pattern in gsub
replacement
A function. See Details.
x
Same as x in gsub
backref
Number of backreferences to be passed to function. If omitted it will be determined automatically in which case the function must be defined to accept them all. If a negative number is used then its absolute value will be the number of backreferences
env
Environment in which to evaluation the replacement function. Normally this is left at its default value.
...
Other gsub arguments.

Value

Details

Acts the same as gsub except each matched string is passed to the replacement function and the output of that function replaces the matched string in the result. The first argument to the replacement function is the matched string and subsequent arguments are the backreferences, if any.

See Also

gsub, strapply

Examples

Run this code
# adds 1 to each number in third arg
gsubfn("[[:digit:]]+", function(x) as.numeric(x)+1, "(10 20)(100 30)") 

# replaces pairs m:n with their sum
f <- function(x,y) as.numeric(x)+as.numeric(y)
gsubfn("([0-9]+):([0-9]+)", f, "abc 10:20 def 30:40 50", backref = -2)

gsubfn( , , "pi = $pi, 2pi = `2*pi`") 

# Extracts numbers from string and places them into numeric vector v.
# Also see ?strapply
v <- c(); f <- function(x) v <<- append(v,as.numeric(x))
junk <- gsubfn("[0-9]+", f, "12;34:56,89,,12")
v

# makes all letters except first in word lower case
gsubfn("\\B.", tolower, "I LIKE A BANANA SPLIT")

Run the code above in your browser using DataLab