How to multiply e.g. three lists in data table? Two lists are easy:
如何乘以例如数据表中有三个列表?两个列表很简单:
DT <- data.table(a = list(2,2,c(3,3),c(2,2)), b = list(2,2,c(3,3),c(2,2)), d = list(2,2,c(3,3),c(2,2)) )
DT[, e := Map("*", a , b)]
My expected result is
我的预期结果是
DT <- data.table(a = list(2,2,c(3,3),c(2,2)), b = list(2,2,c(3,3),c(2,2)), d = list(2,2,c(3,3),c(2,2)), e = list(8,8,c(27,27), c(8,8)) )
Solution (DT[, e := Map("*", a , b, c )])
is not working, I also tried to use Reduce in place of Map, but it did not helped.
解决方案(DT [,e:= Map(“*”,a,b,c)])不起作用,我也尝试使用Reduce代替Map,但它没有帮助。
1 个解决方案
#1
0
The *
is a binary operator. How about this:
*是二元运算符。这个怎么样:
DT[, e := Map('*',DT$a,DT$b)]
DT[, e := Map('*',DT$d,DT$e)]
or
DT[, e := Map('*',Map('*',DT$a,DT$b),DT$d)]
#1
0
The *
is a binary operator. How about this:
*是二元运算符。这个怎么样:
DT[, e := Map('*',DT$a,DT$b)]
DT[, e := Map('*',DT$d,DT$e)]
or
DT[, e := Map('*',Map('*',DT$a,DT$b),DT$d)]