[R] sparse matrix from vector outer product

Philipp A.
Wed, 14 Jan 2015 14:02:40 -0000

Hi,

creating a matrix from two vectors a, b by multiplying each combination can
be done e.g. via

a %*% t(b)

or via

outer(a, b)  # default for third argument is '*'

But this yields a normal matrix.

Is there an efficient way to create sparse matrices (from the Matrix
package) like that?

Right now i?m doing

a.sparse = as(a, 'sparseVector')
b.sparse = as(t(b), 'sparseMatrix')
a.sparse %*% b.sparse

but this strikes me as wasteful.
?

	[[alternative HTML version deleted]]