array: Sparse Arrays ("Tensors")

Description Usage Arguments Details Value Note Author(s) Examples

Description

Convenient function linking sparse Arrays from the package spam to the sparse Matrices from the package Matrix.

Usage

1
2
3
4

Arguments

A

An array to be turned into a sparse Array using as.simple_sparse_array. Can also be a dataframe, but see Details below about the treatment of data frames here.

i

Integer matrix of array indices passed to simple_sparse_array.

v

vector of values passed to simple_sparse_array. If NULL (by default), all specified indices (i.e. all rows in i) are given the value 1.

M

Matrix of type simple_triple_matrix from the package spam to be turned into a TsparseMatrix from the packages Matrix.

...

Further arguments passed to simple_sparse_array.

Details

Array turns an array into a sparse Array. There is a special behavior when a dataframe is supplied. Such a dataframe is treated as 'long format', i.e. the columns of the dataframe are treated as dimensions of the Array, and all rows of the dataframe are interpreted as entries. The coordinates are given by the ordering of the levels in the dataframe, and the dimnames are given by the levels.

sparseArray constructs sparse Arrays from a matrix of indices and a vector of values. dim and dimnames can be added as in simple_sparse_array

as.Matrix turns a simple_triplet_matrix into a dgTMatrix.

Value

Sparse Arrays use the class "simple_sparse_array" from spam

Note

These functions are only an example of how spam can be linked to Matrix.

Author(s)

Michael Cysouw

Examples

1
2
3
4
5
6
x <- matrix(c(1, 0, 0, 2), nrow = 2)
s <- as.simple_triplet_matrix(x)
str(s)

as.Matrix(s)
str(as.Matrix(s))

Example output

Loading required package: Matrix
Loading required package: slam
List of 6
 $ i       : int [1:2] 1 2
 $ j       : int [1:2] 1 2
 $ v       : num [1:2] 1 2
 $ nrow    : int 2
 $ ncol    : int 2
 $ dimnames: NULL
 - attr(*, "class")= chr "simple_triplet_matrix"
2 x 2 sparse Matrix of class "dgTMatrix"
        
[1,] 1 .
[2,] . 2
Formal class 'dgTMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:2] 0 1
  ..@ j       : int [1:2] 0 1
  ..@ Dim     : int [1:2] 2 2
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : NULL
  ..@ x       : num [1:2] 1 2
  ..@ factors : list()

qlcMatrix documentation built on May 2, 2019, 9:14 a.m.