introducing diagonals
A new R package diagonals is now available on CRAN. The package implements several tools for dealing with fat diagonals on matrices, such as this one:
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
## [1,] 1 1 1 1 0 0 0 0 0 0 0 0
## [2,] 1 1 1 1 0 0 0 0 0 0 0 0
## [3,] 1 1 1 1 0 0 0 0 0 0 0 0
## [4,] 1 1 1 1 0 0 0 0 0 0 0 0
## [5,] 0 0 0 0 1 1 1 1 0 0 0 0
## [6,] 0 0 0 0 1 1 1 1 0 0 0 0
## [7,] 0 0 0 0 1 1 1 1 0 0 0 0
## [8,] 0 0 0 0 1 1 1 1 0 0 0 0
## [9,] 0 0 0 0 0 0 0 0 1 1 1 1
## [10,] 0 0 0 0 0 0 0 0 1 1 1 1
## [11,] 0 0 0 0 0 0 0 0 1 1 1 1
## [12,] 0 0 0 0 0 0 0 0 1 1 1 1Fat diagonal matrices occur when we combine two dimensions of a data set along one edge of a matrix. For example, trade-flow data in the decompr and gvc package have each country-industry combination occur on each edge of the matrix.
The workhorse function of this package is the fatdiag function, which tries behave similarly to the diag function from the base package, but then with diagonals being fat.
We can also use the function to assign values to the diagonal.
( m <- matrix(111, nrow=6, ncol=9) )## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,] 111 111 111 111 111 111 111 111 111
## [2,] 111 111 111 111 111 111 111 111 111
## [3,] 111 111 111 111 111 111 111 111 111
## [4,] 111 111 111 111 111 111 111 111 111
## [5,] 111 111 111 111 111 111 111 111 111
## [6,] 111 111 111 111 111 111 111 111 111fatdiag(m, steps=3) <- 5
m## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,] 5 5 5 111 111 111 111 111 111
## [2,] 5 5 5 111 111 111 111 111 111
## [3,] 111 111 111 5 5 5 111 111 111
## [4,] 111 111 111 5 5 5 111 111 111
## [5,] 111 111 111 111 111 111 5 5 5
## [6,] 111 111 111 111 111 111 5 5 5As can be seen from the above example, the blocks and matrices do not have to be square.
The diagonal of a matrix can also be extracted.
fatdiag(m, steps=3)## [1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5We can also specify the size of the block in stead of the number of steps.
fatdiag(12, size=4)## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
## [1,] 1 1 1 1 0 0 0 0 0 0 0 0
## [2,] 1 1 1 1 0 0 0 0 0 0 0 0
## [3,] 1 1 1 1 0 0 0 0 0 0 0 0
## [4,] 1 1 1 1 0 0 0 0 0 0 0 0
## [5,] 0 0 0 0 1 1 1 1 0 0 0 0
## [6,] 0 0 0 0 1 1 1 1 0 0 0 0
## [7,] 0 0 0 0 1 1 1 1 0 0 0 0
## [8,] 0 0 0 0 1 1 1 1 0 0 0 0
## [9,] 0 0 0 0 0 0 0 0 1 1 1 1
## [10,] 0 0 0 0 0 0 0 0 1 1 1 1
## [11,] 0 0 0 0 0 0 0 0 1 1 1 1
## [12,] 0 0 0 0 0 0 0 0 1 1 1 1This also gives us flexibility in terms of having non-square blocks (and consequently matrices).
fatdiag(12, size=c(3,4) )## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
## [1,] 1 1 1 1 0 0 0 0 0 0 0 0
## [2,] 1 1 1 1 0 0 0 0 0 0 0 0
## [3,] 1 1 1 1 0 0 0 0 0 0 0 0
## [4,] 0 0 0 0 1 1 1 1 0 0 0 0
## [5,] 0 0 0 0 1 1 1 1 0 0 0 0
## [6,] 0 0 0 0 1 1 1 1 0 0 0 0
## [7,] 0 0 0 0 0 0 0 0 1 1 1 1
## [8,] 0 0 0 0 0 0 0 0 1 1 1 1
## [9,] 0 0 0 0 0 0 0 0 1 1 1 1Installation
The diagonals packge is now available on CRAN and can therefore be installed directly from inside R using:
install.packages("diagonals")Subsequently the package can be loaded using:
library(diagonals)The above introduction is also available as a vignette that is included in the package.
It can be accessed from R using:
vignette("fatdiag")
# or
browseVignettes(package = "diagonals")Development
The development version, to be used at your peril, can be installed using:
if (!require('devtools')) install.packages('devtools')
devtools::install_github("bquast/decompr")Development takes place on the GitHub page, bugs can be filed on the issues page.