rgrids
is an R
package that allows you to work consistently with grids and matrices.
rgrids
facilitates density analysis in 1D, 2D and 3D. It is also possible to deal with block matrices and perform operations on lists of matrices. Further developments will concern operations on first neighbors and in general on matrix elements.
rgrids
is mainly developed in R
, but uses the C++
language to increase performance and use less memory.
rgrids
hasn't been released on CRAN yet, so you can install it directly from this repository
devtools::install_github("Leonardo-Bo/rgrids")
If you have already installed the package, you can check for a new version and update it
devtools::update_packages("rgrids")
The purpose of rgrids
is to collect a not too large number of functions that work in a coherent way to solve some recurring problems on grids and matrices. Below is a list of the functions currently available
the functions that operate mainly on the grids are:
makeGrid1d()
: creates a 1D grid object, i.e. divides a plane into strips or a straight linemakeGrid2d()
: creates a 2D grid object, i.e. divides a plane into rectanglesmakeGrid3d()
: creates a 2D grid object, i.e. divides a space into parallelepipedsgetBoxplot()
: for a 1D grid it associates all y values within a strip to the same xgetCell()
: associates each point of a 1D, 2D or 3D array to its respective strip, rectangle or parallelepipedgetCounts()
: generates a dataframe with three columns: the coordinates of each cell of the grid and the number of elements that fall inside each cellthe functions that operate mainly on block matrices are:
blockmax()
: given a matrix and the size of a block, it returns a new matrix containing the max
of each blockblockmin()
: given a matrix and the size of a block, it returns a new matrix containing the min
of each blockblockmean()
: given a matrix and the size of a block, it returns a new matrix containing the mean
of each blockblocklist()
: given a matrix and the size of a block, it returns a list containing all submatrices made by blocksdblocklist()
: given a matrix and the size of a block, it returns a list all diagonal submatrices made by blocksmeanMatrix()
: given a list of matrix, it returns a single matrix in which element (i, j) is the average of all corresponding elements (i, j) of all matrices in the listsumMatrix()
: given a list of matrix, it returns a single matrix in which element (i, j) is the sum of all corresponding elements (i, j) of all matrices in the list dotMatrix()
: given a list of matrix, it returns a single matrix in which element (i, j) is the product of all corresponding elements (i, j) of all matrices in the listthe functions that operate on rows and columns of a matrix are:
colMax()
: given a matrix, it returns the max
of each column colMin()
: given a matrix, it returns the min
of each columnrowMax()
: given a matrix, it returns the max
of each rowrowMin()
: given a matrix, it returns the min
of each rowthe functions that connect matrices and dataframes are:
pileMatrix()
: it transforms a matrix into a dataframe with three columns: row and column coordinates of each matrix element and value of each respective matrix element; it allows different selection options, for example the whole matrix or only the upper triangular matrix, ...getTriang()
: given the number of elements of a square matrix represented as a three-column dataframe (obtained for example from pileMatrix()
), it filters only the desired elements, i.e. the elements of the upper or lower triangular matrix with or without diagonal in the main or mirrored direction.other functions:
tableToLatex()
: given a numeric matrix or a data.frame, it returns a basic LaTeX table write with table and tabular packages; rownames and colnames are highlighted with bold.Generate random points on a plane
```r library(rgrids)
df_points <- data.frame( x = c(rnorm(n = 50000, mean = -2), rnorm(n = 50000, mean = 2)), y = c(rnorm(n = 50000, mean = 1), rnorm(n = 50000, mean = -1)) ) ```
Define a grid that contains all the points generated
r
the_grid <- makeGrid2d(
xmin = floor(min(df_points$x)), ymin = floor(min(df_points$y)),
xmax = ceiling(max(df_points$x)), ymax = ceiling(max(df_points$y)),
xcell = 50, ycell = 50, by = "v"
)
class : Grid2d
dimensions : xcell = 50, ycell = 50, ncell = 2500
range : xmin = -7, xmax = 7
ymin = -6, ymax = 6
by : v, count starts from xmin, ymin (bottom-left)
and y increase faster
Match each point with a grid element
r
grid_index <- getCell(the_grid, df_points)
df_points$grid_index <- grid_index
Count the points within each grid element
r
df_grid <- getCounts(the_grid, grid_index)
Given a matrix
mat1 <- matrix(1:64, nrow = 8, byrow = TRUE)
Calculate the average on 2x2 blocks
r
blockmean(mat1, 2)
```
```
Get a list of 4x4 matrices
r
blocklist(mat1, 4)
```
1
2
3
4
```
from a list of matrices obtain the sum of each element of the matrix
```r
mat1
and using blocklist
list_matrix <- blocklist(mat1, 4)
sumMatrix(list_matrix)
```
Given a matrix
mat2 <- matrix(1:100, ncol = 10)
mat1
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 11 21 31 41 51 61 71 81 91
## [2,] 2 12 22 32 42 52 62 72 82 92
## [3,] 3 13 23 33 43 53 63 73 83 93
## [4,] 4 14 24 34 44 54 64 74 84 94
## [5,] 5 15 25 35 45 55 65 75 85 95
## [6,] 6 16 26 36 46 56 66 76 86 96
## [7,] 7 17 27 37 47 57 67 77 87 97
## [8,] 8 18 28 38 48 58 68 78 88 98
## [9,] 9 19 29 39 49 59 69 79 89 99
## [10,] 10 20 30 40 50 60 70 80 90 100
rowMax(mat1)
## value index
## 1 91 10
## 2 92 10
## 3 93 10
## 4 94 10
## 5 95 10
## 6 96 10
## 7 97 10
## 8 98 10
## 9 99 10
## 10 100 10
rowMin(mat1)
## value index
## 1 1 1
## 2 2 1
## 3 3 1
## 4 4 1
## 5 5 1
## 6 6 1
## 7 7 1
## 8 8 1
## 9 9 1
## 10 10 1
colMax(mat1)
## value index
## 1 10 10
## 2 20 10
## 3 30 10
## 4 40 10
## 5 50 10
## 6 60 10
## 7 70 10
## 8 80 10
## 9 90 10
## 10 100 10
colMin(mat1)
## value index
## 1 1 1
## 2 11 1
## 3 21 1
## 4 31 1
## 5 41 1
## 6 51 1
## 7 61 1
## 8 71 1
## 9 81 1
## 10 91 1
A performance comparison with apply
using the bench
package
mat3 <- matrix(runif(500000), ncol = 500)
rowMax2 <- function(mat) {
df_rowMax <- data.frame(value = apply(mat, 1, max), index = apply(mat, 1, which.max))
return(df_rowMax)
}
colMax2 <- function(mat) {
df_colMax <- data.frame(value = apply(mat, 2, max), index = apply(mat, 2, which.max))
return(df_colMax)
}
bench::mark(colMax(mat3), colMax2(mat3))
## # A tibble: 2 x 6
## expression min median `itr/sec` mem_alloc `gc/sec`
## <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
## 1 colMax(mat3) 821.88µs 887.2µs 1065. 22.3KB 2.02
## 2 colMax2(mat3) 9.15ms 10.7ms 87.1 19.2MB 56.8
bench::mark(rowMax(mat3), rowMax2(mat3))
## # A tibble: 2 x 6
## expression min median `itr/sec` mem_alloc `gc/sec`
## <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
## 1 rowMax(mat2) 1.07ms 1.15ms 776. 41.8KB 0
## 2 rowMax2(mat2) 11.75ms 13.36ms 71.8 19.3MB 92.3
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.