rowsums_byname: Row sums, sorted by name

View source: R/Unary.R

rowsums_bynameR Documentation

Row sums, sorted by name

Description

Calculates row sums for a matrix by post-multiplying by an identity vector (containing all 1's). In contrast to rowSums (which returns a numeric result), the return value from rowsums_byname is a matrix. An optional colname for the resulting column vector can be supplied. If colname is NULL or NA (the default), the column name is set to the column type as given by coltype(a). If colname is set to NULL, the column name is returned empty.

Usage

rowsums_byname(a, colname = NA)

Arguments

a

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

colname

The name of the output column containing row sums.

Value

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

Examples

library(dplyr)
rowsums_byname(42)
m <- matrix(c(1:6), ncol = 2, dimnames = list(paste0("i", 3:1), paste0("c", 1:2))) %>%
  setrowtype("Industries") %>% setcoltype("Commodities")
m
rowsums_byname(m)
rowsums_byname(m, "E.ktoe")
# This also works with lists
rowsums_byname(list(m, m))
rowsums_byname(list(m, m), "E.ktoe")
rowsums_byname(list(m, m), NA)
rowsums_byname(list(m, m), NULL)
DF <- data.frame(m = I(list()))
DF[[1,"m"]] <- m
DF[[2,"m"]] <- m
rowsums_byname(DF$m[[1]])
rowsums_byname(DF$m)
ans <- DF %>% mutate(rs = rowsums_byname(m))
ans
ans$rs[[1]]
# Nonsensical
## Not run: rowsums_byname(NULL)

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