cumsum_byname: Cumulative sum that respects row and column names

View source: R/Unary.R

cumsum_bynameR Documentation

Cumulative sum that respects row and column names

Description

Provides cumulative sums along a list or column of a data frame. If a is a single number, a is returned. If a is a list of numbers, a list representing the cumulative sum of the numbers is returned. If a is a single matrix, a is returned. If a is a list of matrices, a list representing the cumulative sum of the matrices is returned. In this case, each entry in the returned list is sum "by name," such that row and column names of the matrices are respected.

Usage

cumsum_byname(a)

Arguments

a

A number, list of numbers, matrix or list of matrices for which cumulative sum is desired.

Details

If cumulative sums are desired in the context of a data frame, groups in the data frame are respected if mutate is used. See examples.

Value

A single number, list of numbers, a single matrix, or a list of matrices, depending on the nature of a.

Examples

library(dplyr)
library(tibble)
m1 <- matrix(c(1), nrow = 1, ncol = 1, dimnames = list("r1", "c1")) %>% 
  setrowtype("row") %>% setcoltype("col")
m2 <- matrix(c(2), nrow = 1, ncol = 1, dimnames = list("r2", "c2")) %>% 
  setrowtype("row") %>% setcoltype("col")
m3 <- matrix(c(3), nrow = 1, ncol = 1, dimnames = list("r3", "c3")) %>% 
  setrowtype("row") %>% setcoltype("col")
cumsum_byname(list(m1, m2, m3))
# Groups are respected in the context of mutate.
tibble(grp = c("A", "A", "B"), m = list(m1, m2, m3)) %>% group_by(grp) %>% 
  mutate(m2 = cumsum_byname(m))

matsbyname documentation built on Oct. 19, 2023, 5:11 p.m.