mpm_elementwise_apply {Rcompadre} | R Documentation |
Apply a function element-wise to a list of matrices
Description
This function applies a specified function element-wise to the corresponding elements across a list of matrices.
Usage
mat_elementwise_apply(x, fun, na_handling = "stop", ...)
mpm_elementwise_apply(x, fun, na_handling = "stop", ...)
Arguments
x |
A list of matrices. |
fun |
The function to apply to the elements. |
na_handling |
A character string specifying how to handle NA values. Possible values are "stop" (throw an error when NA values are encountered), "zero" (convert NA values to 0), and "ignore" (NA values are ignored and passed to 'fun'). Handling can then be processed appropriately by that function (e.g., with 'na.rm'). |
... |
Additional arguments passed to 'fun'. |
Value
A matrix containing the result of applying the function element-wise to the corresponding elements across the matrices.
See Also
Other data management:
cdb_flatten()
,
cdb_id_stages()
,
cdb_id_studies()
,
cdb_id()
,
cdb_mean_matF()
,
cdb_rbind()
,
cdb_unflatten()
,
cdb_unnest()
,
mpm_mean()
,
mpm_median()
,
mpm_sd()
,
string_representation
Examples
mpms <- Compadre$mat[Compadre$SpeciesAuthor == "Haplopappus_radiatus"]
#The object mpms is a list, containing compadre objects
class(mpms)
class(mpms[[1]])
# Get the mean, max and min for the matrices
mpm_elementwise_apply(mpms, mean)
mpm_elementwise_apply(mpms, max)
mpm_elementwise_apply(mpms, min)
# extract list of matA and take mean
mats <- matA(mpms)
mat_elementwise_apply(mats, mean)
# This should be the same as mat_mean()
mat_mean(mats)
# Mean values, with 25% trimmed from each end
mat_elementwise_apply(mats, mean, trim = 0.25)
# weighted mean, where the second matrix is weighted to 100% and the others to 0%
# do demonstrate usage. The result should be the same as mats[[2]]
mat_elementwise_apply(mats, weighted.mean, w = c(0,1,0,0))
mats[[2]]
#min and max values
mat_elementwise_apply(mats, min)
mat_elementwise_apply(mats, max)
#Demonstrating NA handling
#First adding some NA values to the matrices
mats[[2]][3,2] <- NA
#replace the NA with a 0
mat_elementwise_apply(mats, min, na_handling = "zero")
#ignore the NA
mat_elementwise_apply(mats, min, na_handling = "ignore")
#ignore the NA, but pass na.rm = TRUE to the function (min)
mat_elementwise_apply(mats, min, na_handling = "ignore", na.rm = TRUE)