R - 不同数据框列表的加权平均值
R - Weighted mean over different lists of data frames
我必须计算两个列表的加权平均值。第一个列表如下所示:
april_return list[5] List of lenght 5
April2002 double[4] 0.05 0.01 0.02 0.1
April2003 double[19] 0.06 0.03 0.01 0.3 ...
April2004 double[23] 0.09 0.05 0.03 0.1 ...
April2005 double[22] 0.06 0.07 0.08 0.2 ...
April2006 double[22] 0.02 0.09 0.05 0.6 ...
现在我需要计算 2002 年 4 月、2003 年 4 月......的加权平均值。
第二个列表应该定义权重。第二个列表如下所示
april_MV list[5] List of lenght 5
April2002 double[4] 10 20 25 15
April2003 double[19] 15 20 30 30 ...
April2004 double[23] 20 40 50 20 ...
April2005 double[22] 30 45 60 25 ...
April2006 double[22] 35 30 40 40 ...
根据这两个列表计算 2002 年 4 月的加权平均值应该如下所示
2002 年 4 月的加权平均值 = (10/70)*0.05 + (20/70)*0.01 + (25/70)*0.02 + (15/70)*0.1
现在我正在寻找一种解决方案来计算列表中每个 element/vector 的加权平均值。我无法手动完成,因为我的真实数据/列表不仅是一个列表[5],而且是一个列表[180]
感谢您的帮助:)
重建我的真实数据:
首先列出 april_return 的手段
structure(list(April2002 = c(0.059810295, 0.012052034, -0.040640966,
-0.071444684, 0.00362767, -0.036666412, 0.012681084, -0.003173718,
-0.310532153, 0.044908828, 0.107071823, -0.047645253, -0.000913148,
0.036972821, -0.044169502, 0, -0.04086154, 0.109756157, 0.117437738
), April2003 = c(0.191199036, 0.041629851, 0.135447068, 0.172551366,
0.363662051, 0.126684628, 0.102346456, 0.03394785, 0.093614724,
0.169340872, 0.058748803, 0.257823513, 0.005680838, 0.124511429,
0.367797207, 0.633295158, 0.045312501, 0.086962925, 0.138706685
), April2004 = c(0.070599778, 0.064556381, -0.026714514, -0.021314763,
0.002886201, 0.092696243, -0.044604544, -0.019859405, -0.018196263,
-0.013192787, -0.032356464, -0.0128, 0.094233576, 0.108682686,
0.036889196, 0.028885421, -0.02134008, 0.031550165, 0.089703737,
0.074798598, 0.031329049, 0.009990926, -0.114903475), April2005 = c(0.005993911,
-0.014589752, -0.101264667, 0.006032287, -0.011497624, -0.054151397,
-0.068430071, -0.012466744, -0.005735438, -0.04084729, 0.005404453,
-0.038814751, 0.008856621, -0.049988866, -0.031069367, -0.028684031,
-0.069779686, 0.005284743, 0.087025278, -0.010536234, -0.05975141,
-0.098751295), April2006 = c(0.007108274, -0.033472879, 0.086829945,
0.024163782, -0.028995867, -0.032804048, -0.049975548, -0.017986844,
-0.020345438, -0.003724784, -0.027317881, -0.027046812, 0.043340987,
0.007065344, -0.022421399, 0.040386873, -0.026845924, 0.037131167,
-0.106445824, -0.031818254, -0.019551101, 0.072102052), April2007 = c(0.008594524,
-0.031746693, 0.036520787, 0.024802265, 0.037362498, 0.020322134,
0.018471607, 0.010911075, -0.112041684, -0.030477422, 0.118377396,
-0.008792894, 0.003331673, -0.003875141, 0.005299311, 0.091396673,
0.009480555, 0.070307288, -0.040909482, -0.029332136, 0.001940149,
0.019513168, -0.022626876, 0.056538406, -0.031168514, 0.016699574
), April2008 = c(0.117803042, -0.022050405, 0.059758214, -0.073802662,
0.053297416, 0.018184797, -0.042452618, 0.004482478, 0.014710769,
0.06379017, -0.082262278, 0.103636543, 0.073169977, -0.000862427,
0.032931882, 0.037017156, 0.05970881, 0.055486209, -0.006606342,
0.167493569, 0.188090009, 0.016536658, 0.049824948, 0.089329278,
0.043173581, -0.020675803, 0.030319054, -0.011711492, 0.147917415,
0.153158613, 0.02911807, 0.041131311, 0.123481891), April2009 = c(0.06737771,
0.094634389, 0.098752436, 0.05224699, 0.042600629, 0.194040746,
0.033717875, -0.000987876, -0.006646541, -0.022404587, 0.243615467,
0.180531724, 0.441979293, 0.243149773, 0.411402044, -0.034246426,
0.211650046, 0.039520362, 0.038536671, 0.172222489, 0.180164214,
0.122821456, 0.113802462, 0.125582323, 0.009891332, 0.036137311,
0.187778604, 0.193527824, 0.301568154), April2010 = c(-0.003789351,
0.033925491, -0.005271257, -0.047632335, -0.018408277, -0.013131632,
0.03331033, 0.07109688, 0.028483482, 0.022567201, -0.04306607,
0.166046087, 0.002871973, 0.037502309, 0.083738405, 0.037951787,
0.00791951, -0.014782627, -0.033166221, 0.021600243, -0.007922694,
0.043059582, -0.079609091, 0.020569452, 0.021961908, 0.043419382
), April2011 = c(0.08879903, 0.142561765, 0.082180128, 0.047501462,
0.041829596, -0.006455181, 0.086876462, 0.018588716, 0.009339966,
0.06679837, 0.097520079, 0.07830813, 0.059779282, -0.003624605,
0.116309893, 0.097454376, 0.043455738, 0.02640564, 0.094126081,
0.069487738, 0.009338625, 0.052442242, 0.020600735, 0.026985482,
-0.056236036, -0.007557838, 0.085711299, 0.111495479, 0.016916242,
0.018181084, 0.052596964), April2012 = c(0.006509105, -0.114863903,
0.022787979, 0.00762358, 0.019796118, 0.004707869, -0.077429393,
-0.008732905, -0.092418301, -0.008599666, 0.007802156, 0.03833533,
-0.019318788, -0.038005391, -0.052819767, -0.064146994, -0.050517579,
0.010423533, 0.02025059, -0.052571819, 0.027452729, 0.017110393,
-0.032190199, 0.009877719, -0.007602465, -0.03344712, -0.02457572,
0.045010889, 0.102533291), April2013 = c(0.00890052, -0.018054077,
-0.031055251, 0.093837013, 0.040503154, 0.022103547, -0.020912709,
0.022062186, -0.128430865, 0.057802465, 0.001424035, -0.027154856,
0.05535941, -0.025056449, -0.027048, -0.104656863, 0.087908758,
0.014851506, -0.061549779, 0.037748849, -0.037112316, 0.014978664,
-0.03713202, 0.010456274, -0.044161394, -0.005568825, -0.019470861,
-0.180219357, -0.013742237, -0.026071793, -0.097337903, 0.014299312
), April2014 = c(0.062363776, -0.103262703, 0.122906868, -0.00320478,
0.081024407, 0.055809991, 0.037479665, 0.006509447, 0.010942272,
0.047048594, 0.0142572, -0.002719489, 0.02978357, -0.005598274,
0.043769873, -0.05403823, -0.016188117, -0.007830758, 0.069156644,
-0.042871596, -0.021287004, -0.056807587, 0.023429518, 0.029330781,
0.076197579, 0.018899214, -0.009828368, 0.096036025, 0.040532834,
0.049528432, 0.02343543, 0.188176122, -0.084349028, 0.025998782,
0.073816803, 0.063331908, 0.029911002), April2015 = c(0.018295166,
0.092812652, 0.075589437, 0.011957597, -0.002337694, -0.006477002,
-0.008118181, 0.437959334, 0, 0.042684909, 0.042901335, -0.023003272,
-0.009457721, -0.005050443, 0.129098311, 0.077285269, 0.011016985,
0.045631564, 0.040529887, -0.07698379, -0.037368078, 0.035168751,
-0.034625363, 0.086225862, -0.068743362, -0.050043131, 0.11255604,
-0.030974081, 0.007551137, 0.0241501, -0.013087865, 0.461510179,
0.078845982, -0.086210502, 0.081736295, 0.122281982, -0.003266251
), April2016 = c(0.029399153, -0.020398163, -0.038770186, -0.039389478,
0.026409129, 0.046991742, -0.019341763, -0.029956817, 0.171048037,
-0.014853214, 0.035518337, 0.12111621, 0.042869136, -0.049456696,
0.000982274, -0.055852905, 0.028420732, -0.00798089, -0.033979822,
-0.009905049, 0.016479145, -0.059645853, 0.001065697, -0.039341263,
-0.017200505, -0.042084442, 0.001646077, -0.047752299, -0.007856658,
-0.037444849, -0.083503155, -0.051644346, 0.029739775, 0.018323132,
0.027341037, -0.025394532, -0.023114855, -0.019053574)), .Names = c("April2002",
"April2003", "April2004", "April2005", "April2006", "April2007",
"April2008", "April2009", "April2010", "April2011", "April2012",
"April2013", "April2014", "April2015", "April2016"))
权重的第二个列表april_MV
structure(list(April2002 = c(10654.99, 10817.77, 3199.94, 8917.52,
101990.6, 2986.51, 4954.64, 8108.07, 1419.54, 3404.18, 4099.36,
47585.87, 8499.96, 3062.95, 5217.62, 4523.19, 3626.58, 18586.21,
18283.95), April2003 = c(3886.27, 8832.71, 3255.21, 7073.94,
1691.79, 75396.56, 1447.74, 1814.53, 6388.3, 5035.74, 2912.71,
3470.45, 36463.63, 6311.73, 1478.65, 1573.14, 3736.37, 3871.6,
14327.94), April2004 = c(6418.49, 9257.03, 9320.98, 4816.53,
2430.15, 69136.38, 2047.45, 2443.35, 6603.42, 6081.11, 6349.05,
3712.2, 37606.55, 6673.33, 2501.46, 3882.78, 2311.4, 3415.87,
3920.25, 5344.43, 19104.39, 6561.25, 4006.88), April2005 = c(8194.28,
14344.09, 15771.45, 10879.73, 8213.83, 6175.7, 3433.33, 2162.99,
3036.73, 4961.32, 5157.08, 4846.79, 5102.38, 6514.84, 2533.91,
4073.01, 10202.49, 4809.62, 5039.49, 23979.71, 7133.21, 5705.17
), April2006 = c(13445.95, 11336.95, 10802.33, 2913.63, 8171.86,
4160.39, 3317.23, 3853.98, 4749.32, 4952.19, 10545.22, 6114.19,
72791.44, 6914.38, 3014.82, 8431.66, 9500.6, 5757.04, 4266.03,
25238.59, 8414.65, 12608.36), April2007 = c(14619.87, 7679.12,
13992.47, 14239.12, 4379.16, 2808.65, 7997.34, 4073.76, 4427.79,
4385.92, 4666.95, 5366.6, 6378.22, 10106.15, 8220.97, 9332.64,
6950.35, 8135.26, 3077.26, 8664.71, 3648.61, 10078.84, 5897.32,
36709.95, 9162.48, 25661.71), April2008 = c(2290.62, 16430.56,
41183.07, 4296.87, 12311.77, 10821.13, 3357.75, 2760.09, 2924.32,
60287.87, 2916.28, 6617.41, 2566.82, 7197.37, 3112.71, 7680.74,
7691.31, 17043.61, 5310.85, 71993, 55325.11, 7023.95, 2877.38,
7997.1, 1742.24, 9552.98, 3756.05, 5831.87, 5426.28, 33670.47,
7250.43, 2135.56, 38578.72), April2009 = c(2413.58, 1941.82,
12658.98, 1612.2, 36722.39, 3670.78, 11595.33, 1751.3, 2660.27,
54426.02, 2201.74, 3780.42, 2863.53, 4393.51, 3417.36, 6489.11,
9819.24, 5721.54, 5563.95, 2034.93, 6261.76, 8531.95, 2853.47,
4247.68, 6396.8, 26632.17, 5844.99, 1640.29, 17818.03), April2010 = c(3508.25,
3352.04, 2245.62, 11989.6, 37633.16, 4066.46, 15123.39, 4981.79,
15787.79, 3771.86, 62878.07, 5904.34, 5027.39, 3929.81, 14687.1,
8528.94, 6244.41, 10716.85, 2269.14, 10779.97, 4402.81, 6033,
10171.69, 34779.97, 8758.54, 2707.25), April2011 = c(4548.99,
4906.45, 8348, 2501.25, 11205.34, 51971.23, 5319.08, 16541.95,
4511.59, 4396.91, 67406.88, 3503.82, 3486.87, 7380.95, 6488.97,
7207.83, 7585, 5220.3, 19693.66, 9360.07, 6444.61, 12010.04,
14758.93, 5223.57, 5866.75, 12751.13, 32433.73, 4064.87, 9879.7,
2976.25, 44570.43), April2012 = c(6036.77, 7210.63, 9587.99,
49269.66, 4350.48, 15909.57, 4335.27, 6679.45, 3079.91, 11187.7,
3167.01, 3177.07, 2796.31, 4516.63, 6883.06, 6906.58, 7199.55,
6000.84, 9465.45, 6209.45, 15417.96, 4201.37, 5436.45, 13900.21,
25513.25, 3621.33, 10502.84, 3428.89, 35356.67), April2013 = c(4793.2,
12169.82, 36902.73, 5865.02, 19222.31, 5929.98, 8721.62, 3742.39,
8261.68, 4489.09, 130656.6, 3954.5, 3271.19, 5331.37, 4925.7,
1813.64, 7428.75, 10023.52, 3091.83, 10031.35, 4669.53, 9572.77,
8949.92, 21246.54, 13602.74, 4919.16, 6659.96, 9089.78, 29528.31,
4696.08, 2892.11, 13471.25), April2014 = c(4243.49, 12560.86,
15489.49, 12655.21, 40851.38, 6963.84, 16657.42, 7170.44, 9094.75,
4230.21, 6274.64, 6298.98, 4067.11, 115201.8, 4070.92, 4689.17,
7326.24, 9928.39, 12533.35, 3608.14, 3059.88, 4691.14, 9781.41,
5026.11, 9083.21, 3928.2, 19743.5, 4969.52, 9136.76, 5265.53,
8224.47, 8010.02, 4210.16, 23724.82, 5736.96, 3092.61, 16976.55
), April2015 = c(4348.5, 4228.51, 7704.47, 15723.86, 21319.66,
16072.06, 5153.51, 40381.64, 8495.94, 12704.95, 7596.71, 10069.4,
4546.17, 5789.02, 126465, 30550.32, 4516.84, 4214.77, 10208.43,
8208.35, 15450.43, 4858.59, 4361.95, 11571.27, 10827.8, 12175.27,
19248.93, 4664.2, 9213.36, 4520.2, 10008.14, 3770.33, 5170.72,
17951.83, 5407.25, 4011.56, 19964.53), April2016 = c(4763.66,
17545.8, 15143.15, 5337.21, 7400.66, 12088.04, 6671.73, 9679.34,
8201.62, 4777.19, 23449.02, 5276.05, 89646.56, 7761.7, 4091.92,
4192.44, 5256.37, 9065.22, 8287.63, 13272.3, 47870.36, 4375.02,
4803.5, 9142.63, 4301.43, 6614.13, 46839.6, 13306.76, 12310.16,
4562.01, 16132.97, 6441.46, 4377.91, 10354.1, 4618.98, 6016.56,
39203.25, 20657.03)), .Names = c("April2002", "April2003", "April2004",
"April2005", "April2006", "April2007", "April2008", "April2009",
"April2010", "April2011", "April2012", "April2013", "April2014",
"April2015", "April2016"))
这可能是 purrr
的一个好案例:
library(purrr)
weightings <- map(april_MV, ~ .x / sum(.x))
map2(april_return, weightings, ~ .x * .y)
或者,您可以将它们放入 data_frame
对象并从那里进行转换:
library(tidyverse)
df_returns <- data_frame(april_return)
df_weights <- data_frame(april_MV)
bind_cols(df_returns, df_weights) %>%
unnest(.id = "ID") %>%
group_by(ID) %>%
mutate(weighting = april_MV / sum(april_MV),
wtd_mean = april_return * weighting)
我必须计算两个列表的加权平均值。第一个列表如下所示:
april_return list[5] List of lenght 5
April2002 double[4] 0.05 0.01 0.02 0.1
April2003 double[19] 0.06 0.03 0.01 0.3 ...
April2004 double[23] 0.09 0.05 0.03 0.1 ...
April2005 double[22] 0.06 0.07 0.08 0.2 ...
April2006 double[22] 0.02 0.09 0.05 0.6 ...
现在我需要计算 2002 年 4 月、2003 年 4 月......的加权平均值。 第二个列表应该定义权重。第二个列表如下所示
april_MV list[5] List of lenght 5
April2002 double[4] 10 20 25 15
April2003 double[19] 15 20 30 30 ...
April2004 double[23] 20 40 50 20 ...
April2005 double[22] 30 45 60 25 ...
April2006 double[22] 35 30 40 40 ...
根据这两个列表计算 2002 年 4 月的加权平均值应该如下所示
2002 年 4 月的加权平均值 = (10/70)*0.05 + (20/70)*0.01 + (25/70)*0.02 + (15/70)*0.1
现在我正在寻找一种解决方案来计算列表中每个 element/vector 的加权平均值。我无法手动完成,因为我的真实数据/列表不仅是一个列表[5],而且是一个列表[180]
感谢您的帮助:)
重建我的真实数据: 首先列出 april_return 的手段
structure(list(April2002 = c(0.059810295, 0.012052034, -0.040640966,
-0.071444684, 0.00362767, -0.036666412, 0.012681084, -0.003173718,
-0.310532153, 0.044908828, 0.107071823, -0.047645253, -0.000913148,
0.036972821, -0.044169502, 0, -0.04086154, 0.109756157, 0.117437738
), April2003 = c(0.191199036, 0.041629851, 0.135447068, 0.172551366,
0.363662051, 0.126684628, 0.102346456, 0.03394785, 0.093614724,
0.169340872, 0.058748803, 0.257823513, 0.005680838, 0.124511429,
0.367797207, 0.633295158, 0.045312501, 0.086962925, 0.138706685
), April2004 = c(0.070599778, 0.064556381, -0.026714514, -0.021314763,
0.002886201, 0.092696243, -0.044604544, -0.019859405, -0.018196263,
-0.013192787, -0.032356464, -0.0128, 0.094233576, 0.108682686,
0.036889196, 0.028885421, -0.02134008, 0.031550165, 0.089703737,
0.074798598, 0.031329049, 0.009990926, -0.114903475), April2005 = c(0.005993911,
-0.014589752, -0.101264667, 0.006032287, -0.011497624, -0.054151397,
-0.068430071, -0.012466744, -0.005735438, -0.04084729, 0.005404453,
-0.038814751, 0.008856621, -0.049988866, -0.031069367, -0.028684031,
-0.069779686, 0.005284743, 0.087025278, -0.010536234, -0.05975141,
-0.098751295), April2006 = c(0.007108274, -0.033472879, 0.086829945,
0.024163782, -0.028995867, -0.032804048, -0.049975548, -0.017986844,
-0.020345438, -0.003724784, -0.027317881, -0.027046812, 0.043340987,
0.007065344, -0.022421399, 0.040386873, -0.026845924, 0.037131167,
-0.106445824, -0.031818254, -0.019551101, 0.072102052), April2007 = c(0.008594524,
-0.031746693, 0.036520787, 0.024802265, 0.037362498, 0.020322134,
0.018471607, 0.010911075, -0.112041684, -0.030477422, 0.118377396,
-0.008792894, 0.003331673, -0.003875141, 0.005299311, 0.091396673,
0.009480555, 0.070307288, -0.040909482, -0.029332136, 0.001940149,
0.019513168, -0.022626876, 0.056538406, -0.031168514, 0.016699574
), April2008 = c(0.117803042, -0.022050405, 0.059758214, -0.073802662,
0.053297416, 0.018184797, -0.042452618, 0.004482478, 0.014710769,
0.06379017, -0.082262278, 0.103636543, 0.073169977, -0.000862427,
0.032931882, 0.037017156, 0.05970881, 0.055486209, -0.006606342,
0.167493569, 0.188090009, 0.016536658, 0.049824948, 0.089329278,
0.043173581, -0.020675803, 0.030319054, -0.011711492, 0.147917415,
0.153158613, 0.02911807, 0.041131311, 0.123481891), April2009 = c(0.06737771,
0.094634389, 0.098752436, 0.05224699, 0.042600629, 0.194040746,
0.033717875, -0.000987876, -0.006646541, -0.022404587, 0.243615467,
0.180531724, 0.441979293, 0.243149773, 0.411402044, -0.034246426,
0.211650046, 0.039520362, 0.038536671, 0.172222489, 0.180164214,
0.122821456, 0.113802462, 0.125582323, 0.009891332, 0.036137311,
0.187778604, 0.193527824, 0.301568154), April2010 = c(-0.003789351,
0.033925491, -0.005271257, -0.047632335, -0.018408277, -0.013131632,
0.03331033, 0.07109688, 0.028483482, 0.022567201, -0.04306607,
0.166046087, 0.002871973, 0.037502309, 0.083738405, 0.037951787,
0.00791951, -0.014782627, -0.033166221, 0.021600243, -0.007922694,
0.043059582, -0.079609091, 0.020569452, 0.021961908, 0.043419382
), April2011 = c(0.08879903, 0.142561765, 0.082180128, 0.047501462,
0.041829596, -0.006455181, 0.086876462, 0.018588716, 0.009339966,
0.06679837, 0.097520079, 0.07830813, 0.059779282, -0.003624605,
0.116309893, 0.097454376, 0.043455738, 0.02640564, 0.094126081,
0.069487738, 0.009338625, 0.052442242, 0.020600735, 0.026985482,
-0.056236036, -0.007557838, 0.085711299, 0.111495479, 0.016916242,
0.018181084, 0.052596964), April2012 = c(0.006509105, -0.114863903,
0.022787979, 0.00762358, 0.019796118, 0.004707869, -0.077429393,
-0.008732905, -0.092418301, -0.008599666, 0.007802156, 0.03833533,
-0.019318788, -0.038005391, -0.052819767, -0.064146994, -0.050517579,
0.010423533, 0.02025059, -0.052571819, 0.027452729, 0.017110393,
-0.032190199, 0.009877719, -0.007602465, -0.03344712, -0.02457572,
0.045010889, 0.102533291), April2013 = c(0.00890052, -0.018054077,
-0.031055251, 0.093837013, 0.040503154, 0.022103547, -0.020912709,
0.022062186, -0.128430865, 0.057802465, 0.001424035, -0.027154856,
0.05535941, -0.025056449, -0.027048, -0.104656863, 0.087908758,
0.014851506, -0.061549779, 0.037748849, -0.037112316, 0.014978664,
-0.03713202, 0.010456274, -0.044161394, -0.005568825, -0.019470861,
-0.180219357, -0.013742237, -0.026071793, -0.097337903, 0.014299312
), April2014 = c(0.062363776, -0.103262703, 0.122906868, -0.00320478,
0.081024407, 0.055809991, 0.037479665, 0.006509447, 0.010942272,
0.047048594, 0.0142572, -0.002719489, 0.02978357, -0.005598274,
0.043769873, -0.05403823, -0.016188117, -0.007830758, 0.069156644,
-0.042871596, -0.021287004, -0.056807587, 0.023429518, 0.029330781,
0.076197579, 0.018899214, -0.009828368, 0.096036025, 0.040532834,
0.049528432, 0.02343543, 0.188176122, -0.084349028, 0.025998782,
0.073816803, 0.063331908, 0.029911002), April2015 = c(0.018295166,
0.092812652, 0.075589437, 0.011957597, -0.002337694, -0.006477002,
-0.008118181, 0.437959334, 0, 0.042684909, 0.042901335, -0.023003272,
-0.009457721, -0.005050443, 0.129098311, 0.077285269, 0.011016985,
0.045631564, 0.040529887, -0.07698379, -0.037368078, 0.035168751,
-0.034625363, 0.086225862, -0.068743362, -0.050043131, 0.11255604,
-0.030974081, 0.007551137, 0.0241501, -0.013087865, 0.461510179,
0.078845982, -0.086210502, 0.081736295, 0.122281982, -0.003266251
), April2016 = c(0.029399153, -0.020398163, -0.038770186, -0.039389478,
0.026409129, 0.046991742, -0.019341763, -0.029956817, 0.171048037,
-0.014853214, 0.035518337, 0.12111621, 0.042869136, -0.049456696,
0.000982274, -0.055852905, 0.028420732, -0.00798089, -0.033979822,
-0.009905049, 0.016479145, -0.059645853, 0.001065697, -0.039341263,
-0.017200505, -0.042084442, 0.001646077, -0.047752299, -0.007856658,
-0.037444849, -0.083503155, -0.051644346, 0.029739775, 0.018323132,
0.027341037, -0.025394532, -0.023114855, -0.019053574)), .Names = c("April2002",
"April2003", "April2004", "April2005", "April2006", "April2007",
"April2008", "April2009", "April2010", "April2011", "April2012",
"April2013", "April2014", "April2015", "April2016"))
权重的第二个列表april_MV
structure(list(April2002 = c(10654.99, 10817.77, 3199.94, 8917.52,
101990.6, 2986.51, 4954.64, 8108.07, 1419.54, 3404.18, 4099.36,
47585.87, 8499.96, 3062.95, 5217.62, 4523.19, 3626.58, 18586.21,
18283.95), April2003 = c(3886.27, 8832.71, 3255.21, 7073.94,
1691.79, 75396.56, 1447.74, 1814.53, 6388.3, 5035.74, 2912.71,
3470.45, 36463.63, 6311.73, 1478.65, 1573.14, 3736.37, 3871.6,
14327.94), April2004 = c(6418.49, 9257.03, 9320.98, 4816.53,
2430.15, 69136.38, 2047.45, 2443.35, 6603.42, 6081.11, 6349.05,
3712.2, 37606.55, 6673.33, 2501.46, 3882.78, 2311.4, 3415.87,
3920.25, 5344.43, 19104.39, 6561.25, 4006.88), April2005 = c(8194.28,
14344.09, 15771.45, 10879.73, 8213.83, 6175.7, 3433.33, 2162.99,
3036.73, 4961.32, 5157.08, 4846.79, 5102.38, 6514.84, 2533.91,
4073.01, 10202.49, 4809.62, 5039.49, 23979.71, 7133.21, 5705.17
), April2006 = c(13445.95, 11336.95, 10802.33, 2913.63, 8171.86,
4160.39, 3317.23, 3853.98, 4749.32, 4952.19, 10545.22, 6114.19,
72791.44, 6914.38, 3014.82, 8431.66, 9500.6, 5757.04, 4266.03,
25238.59, 8414.65, 12608.36), April2007 = c(14619.87, 7679.12,
13992.47, 14239.12, 4379.16, 2808.65, 7997.34, 4073.76, 4427.79,
4385.92, 4666.95, 5366.6, 6378.22, 10106.15, 8220.97, 9332.64,
6950.35, 8135.26, 3077.26, 8664.71, 3648.61, 10078.84, 5897.32,
36709.95, 9162.48, 25661.71), April2008 = c(2290.62, 16430.56,
41183.07, 4296.87, 12311.77, 10821.13, 3357.75, 2760.09, 2924.32,
60287.87, 2916.28, 6617.41, 2566.82, 7197.37, 3112.71, 7680.74,
7691.31, 17043.61, 5310.85, 71993, 55325.11, 7023.95, 2877.38,
7997.1, 1742.24, 9552.98, 3756.05, 5831.87, 5426.28, 33670.47,
7250.43, 2135.56, 38578.72), April2009 = c(2413.58, 1941.82,
12658.98, 1612.2, 36722.39, 3670.78, 11595.33, 1751.3, 2660.27,
54426.02, 2201.74, 3780.42, 2863.53, 4393.51, 3417.36, 6489.11,
9819.24, 5721.54, 5563.95, 2034.93, 6261.76, 8531.95, 2853.47,
4247.68, 6396.8, 26632.17, 5844.99, 1640.29, 17818.03), April2010 = c(3508.25,
3352.04, 2245.62, 11989.6, 37633.16, 4066.46, 15123.39, 4981.79,
15787.79, 3771.86, 62878.07, 5904.34, 5027.39, 3929.81, 14687.1,
8528.94, 6244.41, 10716.85, 2269.14, 10779.97, 4402.81, 6033,
10171.69, 34779.97, 8758.54, 2707.25), April2011 = c(4548.99,
4906.45, 8348, 2501.25, 11205.34, 51971.23, 5319.08, 16541.95,
4511.59, 4396.91, 67406.88, 3503.82, 3486.87, 7380.95, 6488.97,
7207.83, 7585, 5220.3, 19693.66, 9360.07, 6444.61, 12010.04,
14758.93, 5223.57, 5866.75, 12751.13, 32433.73, 4064.87, 9879.7,
2976.25, 44570.43), April2012 = c(6036.77, 7210.63, 9587.99,
49269.66, 4350.48, 15909.57, 4335.27, 6679.45, 3079.91, 11187.7,
3167.01, 3177.07, 2796.31, 4516.63, 6883.06, 6906.58, 7199.55,
6000.84, 9465.45, 6209.45, 15417.96, 4201.37, 5436.45, 13900.21,
25513.25, 3621.33, 10502.84, 3428.89, 35356.67), April2013 = c(4793.2,
12169.82, 36902.73, 5865.02, 19222.31, 5929.98, 8721.62, 3742.39,
8261.68, 4489.09, 130656.6, 3954.5, 3271.19, 5331.37, 4925.7,
1813.64, 7428.75, 10023.52, 3091.83, 10031.35, 4669.53, 9572.77,
8949.92, 21246.54, 13602.74, 4919.16, 6659.96, 9089.78, 29528.31,
4696.08, 2892.11, 13471.25), April2014 = c(4243.49, 12560.86,
15489.49, 12655.21, 40851.38, 6963.84, 16657.42, 7170.44, 9094.75,
4230.21, 6274.64, 6298.98, 4067.11, 115201.8, 4070.92, 4689.17,
7326.24, 9928.39, 12533.35, 3608.14, 3059.88, 4691.14, 9781.41,
5026.11, 9083.21, 3928.2, 19743.5, 4969.52, 9136.76, 5265.53,
8224.47, 8010.02, 4210.16, 23724.82, 5736.96, 3092.61, 16976.55
), April2015 = c(4348.5, 4228.51, 7704.47, 15723.86, 21319.66,
16072.06, 5153.51, 40381.64, 8495.94, 12704.95, 7596.71, 10069.4,
4546.17, 5789.02, 126465, 30550.32, 4516.84, 4214.77, 10208.43,
8208.35, 15450.43, 4858.59, 4361.95, 11571.27, 10827.8, 12175.27,
19248.93, 4664.2, 9213.36, 4520.2, 10008.14, 3770.33, 5170.72,
17951.83, 5407.25, 4011.56, 19964.53), April2016 = c(4763.66,
17545.8, 15143.15, 5337.21, 7400.66, 12088.04, 6671.73, 9679.34,
8201.62, 4777.19, 23449.02, 5276.05, 89646.56, 7761.7, 4091.92,
4192.44, 5256.37, 9065.22, 8287.63, 13272.3, 47870.36, 4375.02,
4803.5, 9142.63, 4301.43, 6614.13, 46839.6, 13306.76, 12310.16,
4562.01, 16132.97, 6441.46, 4377.91, 10354.1, 4618.98, 6016.56,
39203.25, 20657.03)), .Names = c("April2002", "April2003", "April2004",
"April2005", "April2006", "April2007", "April2008", "April2009",
"April2010", "April2011", "April2012", "April2013", "April2014",
"April2015", "April2016"))
这可能是 purrr
的一个好案例:
library(purrr)
weightings <- map(april_MV, ~ .x / sum(.x))
map2(april_return, weightings, ~ .x * .y)
或者,您可以将它们放入 data_frame
对象并从那里进行转换:
library(tidyverse)
df_returns <- data_frame(april_return)
df_weights <- data_frame(april_MV)
bind_cols(df_returns, df_weights) %>%
unnest(.id = "ID") %>%
group_by(ID) %>%
mutate(weighting = april_MV / sum(april_MV),
wtd_mean = april_return * weighting)