Learn R Programming

matsbyname (version 0.6.11)

prodall_byname: Product of all elements in a matrix

Description

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

Usage

prodall_byname(a)

Value

The product of all elements in a as a numeric.

Arguments

a

The matrix whose elements are to be multiplied.

Examples

Run this code
library(dplyr)
M <- matrix(2, nrow=2, ncol=2, dimnames = list(paste0("i", 1:2), paste0("c", 1:2))) %>%
  setrowtype("Industry") %>% setcoltype("Product")
prodall_byname(M)
rowprods_byname(M) %>% colprods_byname
# Also works for lists
prodall_byname(list(M,M))
DF <- data.frame(M = I(list()))
DF[[1,"M"]] <- M
DF[[2,"M"]] <- M
prodall_byname(DF$M[[1]])
prodall_byname(DF$M)
res <- DF %>% mutate(
  prods = prodall_byname(M)
)
res$prods

Run the code above in your browser using DataLab