map {dat} | R Documentation |
An implementation of map
Description
An implementation of map and flatmap. They support the use of formulas as syntactic sugar for anonymous functions.
Usage
map(x, f, ...)
## S4 method for signature 'ANY,formula'
map(x, f, ...)
## S4 method for signature 'atomic,'function''
map(x, f, ...)
## S4 method for signature 'list,'function''
map(x, f, p = function(x) TRUE, ...)
## S4 method for signature 'list,numericORcharacteORlogical'
map(x, f, ...)
## S4 method for signature 'MList,'function''
map(x, f, ..., simplify = FALSE)
## S4 method for signature 'formula,'function''
map(x, f, ...)
flatmap(x, f, ..., flatten = unlist)
## S4 method for signature 'ANY,formula'
flatmap(x, f, ..., flatten = unlist)
sac(x, f, by, ..., combine = bindRows)
## S4 method for signature 'data.frame,'function''
sac(x, f, by, ..., combine = bindRows)
## S4 method for signature 'ANY,formula'
sac(x, f, by, ..., combine = bindRows)
vmap(x, f, ..., .mc = min(length(x), detectCores()), .bar = "bar")
Arguments
x |
(vector | data.frame | formula) if x inherits from data.frame, a data.frame is returned. Use as.list if this is not what you want. When x is a formula it is interpreted to trigger a multivariate map. |
f |
(function | formula | character | logical | numeric) something which can be interpreted as a function. formula objects are coerced to a function. atomics are used for subsetting in each element of x. See the examples. |
... |
further arguments passed to the apply function. |
p |
(function | formula) a predicate function indicating which columns in a data.frame to use in map. This is a filter for the map operation, the full data.frame is returned. |
simplify |
see SIMPLIFY in mapply |
flatten |
(function | formula) a function used to flatten the results. |
by |
(e.g. character) argument is passed to extract to select columns. |
combine |
(function | formula) a function which knows how to combine the list of results. bindRows is the default. |
.mc |
(integer) the number of cores. Passed down to mclapply or mcmapply. |
.bar |
(character) see verboseApply. |
Details
map
will dispatch to lapply. When x
is a
formula this is interpreted as a multivariate map; this is implemented
using mapply
. When x
is a data.frame map
will iterate
over columns, however the return value is a data.frame
. p
can
be used to map over a subset of x
.
flatmap
will dispatch to map
. The result is then wrapped by
flatten
which is unlist by default.
sac
is a naive implementation of split-apply-combine and implemented
using flatmap
.
vmap
is a 'verbose' version of map
and provides a progress bar
and a link to parallel map (mclapply).
map
, flatmap
, and sac
can be extended; they are S4
generic functions. You don't and should not implement a new method for
formulas. This method will coerce a formula into a function and pass it down
to your map(newtype, function) method.
Examples
# Sugar for anonymous functions
map(data.frame(y = 1:10, z = 2), x ~ x + 1)
map(data.frame(y = 1:10, z = 2), x ~ x + 1, is.numeric)
map(data.frame(y = 1:10, z = 2), x ~ x + 1, x ~ all(x == 2))
sac(data.frame(y = 1:10, z = 1:2), df ~ data.frame(my = mean(df$y)), "z")
# Trigger a multivariate map with a formula
map(1:2 ~ 3:4, f(x, y) ~ x + y)
map(1:2 ~ 3:4, f(x, y) ~ x + y, simplify = TRUE)
map(1:2 ~ 3:4, f(x, y, z) ~ x + y + z, z = 1)
# Extracting values from lists
map(list(1:2, 3:4), 2)
map(list(1:3, 2:5), 2:3)
map(list(1:3, 2:5), c(TRUE, FALSE, TRUE))
# Some type checking along the way
map(as.numeric(1:2), numeric : x ~ x)
map(1:2, integer(1) : x ~ x)
map(1:2, numeric(1) : x ~ x + 0.5)