Last chance! 50% off unlimited learning
Sale ends in
"'gsub'"
and associated function.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"
:
# 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