我有一个非常大的数据框(数百列和数万行),我想对其进行以下操作:
这样做计算效率最高的方法是什么?
复制简单示例:
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)
答案 0 :(得分:1)
正如@BenoitLondon所提到的,矩阵产品将是你做你想做的最快的方式。
cbind.data.frame(
theTibble[1],
as.matrix(theTibble[-1]) %*% cbind(weights_1, weights_2, weights_3)
)