setrowtype: Sets row type for a matrix or a list of matrices

View source: R/Utilities.R

setrowtypeR Documentation

Sets row type for a matrix or a list of matrices

Description

This function is a wrapper for attr() so that setting can be accomplished by the pipe operator (⁠%>%⁠). Row types are strings stored in the rowtype attribute.

Usage

setrowtype(a, rowtype)

Arguments

a

The matrix on which row type is to be set.

rowtype

The type of item stored in rows.

Details

If is.null(rowtype), the rowtype attribute is deleted and subsequent calls to rowtype will return NULL.

Value

a with rowtype attribute set to rowtype.

Examples

library(dplyr)
commoditynames <- c("c1", "c2")
industrynames <- c("i1", "i2")
U <- matrix(1:4, ncol = 2, dimnames = list(commoditynames, industrynames))
U %>% setrowtype("Commodities")
# This also works for lists
setrowtype(list(U,U), rowtype = "Commodities")
setrowtype(list(U,U), rowtype = list("Commodities", "Commodities"))
DF <- data.frame(U = I(list()))
DF[[1,"U"]] <- U
DF[[2,"U"]] <- U
setrowtype(DF$U, "Commodities")
DF <- DF %>% mutate(newcol = setrowtype(U, "Commodities"))
DF$newcol[[1]]
DF$newcol[[2]]

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