I am trying to subtract each column from each other column in a large R data.table, that has 13125 columns and 90 rows.
I am following up on a previous question which addresses this for data.tables of smaller dimensions (Subtract every column from each other column in a R data.table).
My problem is that I am currently running out of memory to generate the resulting data.table of column combinations (which seems to require 59.0GB).
My question is, is there a more memory-efficient way to calculate the column differences with combn or perhaps another function for larger datasets?
The code I have been using is:
# I have a data.table of 13125 columns and 90 rows, called data.
# use combn to generate all possible pairwise column combinations (column + column),
# then within this apply a function to subtract the column value from its paired column value.
# this is done for each row, to produce a new datatable called res.
res <- as.data.table(combn(colnames(data), 2, function(x) data[[x[1]]] - data[[x[2]]]))
# take the pairwise column combinations and paste the pairing as the new column name
colnames(res) <- combn(colnames(data), 2, paste, collapse="_")
I apologise if this question is too similar and therefore considered a duplication. I would be very grateful for any advice with how to improve the efficiency of this code for the scale of my data.