colsums_byname: Column sums, sorted by name

View source: R/Unary.R

colsums_bynameR Documentation

Column sums, sorted by name

Description

Calculates column sums for a matrix by premultiplying by an identity vector (containing all 1's). In contrast to colSums (which returns a numeric result), the return value from colsums_byname is a matrix. An optional rowname for the resulting row vector can be supplied. If rowname is NA (the default), the row name is set to the row type as given by rowtype(a). If rowname is set to NULL, the row name is returned empty.

Usage

colsums_byname(a, rowname = NA)

Arguments

a

A matrix or list of matrices from which column sums are desired.

rowname

The name of the output row containing column sums.

Value

A row vector of type matrix containing the column sums of a.

Examples

library(dplyr)
colsums_byname(42)
m <- matrix(c(1:6), nrow = 2, dimnames = list(paste0("i", 1:2), paste0("c", 3:1))) %>%
  setrowtype("Industries") %>% setcoltype("Commodities")
m
colsums_byname(m)
colsums_byname(m, rowname = "E.ktoe")
m %>% 
  colsums_byname() %>% 
  rowsums_byname()
# This also works with lists
colsums_byname(list(m, m))
colsums_byname(list(m, m), rowname = "E.ktoe")
colsums_byname(list(m, m), rowname = NA)
colsums_byname(list(m, m), rowname = NULL)
DF <- data.frame(m = I(list()))
DF[[1,"m"]] <- m
DF[[2,"m"]] <- m
colsums_byname(DF$m[[1]])
colsums_byname(DF$m)
colsums_byname(DF$m, "sums")
res <- DF %>% mutate(
  cs = colsums_byname(m),
  cs2 = colsums_byname(m, rowname = "sum")
)
res$cs2

MatthewHeun/byname documentation built on Feb. 17, 2024, 4:51 p.m.