colsums_byname | R Documentation |
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.
colsums_byname(a, rowname = NA)
a |
A matrix or list of matrices from which column sums are desired. |
rowname |
The name of the output row containing column sums. |
A row vector of type matrix
containing the column sums of a
.
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.