Performs a union and sorting of addend and augend row and column names prior to summation.
Zeroes are inserted for missing matrix elements.
Treats missing or NULL
operands as 0
.
sum_byname(...)
... | operands: constants, matrices, or lists of matrices |
---|
A matrix representing the name-wise sum of addend
and augend
#> [1] 4sum_byname(2, 2, 2)#> [1] 6sum_byname(2, 2, -2, -2)#> [1] 0productnames <- c("p1", "p2") industrynames <- c("i1", "i2") U <- matrix(1:4, ncol = 2, dimnames = list(productnames, industrynames)) %>% setrowtype("Products") %>% setcoltype("Industries") Y <- matrix(1:4, ncol = 2, dimnames = list(rev(productnames), rev(industrynames))) %>% setrowtype("Products") %>% setcoltype("Industries") sum_byname(U, 100)#> i1 i2 #> p1 101 103 #> p2 102 104 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries"sum_byname(200, Y)#> i1 i2 #> p1 204 202 #> p2 203 201 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries"U + Y # Non-sensical. Row and column names not respected.#> i1 i2 #> p1 2 6 #> p2 4 8 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries"sum_byname(U, U)#> i1 i2 #> p1 2 6 #> p2 4 8 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries"sum_byname(U, Y)#> i1 i2 #> p1 5 5 #> p2 5 5 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries"sum_byname(U, U, Y, Y)#> i1 i2 #> p1 10 10 #> p2 10 10 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries"V <- matrix(1:4, ncol = 2, dimnames = list(industrynames, productnames)) %>% setrowtype("Industries") %>% setcoltype("Products") U + V # row and column names are non-sensical and blindly taken from first argument (U)#> i1 i2 #> p1 2 6 #> p2 4 8 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries"if (FALSE) sum_byname(U, V) # Fails, because row and column types are different # This also works with lists sum_byname(list(U,U), list(Y,Y))#> [[1]] #> i1 i2 #> p1 5 5 #> p2 5 5 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries" #> #> [[2]] #> i1 i2 #> p1 5 5 #> p2 5 5 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries" #>#> [[1]] #> i1 i2 #> p1 101 103 #> p2 102 104 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries" #> #> [[2]] #> i1 i2 #> p1 101 103 #> p2 102 104 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries" #>#> [[1]] #> i1 i2 #> p1 101 103 #> p2 102 104 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries" #> #> [[2]] #> i1 i2 #> p1 101 103 #> p2 102 104 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries" #>DF <- data.frame(U = I(list()), Y = I(list())) DF[[1,"U"]] <- U DF[[2,"U"]] <- U DF[[1,"Y"]] <- Y DF[[2,"Y"]] <- Y sum_byname(DF$U, DF$Y)#> [[1]] #> i1 i2 #> p1 5 5 #> p2 5 5 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries" #> #> [[2]] #> i1 i2 #> p1 5 5 #> p2 5 5 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries" #>#> U Y sums #> 1 1, 2, 3, 4 1, 2, 3, 4 5, 5, 5, 5 #> 2 1, 2, 3, 4 1, 2, 3, 4 5, 5, 5, 5sum_byname(U) # If only one argument, return it.#> i1 i2 #> p1 1 3 #> p2 2 4 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries"sum_byname(2, NULL) # Gives 2#> [1] 2sum_byname(2, NA) # Gives NA#> [1] NAsum_byname(NULL, 1) # Gives 1#> [1] 1#> [[1]] #> [1] 1 #> #> [[2]] #> [1] 2 #>DF2 <- data.frame(U = I(list()), Y = I(list())) DF2[[1,"U"]] <- NULL DF2[[2,"U"]] <- U DF2[[1,"Y"]] <- Y DF2[[2,"Y"]] <- Y sum_byname(DF2$U, DF2$Y)#> [[1]] #> i1 i2 #> p1 4 2 #> p2 3 1 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries" #> #> [[2]] #> i1 i2 #> p1 5 5 #> p2 5 5 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries" #>#> U Y sums #> 1 1, 2, 3, 4 4, 3, 2, 1 #> 2 1, 2, 3, 4 1, 2, 3, 4 5, 5, 5, 5DF3$sums[[1]]#> i1 i2 #> p1 4 2 #> p2 3 1 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries"DF3$sums[[2]]#> i1 i2 #> p1 5 5 #> p2 5 5 #> attr(,"rowtype") #> [1] "Products" #> attr(,"coltype") #> [1] "Industries"