logarithmicmean_byname: Name- and element-wise logarithmic mean of matrices

View source: R/Binary.R

logarithmicmean_bynameR Documentation

Name- and element-wise logarithmic mean of matrices


The logarithmic mean of corresponding entries of a and b is 0 if a = 0 or b = 0, a if a = b, or (b - a) / (log(b) - log(a)) otherwise.


logarithmicmean_byname(a, b, base = exp(1))



first operand (a matrix or constant value or lists of same).


second operand (a matrix or constant value or lists of same).


the base of the logarithm used when computing the logarithmic mean. (Default is base = exp(1).)


This function performs a union and sorting of row and column names prior to performing logarithmic mean. Zeroes are inserted for missing matrix elements.

Internally, the third condition is implemented as (b - a) / log(b/a).

Note that (b - a) / log(b/a) = (a - b) / log(a/b), so logarithmic mean is commutative; the order of arguments a and b does not change the result.


A matrix representing the name-wise logarithmic mean of a and b.


m1 <- matrix(c(1:6), nrow = 3, ncol = 2) %>% 
  setrownames_byname(c("r1", "r2", "r3")) %>% setcolnames_byname(c("c1", "c2")) %>% 
  setrowtype("row") %>% setcoltype("col")
m2 <- matrix(c(7:12), nrow = 3, ncol = 2) %>% 
  setrownames_byname(c("r2", "r3", "r4")) %>% setcolnames_byname(c("c2", "c3")) %>% 
  setrowtype("row") %>% setcoltype("col")
logarithmicmean_byname(m1, m2)
# This also works with lists
logarithmicmean_byname(list(m1, m1), list(m2, m2))
DF <- data.frame(m1 = I(list()), m2 = I(list()))
DF[[1,"m1"]] <- m1
DF[[2,"m1"]] <- m1
DF[[1,"m2"]] <- m2
DF[[2,"m2"]] <- m2
logarithmicmean_byname(DF$m1, DF$m2)
DF %>% mutate(logmeans = logarithmicmean_byname(m1, m2))

matsbyname documentation built on April 2, 2022, 1:06 a.m.