Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 249
user1631306 Avatar asked Jan 11 '23 02:01

user1631306


1 Answers

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

like image 164
Ricardo Saporta Avatar answered Jan 19 '23 19:01

Ricardo Saporta