sumall_byname {matsbyname}R Documentation

Sum of all elements in a matrix

Description

This function is equivalent to ⁠a \%>\% rowsums_byname() \%>\% colsums_byname()⁠, but returns a single numeric value instead of a 1x1 matrix.

Usage

sumall_byname(a)

Arguments

a

The matrix whose elements are to be summed.

Value

The sum of all elements in a as a numeric.

Examples

library(dplyr)
sumall_byname(42)
m <- matrix(2, nrow=2, ncol=2, dimnames = list(paste0("i", 1:2), paste0("c", 1:2))) %>%
  setrowtype("Industry") %>% setcoltype("Commodity")
m
sumall_byname(m)
rowsums_byname(m) %>% colsums_byname
# Also works for lists
sumall_byname(list(m,m))
DF <- data.frame(m = I(list()))
DF[[1,"m"]] <- m
DF[[2,"m"]] <- m
sumall_byname(DF$m[[1]])
sumall_byname(DF$m)
res <- DF %>% mutate(
  sums = sumall_byname(m)
)
res$sums
sumall_byname(list(m, NULL))

[Package matsbyname version 0.6.10 Index]