partition_map | R Documentation |
Apply function along the last dimension of an array and aggregate the results
partition_map(x, map_fun, reduce, partitions, ...)
x |
R array or |
map_fun |
function that takes in a slice of array and an optional argument indicating current partition number |
reduce |
function that accept a list of results returned by
|
partitions |
integers of partitions, i.e. the slices of array to be applied to, can be missing. If missing, then applies to all partitions |
... |
internally used |
If reduce
is missing, returns a list of results. Each result
is returned by map_fun
, and the total length equals to number of
partitions mapped. If reduce
is a function, that list of results will
be passed to reduce
and partition_map
returns the results
generated from reduce
.
# -------------------------- Ordinary R array ---------------------------
x <- array(1:24, c(2,3,4))
partition_map(x, function(slice, part){
sum(slice)
})
# When reduce and partitions are missing, the following code is equivalent
as.list(apply(x, 3, sum))
# When reduce is present
partition_map(x, function(slice, part){
sum(slice)
}, function(slice_sum){
max(unlist(slice_sum))
})
# equivalently, we could call
slice_sum <- partition_map(x, function(slice, part){
sum(slice)
})
max(unlist(slice_sum))
# When partition is specified
# Partition 1, 2, and 4 exist but 5 is missing
# when a partition is missing, the missing slice will be NA
partition_map(x, function(slice, part){
sum(slice)
}, partitions = c(1,2,4,5))
# -------------------------- LazyArray ---------------------------
x <- lazyarray(tempfile(), storage_format = 'complex', dim = c(2,3,4))
x[] <- 1:24 + (24:1) * 1i
partition_map(x, function(slice, part){
slice[1, ,] * slice[2, ,]
}, reduce = function(mapped_prod){
mean(unlist(mapped_prod))
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.