akash87
akash87

Reputation: 3994

Multiplying an array with data.table in R

I have a data.table named dt that looks as follows:

 ID V1 V2 V3 V4 V5 time color
 1  F  T  F  F  T  1    red
 1  F  T  T  F  T  2    red
 2  T  F  T  F  F  1    blue
 3  F  F  F  F  F  2    green
 3  T  T  T  F  T  3    purple

In reality, dim(dt) = [1321221 123]. Now I know that in general, true and false are stored as 1 and 0, respectively, in R. I also have an array l, though that looks like

 V1 V2 V3 V4 V5
 1  2  1  3  4

These are weights assigned to V1,V2,V3,V4,V5. I would like to multiply these weights to the true values (as they have a numeric value of 1) and add across each row, like we can do with matrices. The output should look like

ID Total time color
1  6     1    red
1  7     2    red
2  2     1    blue
3  0     2    green
3  8     3    purple

Now remember, this is actually a tiny subset of the real data, so I am looking for a fast solution, and preferably a data.table solution so I can improve on my data.table skills.

Upvotes: 2

Views: 192

Answers (2)

ddunn801
ddunn801

Reputation: 1890

@Eddi has a cleaner answer, but if you need something in version 1.9.6:

dt[, Total := apply((t(l * t(dt[, 2:6, with = FALSE]))),1,sum)]
dt[, 2:6 := NULL]
setcolorder(dt, c("ID", "Total", "time", "color"))

Upvotes: 1

eddi
eddi

Reputation: 49448

dt[, .(ID, Reduce(`+`, Map(`*`, .SD, l)), time, color), .SDcols = V1:V5]
#   ID V2 time  color
#1:  1  6    1    red
#2:  1  7    2    red
#3:  2  2    1   blue
#4:  3  0    2  green
#5:  3  8    3 purple

# or using matrix product

dt[, .(ID, as.matrix(.SD) %*% t(l), time, color), .SDcols = V1:V5]

Upvotes: 6

Related Questions