[R] sparse matrix from vector outer product

Philipp A. flying-sheep at web.de
Wed Jan 14 15:02:40 CET 2015


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]]



More information about the R-help mailing list