user1631306
user1631306

Reputation: 4470

mathematical operations on data.table(in R)

I heard (from my frnd) that mathematical operations on data.table is way faster than on matrix. I am trying to compute the dot.product of two matrix of size 30kx30k, and looking for the time taken

matrix1 = matrix(rexp(200, rate=.1), ncol=30000,nrow=30000)
matrix2 = matrix(rexp(200, rate=.1), ncol=30000,nrow=30000)
product = matrix1 %*% matrix2

Same thing I want to do with using data.table

dt1<- as.data.table(matrix1)
dt2<- as.data.table(matrix2)

Can you please tell me if there is easier way to do dot product on data.table(without converting them into matrix)?

Upvotes: 4

Views: 1753

Answers (1)

Ricardo Saporta
Ricardo Saporta

Reputation: 55350

The premise in this question is incorrect. data.tables, like data.frames are lists of vectors.

In contrast, a matrix is a single vector with a dimension attribute.

There is an overhead associated to lists, which can be avoided if your data can fit a matrix.

data.tables are faster relative to data.frames (and depending on the application, lists themselves), or when using another vector as an index to iterate against.

However, for straight matrix multiplication, stick to matrix

Upvotes: 9

Related Questions