Q:

Product of a set of columns in data.table in R

I have a data.table given as below
> temp = data.table(A = runif(10), B = runif(10), C = runif(10))
> temp
            A         B          C
 1: 0.9780187 0.3706999 0.38986569
 2: 0.3614894 0.4578619 0.47973024
 3: 0.1974928 0.2198876 0.79985610
 4: 0.2509917 0.7791180 0.13150595
 5: 0.5975064 0.1469739 0.78714248
 6: 0.7823076 0.1806161 0.04970207
 7: 0.9637526 0.4106579 0.63153566
 8: 0.6985434 0.5870737 0.53784899
 9: 0.9755159 0.1097774 0.18338828
10: 0.5010543 0.9847809 0.99354549

I want to compute the product of all of these columns. How can I do that using data.table?

A:

With data.table, .SD indicates Subset of Data.table.  If we don't specify any columns to be selected in .SDcols, then it is the whole dataset .  Using Reduce with *, it does multiplication of corresponding elements of each column of the dataset
temp[, Reduce(`*`, .SD)]

gives a vector of multiplied values for each row
If the intention is to create a column in the dataset, assign (:=) it to a new column
temp[, prodCol := Reduce(`*`, .SD)]

