在R中对非常大的数据帧执行sumproduct的计算效率最高的方法

在R中对非常大的数据帧执行sumproduct的计算效率最高的方法,r,performance,R,Performance,我有一个非常大的数据帧(数百列和数万行),我想对其执行以下操作: 在每一行上,使用给定的权重向量计算列上元素的加权组合(某些权重可能为零) 创建一个新的数据帧,其中每个列都包含相应权重向量的加权组合 计算效率最高的方法是什么 复制简单示例: require(tidyverse) theDatesTibble <-c(lubridate::mdy("3/15/2017"), lubridate::mdy("4/15/2017"), lubridate::mdy("5/15/2017")

我有一个非常大的数据帧(数百列和数万行),我想对其执行以下操作:

  • 在每一行上,使用给定的权重向量计算列上元素的加权组合(某些权重可能为零)
  • 创建一个新的数据帧,其中每个列都包含相应权重向量的加权组合
计算效率最高的方法是什么

复制简单示例:

require(tidyverse)

theDatesTibble <-c(lubridate::mdy("3/15/2017"), lubridate::mdy("4/15/2017"), lubridate::mdy("5/15/2017"), lubridate::mdy("6/15/2017"))

theValuesTibble_A <- c(123.45, 201.29, 337.78, 275.98)
theValuesTibble_B <- c(113.45, 221.29, 327.78, 255.98)
theValuesTibble_C <- c(143.45, 251.29, 307.78, 235.98)
theValuesTibble_D <- c(153.45, 231.29, 347.78, 225.98)
theValuesTibble_E <- c(163.45, 291.29, 323.78, 215.98)

theTibble <- tibble(Dates= theDatesTibble, A = theValuesTibble_A, B = theValuesTibble_B, 
                    C = theValuesTibble_C, D = theValuesTibble_D, E = theValuesTibble_E)

weights_1 <- c(2.3, 0.0, 3.6, 12.7, 8.9)
weights_2 <- c(0.0, 0.0, 13.6, 4.7, 0.0)
weights_3 <- c(6.3, 4.4, 8.6, 12.3, 18.9)
require(tidyverse)

DateStible正如@BenoitLondon所提到的,矩阵积是做你想做的事情的最快方法

cbind.data.frame(
  theTibble[1],
  as.matrix(theTibble[-1]) %*% cbind(weights_1, weights_2, weights_3)
)

在我看来像是矩阵产品。。。