title: Columnwise matrix multiplication of matrices stored inside lists in R?
body: <p>I have two large datasets stored in lists (L1, L2) with same dimensions. In each list element L[[.]], there's a data frame.  I want to multiply both lists column-wise, and then some the results for all combinations of L1[[i]] . L2[[j]] for whatever value <em>i</em> and <em>j</em> may take. </p>
  
  <p>For some clarification, see the code below</p>
  
  <pre><code>#Data Generation-----------------------
  set.seed(200)
  a = matrix(sample(50,16), 4)
  b = matrix(sample(50,16), 4)
  c = matrix(sample(50,16), 4)
  d = matrix(sample(50,16), 4)
  
  #Creating index and binding------------
  r = cbind(rep(1,4),a)
  s = cbind(rep(2,4),b)
  o = as.data.frame(rbind(r,s))
  t = cbind(rep(1,4),c)
  u = cbind(rep(2,4),d)
  p = as.data.frame(rbind(t,u))
  
  #Splitting the data -------------------
  list.1 = split(o, o$V1) #o$V1 is the index column
  list.2 = split(p, p$V1) #o$V1 is the index column
  </code></pre>
  
  <p>Now, I want to reproduce in R the Excel function SUMPRODUCT. </p>
  
  <p>So, for instance, from the data above, I want a simple data frame as in:</p>
  
  <pre><code>#Col1      #Col2
  sum(a*c)   sum(b*c)
  sum(a*d)   sum(b*d)
  </code></pre>
  
  <p>The problem is that <em>a</em>,<em>b</em>,<em>c</em>, and <em>d</em> are all inside lists now. I had a go at it with <em>for-loops</em>, but to no avail. I tried to use the <em>split-apply-combine</em> approach, but I wasn't successful either.</p>
  
  <blockquote>
    <p>Since both lists have 9000 elements each, I'm looking for an optimized
    way to do this. How can I accomplish this?</p>
  </blockquote>
  
  <p>Thank you. </p>
  
  <hr>
  
  <p>Edit: Expanding the example for 3 elements in each list as asked by userR</p>
  
  <pre><code>#Data Generation-----------------------
  set.seed(200)
  a = matrix(sample(50,16), 4)
  b = matrix(sample(50,16), 4)
  c = matrix(sample(50,16), 4)
  d = matrix(sample(50,16), 4)
  e = matrix(sample(50,16), 4)
  f = matrix(sample(50,16), 4)
  
  #Creating index and binding------------
  r = cbind(rep(1,4),a)
  s = cbind(rep(2,4),b)
  m = cbind(rep(3,4),c)
  o = as.data.frame(rbind(r,s,m))
  
  t = cbind(rep(1,4),d)
  u = cbind(rep(2,4),e)
  v = cbind(rep(3,4),f)
  p = as.data.frame(rbind(t,u,v))
  
  #Splitting the data -------------------
  list.1 = split(o, o$V1) #o$V1 is the index column
  list.2 = split(p, p$V1) #o$V1 is the index column
  </code></pre>
  
  <p>Now every list has 3 elements each containing a 4x4 matrix.</p>
  
  <p>The result I'm looking for is structured as:</p>
  
  <pre><code>#Col1      #Col2      #Col3
  sum(a*d)   sum(b*d)   sum(c*d)
  sum(a*e)   sum(b*e)   sum(c*e)
  sum(a*f)   sum(b*f)   sum(c*f)
  </code></pre>
  
  <p>For my real problem, the result will be an array (matrix or dataframe) with 9000 columns and 9000 rows.</p>
  
  <hr>
  
  <p>Edit: Adding real data as recommended by userR</p>
  
  <p>useR suggested I showed some real data so people will know how my real data look like: </p>
  
  <p><code>dput(list.1[1:3])</code></p>
  
  <pre><code>list(`1` = structure(list(vol = c(1425.76, 272.52, 0, 0, 31912.78, 
  6056.18, 8212.88, 3909.3, 0, 761.06, 22.45, 237.18), i_1 = c(2819.81, 
  4026.72, 827.2, 4790.52, 12218.1, 3632.64, 6308.66, 4076.71, 
  2192.98, 952.94, 112.84, 170.97), i_2 = c(2857.88, 2914.34, 761.87, 
  4412.4, 11046.36, 2363.24, 7761.31, 5431.03, 1337.62, 857, 103.46, 
  110.33), i_3 = c(1389.12, 932.86, 238.51, 5046, 5298.57, 3087.9, 
  8746.02, 7129.57, 708.53, 549.1, 86.58, 163.15), i_4 = c(1626.96, 
  936.04, 377.81, 4909.62, 6323.5, 2766.49, 3746.06, 2858.07, 900.29, 
  975.21, 102.76, 295.1), i_5 = c(1653.05, 1724.74, 321.59, 3937.2, 
  6966.48, 2614.67, 3326.99, 2371.44, 1082.43, 970.25, 123.51, 
  491.92), i_6 = c(1584.14, 3399.31, 392.24, 3957.88, 8042.5, 2614.46, 
  2371.67, 1896.1, 1201.83, 1314.06, 161.23, 892.91)), row.names = c(NA, 
  12L), class = "data.frame"), `2` = structure(list(vol = c(10774.34, 
  287.53, 0, 0, 57507.79, 10692.91, 9028.38, 10355.78, 8900.38, 
  3253.59, 22.45, 219), i_1 = c(5760.16, 4315.77, 585.28, 2886.11, 
  23767.55, 3095.39, 6705.94, 6445.96, 10612.49, 2470.32, 126.65, 
  143.46), i_2 = c(5035.23, 1785.77, 405.05, 4492.64, 21509.39, 
  3654.16, 10203.03, 9505.1, 6628.42, 1298.06, 111.76, 110.13), 
      i_3 = c(2798.54, 1920.72, 464.92, 7916.61, 13628.15, 8365.88, 
      18425.9, 22368.93, 2253.38, 1078.65, 101.34, 134.98), i_4 = c(2344.65, 
      1407.02, 369.7, 2889.69, 7618.72, 2110.01, 4982.27, 2250.94, 
      1744.4, 1033.89, 105.74, 212.26), i_5 = c(1653.05, 1724.74, 
      321.59, 3937.2, 6966.48, 2614.67, 3326.99, 2371.44, 1082.43, 
      970.25, 123.51, 491.92), i_6 = c(1584.14, 3399.31, 392.24, 
      3957.88, 8042.5, 2614.46, 2371.67, 1896.1, 1201.83, 1314.06, 
      161.23, 892.91)), row.names = 13:24, class = "data.frame"), 
      `3` = structure(list(vol = c(850.15, 218.58, 0, 0, 38959.27, 
      3081.31, 3441.35, 2760.54, 0, 2826.8, 0, 34.12), i_1 = c(6048.28, 
      3545.14, 1566.05, 2866.46, 20149.24, 1459.03, 2051.68, 2047.74, 
      5059.57, 3369.86, 129.37, 361.49), i_2 = c(1728.12, 1530.14, 
      364.37, 4761.97, 6934.24, 1802.96, 5394.15, 3972.96, 510.25, 
      989.3, 109.05, 322.37), i_3 = c(1182.59, 750.55, 311.19, 
      5540.96, 4544.96, 2535.07, 8926.35, 7209.61, 423.66, 446.62, 
      92.21, 282.36), i_4 = c(1179.1, 645.18, 283.97, 4616.66, 
      5063.41, 3110.14, 9240.41, 4752.86, 744.85, 648.44, 100.52, 
      311.67), i_5 = c(1653.05, 1724.74, 321.59, 3937.2, 6966.48, 
      2614.67, 3326.99, 2371.44, 1082.43, 970.25, 123.51, 491.92
      ), i_6 = c(1584.14, 3399.31, 392.24, 3957.88, 8042.5, 2614.46, 
      2371.67, 1896.1, 1201.83, 1314.06, 161.23, 892.91)), row.names = 25:36, class = "data.frame"))
  </code></pre>
  
  <p><code>dput(list.2[1:3])</code></p>
  
  <pre><code>list(`1` = structure(list(Index = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,  1L,
  1L, 1L, 1L, 1L), itenm1 = c(998399, 998399, 998399, 998399,  998399,
  998399, 998399, 998399, 998399, 998399, 998399, 998399 ), j_1 =
  c(-261.62831, -605.82802, -190.35225, -802.27542, -835.07636, 
  -709.70814, -444.26492, -207.96871, -986.93606, -968.29324, -7675.97567, 
  -1271.43424), j_2 = c(0, -188.67302, 0, -799.17034, 0, 247.70379,  0, 0, 1051.71715, -27.94787, 0, -13.34628), j_3 = c(0, 0, 0,  0, 0,
  -207.58736, 0, 0, -2333.43115, -1346.57579, 0, -205.13053 ), j_4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), j_5 = c(0, 0,  0, 0, 0, 0, 0,
  0, 0, 0, 0, 0), j_6 = c(0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0)),
  row.names = c(NA, 12L), class = "data.frame"), 
      `2` = structure(list(Index = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
      2L, 2L, 2L, 2L, 2L), itenm1 = c(998398, 998398, 998398, 998398, 
      998398, 998398, 998398, 998398, 998398, 998398, 998398, 998398
      ), j_1 = c(-106.64606, -203.78915, -76.30121, -310.10454, 
      -321.62536, -227.3462, -160.82221, -70.87354, -286.94001, 
      -137.28382, -3779.42484, -604.71574), j_2 = c(0, -96.94433, 
     0, -297.21757, 0, 67.67053, 0, 0, 309.38773, -8.42931, 0, 
     -6.7299), j_3 = c(0, 0, 0, 0, 0, -56.71107, 0, 0, -686.43453, 
     -406.13843, 0, -103.43761), j_4 = c(0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0), j_5 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0), j_6 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = 13:24, class = "data.frame"), 
     `3` = structure(list(Index = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
     3L, 3L, 3L, 3L, 3L), itenm1 = c(998397, 998397, 998397, 998397, 
     998397, 998397, 998397, 998397, 998397, 998397, 998397, 998397
     ), j_1 = c(-238.10472, -543.97414, -71.04739, -756.58841, 
     -782.7918, -667.84871, -424.38314, -193.82405, -638.12855, 
     -319.65804, -6693.88425, -1189.81911), j_2 = c(0, -182.11783, 
     0, -750.99738, 0, 233.80836, 0, 0, 683.61007, -18.48993, 
     0, -11.46144), j_3 = c(0, 0, 0, 0, 0, -195.94234, 0, 0, -1516.71678, 
     -890.87644, 0, -176.16082), j_4 = c(0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0), j_5 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0), j_6 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = 25:36, class = "data.frame"))
  </code></pre>