chunk_map | R Documentation |
Apply functions to all partitions, but small chunks each time
chunk_map(x, map_fun, reduce, max_nchunks, chunk_size, ...)
x |
a |
map_fun |
function to apply to each chunk |
reduce |
similar to |
max_nchunks |
maximum number of chunks. If number of chunks is too
large, then |
chunk_size |
integer chunk size. If |
... |
ignored or passed to other methods |
The difference between chunk_map
and
partition_map
is the margin or direction to apply mapping
functions. In partition_map
, mapping function is applied to
each partition. If x
is a matrix, this means applying to each column.
chunk_map
generate small chunks along all dimensions except the last,
and apply mapping functions to each chunks. If x
is a matrix, it
make chunks along rows and apply mapping functions along rows.
If reduce
is missing, returns a list of results. Each result
is returned by map_fun
, and the total length equals to number of
chunks mapped. If reduce
is a function, that list of results will
be passed to reduce
and chunk_map
returns the results
generated from reduce
.
partition_map
x <- as.lazymatrix(matrix(1:100, ncol = 2))
x
# Set max_nchunks=Inf and chunk_size=10 to force total number of chunks
# is around nrow(x)/10 and each chunk contains at most 10 rows
chunk_map(x, function(chunk){chunk[1:2,]}, chunk_size = 10, max_nchunks = Inf)
# For each chunks, calculate mean, then calculate the mean of chunk mean
chunk_map(x, function(chunk) {
colMeans(chunk)
}, function(chunk_means) {
Reduce('+', chunk_means) / length(chunk_means)
})
colMeans(x[])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.