50% off | Unlimited Data & AI Learning

Last chance! 50% off unlimited learning

Sale ends in


gsubfn (version 0.3-1)

gsubfn-package: gsubfn

Description

Generalized "'gsub'" and associated function.

Arguments

Details

gsubfn is an R package that has two purposes: (1) it provides a generalization of gsub called gsubfn which uses a replacement function (rather than a replacement string) together with a variety of associated functions, such as strapply and cati, which build upon it. Applications include string transforms such as parsing strings based on content rather than delimiters and perl-style string interpolation. (2) it provides a compact method of passing anonymous functions by using formula, rather than function, notation. This facilitates writing streamlined expressions to call functions whose arguments pass functions. This has application to the use of apply, lapply, mapply, sapply, tapply, by, integrate, optim, outer and other functions in the core of R and in addon packages. The following are sources of information on "gsubfn": ll{ News file.show(system.file("NEWS", package = "gsubfn")) Wish List file.show(system.file("WISHLIST", package = "gsubfn")) Thanks file file.show(system.file("THANKS", package = "gsubfn")) License file.show(system.file("COPYING", package = "gsubfn")) Demo demo("gsubfn-chron") Demo demo("gsubfn-cut") Demo demo("gsubfn-gries") Demo demo("gsubfn-si") This File package?gsubfn Help files ?gsubfn, ?strapply, ?cati, ?cati0, ?cat0, ?paste0 More Help files ?as.function.formula, ?match.funfn, ?fn Home page http://code.google.com/p/gsubfn/ }

Examples

Run this code
# replace each number with that number plus 1
gsubfn("[[:digit:]]+", function(x) as.numeric(x)+1, "(10 20)(100 30)") 

# same
gsubfn("[[:digit:]]+", ~ as.numeric(x)+1, "(10 20)(100 30)") 

# perl-style string interpolation
cati("pi = $pi, e = `exp(1)`\n")

# split out numbers
strapply("12abc34 55", "[0-9]+")

fn$optim(1, ~ x^2, method = "CG")

fn$integrate(~ sin(x) + cos(x), 0, pi/2)

fn$lapply(list(1:4, 1:5), ~ LETTERS[x]) # list(LETTERS[1:4], LETTERS[1:5])

fn$mapply(~ seq_len(x) + y * z, 1:3, 4:6, 2) # list(9, 11:12, 13:15)

# must specify x since . is a free variable
fn$by(CO2[4:5], CO2[1], x ~ coef(lm(uptake ~ ., x)), simplify = rbind)

# evaluate f at x^2 where f may be function or formula
square <- function(f, x, ...) { f <- match.funfn(f); f(x^2, ...) }
square(~ exp(x)/x, pi)
square(function(x) exp(x)/x, pi) # same

Run the code above in your browser using DataLab