这可行,但我真的不喜欢在r中使用循环
for(x in 1:nWeeks)
{
WSResultsres[,x] <-rowSums(SSresTrim[,x,]*LeafWeights);
}
SSres是一个数组[1:55470,1:141,1:20]和
str(SSresTrim)
num [1:55470, 1:141, 1:20] 0 0 0 0 0 0 0 0 0 0 ...
LeafWeights是数字df [1:55470,1:20]
我可以做一个2步,然后先将它们相乘,但是当我这样做时:
WSResultsres<-sweep(SSresTrim,c(1,3),LeafWeights,'*')
我明白了
Error in FUN(x, aperm(array(STATS, dims[perm]), order(perm)), ...) :
non-numeric argument to binary operator
将LeafWeights扩展到数组并进行元素明智的乘法工作,并且比循环快得多,但是感觉不太好。
mLeafWeights<-as.matrix(LeafWeights)
dimnames(mLeafWeights) <- NULL
LeafWeightsArray <- array(mLeafWeights,
dim=c(nrow(LeafWeights),nWeeks,ncol(LeafWeights)));
WSResultsres<-SSresTrim*LeafWeightsArray
#and then do a sum on dim=2
有人有更经典的东西吗?谢谢 -里克