View source: R/Spectra-functions.R
chunkapply | R Documentation |
chunkapply()
splits x
into chunks and applies the function FUN
stepwise
to each of these chunks. Depending on the object it is called, this
function might reduce memory demand considerably, if for example only the
full data for a single chunk needs to be loaded into memory at a time (e.g.,
for Spectra
objects with on-disk or similar backends).
chunkapply(x, FUN, ..., chunkSize = 1000L, chunks = factor())
x |
object to which |
FUN |
the function to apply to |
... |
additional parameters to |
chunkSize |
|
chunks |
optional |
Depending on FUN
, but in most cases a vector/result object of
length equal to length(x)
.
Johannes Rainer
## Apply a function (`sqrt`) to each element in `x`, processed in chunks of
## size 200.
x <- rnorm(n = 1000, mean = 500)
res <- chunkapply(x, sqrt, chunkSize = 200)
length(res)
head(res)
## For such a calculation the vectorized `sqrt` would however be recommended
system.time(sqrt(x))
system.time(chunkapply(x, sqrt, chunkSize = 200))
## Simple example splitting a numeric vector into chunks of 200 and
## aggregating the values within the chunk using the `mean`. Due to the
## `unsplit` the result has the same length than the input with the mean
## value repeated.
x <- 1:1000
res <- chunkapply(x, mean, chunkSize = 200)
length(res)
head(res)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.