
Class for symmetric correlation matrices
upper_triangular_sym_mat.RdThe class allows to store the upper triangular matrix of a symmetric matrix (think correlation matrix, distance matrix, etc.) in an memory-efficient form and return a data.table or dense (or sparse) R matrix if need be.
Methods
upper_triangular_sym_mat$new()
Initialises the R6 class.
Usage
upper_triangular_sym_mat$new(values, features, shift)Arguments
valuesNumerical vector. The correlation coefficients of the upper triangular correlation matrix stored as a row-major vector
featuresString vector. The features of the correlation matrix.
shiftBollean Was a shift applied during the calculation of the upper triangular matrix. If
FALSE, the diagonal was included, ifTRUE, the diagonal was removed.
upper_triangular_sym_mat$get_data_table()
Returns the data in form of a data.table.
Examples
# store the off-diagonal of a correlation matrix and get it back
set.seed(42)
cor_mat <- cor(matrix(rnorm(80), nrow = 20, ncol = 4))
object <- upper_triangular_sym_mat$new(
values = cor_mat[lower.tri(cor_mat)],
features = sprintf("gene_%i", 1:4),
shift = TRUE
)
object$get_data_table(.verbose = FALSE)
#> feature_a feature_b sim
#> <char> <char> <num>
#> 1: gene_1 gene_2 0.43694936
#> 2: gene_1 gene_3 0.04324370
#> 3: gene_1 gene_4 -0.03960938
#> 4: gene_2 gene_3 0.03976509
#> 5: gene_2 gene_4 -0.06555478
#> 6: gene_3 gene_4 0.33289052
dim(object$get_sym_matrix(.verbose = FALSE))
#> [1] 4 4