This function applies a specified function element-wise to the corresponding elements across a list of matrices.

mat_elementwise_apply(matrix_list, fun, na_handling = "stop", ...)

Arguments

matrix_list

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`).

Value

A matrix containing the result of applying the function element-wise to the corresponding elements across the matrices.

Examples

mpms <- Compadre$mat[Compadre$SpeciesAuthor == "Haplopappus_radiatus"]

#The object mpms is a list, containing compadre objects
class(mpms)
#> [1] "list"
class(mpms[[1]])
#> [1] "CompadreMat"
#> attr(,"package")
#> [1] "Rcompadre"

# extract list of matA and take mean
mats <- matA(mpms)
mat_elementwise_apply(mats, mean)
#>         [,1]   [,2]    [,3]    [,4]
#> [1,] 0.00000 0.0000 0.00000 1.96575
#> [2,] 0.19725 0.4090 0.17650 0.15400
#> [3,] 0.03250 0.1605 0.38175 0.37500
#> [4,] 0.00000 0.1175 0.26100 0.37150

# This should be the same as mat_mean()
mat_mean(mats)
#>         A1     A2      A3      A4
#> A1 0.00000 0.0000 0.00000 1.96575
#> A2 0.19725 0.4090 0.17650 0.15400
#> A3 0.03250 0.1605 0.38175 0.37500
#> A4 0.00000 0.1175 0.26100 0.37150

# Mean values, with 25% trimmed from each end
mat_elementwise_apply(mats, mean, trim = 0.25)
#>        [,1]  [,2]  [,3]   [,4]
#> [1,] 0.0000 0.000 0.000 1.5475
#> [2,] 0.1955 0.339 0.177 0.0835
#> [3,] 0.0175 0.186 0.327 0.3035
#> [4,] 0.0000 0.077 0.191 0.3075

# 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))
#>       [,1]  [,2]  [,3]  [,4]
#> [1,] 0.000 0.000 0.000 4.033
#> [2,] 0.105 0.483 0.154 0.167
#> [3,] 0.035 0.172 0.673 0.400
#> [4,] 0.000 0.081 0.096 0.350
mats[[2]]
#>       A1    A2    A3    A4
#> A1 0.000 0.000 0.000 4.033
#> A2 0.105 0.483 0.154 0.167
#> A3 0.035 0.172 0.673 0.400
#> A4 0.000 0.081 0.096 0.350

#min and max values
mat_elementwise_apply(mats, min)
#>       [,1]  [,2]  [,3]  [,4]
#> [1,] 0.000 0.000 0.000 0.735
#> [2,] 0.013 0.158 0.019 0.000
#> [3,] 0.000 0.024 0.200 0.143
#> [4,] 0.000 0.000 0.096 0.250
mat_elementwise_apply(mats, max)
#>       [,1]  [,2]  [,3]  [,4]
#> [1,] 0.000 0.000 0.000 4.033
#> [2,] 0.385 0.800 0.333 0.449
#> [3,] 0.095 0.246 0.673 0.750
#> [4,] 0.000 0.316 0.566 0.621

#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")
#>       [,1]  [,2]  [,3]  [,4]
#> [1,] 0.000 0.000 0.000 0.735
#> [2,] 0.013 0.158 0.019 0.000
#> [3,] 0.000 0.000 0.200 0.143
#> [4,] 0.000 0.000 0.096 0.250

#ignore the NA
mat_elementwise_apply(mats, min, na_handling = "ignore")
#>       [,1]  [,2]  [,3]  [,4]
#> [1,] 0.000 0.000 0.000 0.735
#> [2,] 0.013 0.158 0.019 0.000
#> [3,] 0.000    NA 0.200 0.143
#> [4,] 0.000 0.000 0.096 0.250

#ignore the NA, but pass na.rm = TRUE to the function (min)
mat_elementwise_apply(mats, min, na_handling = "ignore", na.rm = TRUE)
#>       [,1]  [,2]  [,3]  [,4]
#> [1,] 0.000 0.000 0.000 0.735
#> [2,] 0.013 0.158 0.019 0.000
#> [3,] 0.000 0.024 0.200 0.143
#> [4,] 0.000 0.000 0.096 0.250