# adds 1 to each number in third arg
gsubfn("[[:digit:]]+", function(x) as.numeric(x)+1, "(10 20)(100 30)")
# same but using formula notation for function
gsubfn("[[:digit:]]+", ~ as.numeric(x)+1, "(10 20)(100 30)")
# replaces pairs m:n with their sum
s <- "abc 10:20 def 30:40 50"
gsubfn("([0-9]+):([0-9]+)", z + x + y ~ as.numeric(x) + as.numeric(y), s)
# same - can reduce args in function to two using backref = -2
gsubfn("([0-9]+):([0-9]+)", ~ as.numeric(x) + as.numeric(y), s, backref = -2)
# default pattern for gsubfn does quasi-perl-style string interpolation
gsubfn( , , "pi = \$pi, 2pi = `2*pi`")
# Extracts numbers from string and places them into numeric vector v.
# Normally this would be done in strapply instead.
v <- c(); f <- function(x) v <<- append(v,as.numeric(x))
junk <- gsubfn("[0-9]+", f, "12;34:56,89,,12")
v
# same
strapply("12;34:56,89,,12", "[0-9]+", simplify = c)
# makes all letters except first in word lower case
gsubfn("\\B.", tolower, "I LIKE A BANANA SPLIT", perl = TRUE)
# replaces numbers with that many Xs
gsubfn("[[:digit:]]+", ~ paste(rep("X", n), collapse = ""), "5.2")
# place <...> around first two occurrences
p <- proto(fun = function(this, x) if (count <= 2) paste0("<", x, ">") else x)
gsubfn("\\w+", p, "the cat in the hat is back")
# replace each number by cumulative sum to that point
p2 <- proto(pre = function(this) this$value <- 0,
fun = function(this, x) this$value <- value + as.numeric(x))
gsubfn("[0-9]+", p2, "12 3 11, 25 9")
Run the code above in your browser using DataLab