每年为 R 中的多家公司计算 returns

Calculate yearly returns for multiple firms in R

我有一个跨多年期多家公司股票价格的数据集。下面包含的示例仅包含两家公司约 2 年的每日数据。

structure(list(NATIONAL.AUS.BANK = c(6.0588, 6.0588, 6.0402, 
6.0402, 6.003, 5.9658, 5.8543, 5.7242, 5.6313, 5.4826, 5.4269, 
5.2968, 5.4269, 5.4455, 5.3711, 5.6685, 5.5384, 5.5012, 5.4826, 
5.557, 5.6127, 5.5941, 5.5198, 5.5198, 5.557, 5.6685, 5.7614, 
5.8543, 5.8729, 5.8172, 5.8543, 5.7614, 5.6313, 5.4455, 5.4826, 
5.3525, 5.3525, 5.3154, 5.1667, 5.3154, 5.2596, 5.3897, 5.5756, 
5.5012, 5.5198, 5.557, 5.5198, 5.5012, 5.4826, 5.5198, 5.5941, 
5.6685, 5.6499, 5.6313, 5.5941, 5.5941, 5.5756, 5.4269, 5.3339, 
5.2782, 5.2782, 5.3339, 5.3339, 5.4455, 5.3897, 5.3339, 5.3154, 
5.3154, 5.2968, 5.241, 5.1667, 5.1481, 4.7578, 4.7206, 4.7206, 
4.7206, 4.7206, 4.7578, 4.8693, 4.8693, 4.8693, 4.9808, 4.8693, 
4.6463, 4.7392, 4.795, 4.795, 4.8879, 4.9808, 4.9065, 4.8136, 
4.7578, 4.9994, 5.018, 5.0552, 5.0923, 5.1481, 5.1481, 5.2968, 
5.2968, 5.3154, 5.4269, 5.4455, 5.2968, 5.4083, 5.5756, 5.5756, 
5.5012, 5.5012, 5.6685, 5.7614, 5.7056, 5.6685, 5.7614, 5.8915, 
5.8915, 5.7428, 5.78, 5.8729, 5.8729, 5.8915, 5.7614, 5.8172, 
5.5756, 5.557, 5.3711, 5.464, 5.4269, 5.4269, 5.5198, 5.5384, 
5.557, 5.5941, 5.5941, 5.6401, 5.6401, 5.5832, 5.5262, 5.5452, 
5.5072, 5.5642, 5.5452, 5.5642, 5.5642, 5.5642, 5.5452, 5.5642, 
5.5452, 5.5452, 5.4882, 5.5262, 5.5262, 5.5642, 5.5642, 5.5642, 
5.6021, 5.6591, 5.7731, 5.944, 5.887, 5.849, 5.868, 5.868, 5.963, 
5.944, 5.887, 5.963, 5.83, 5.83, 5.792, 5.811, 5.83, 5.83, 5.982, 
5.963, 5.982, 6.0389, 6.2478, 6.3618, 6.2668, 6.2668, 6.3048, 
6.2858, 6.2858, 6.3048, 6.2858, 6.2668, 6.3238, 6.3238, 6.3997, 
6.3428, 6.3428, 6.3238, 6.1908, 6.1908, 6.2478, 6.3428, 6.2478, 
6.3048, 6.1149, 6.1719, 6.1908, 6.1908, 6.1908, 6.1719, 6.1908, 
6.2858, 6.2858, 6.3618, 6.4567, 6.5517, 6.4947, 6.4187, 6.4377, 
6.3618, 6.4377, 6.4377, 6.4377, 6.4187, 6.3997, 6.3997, 6.4187, 
6.3997, 6.4377, 6.4947, 6.4947, 6.5137, 6.5327, 6.5896, 6.6466, 
6.6466, 6.6276, 6.6276, 6.7416, 6.7416, 6.7606, 6.8555, 6.8555, 
6.7985, 6.7226, 6.6276, 6.6656, 6.7226, 6.7985, 6.4187, 6.5896, 
6.6276, 6.7606, 6.6846, 6.6656, 6.7036, 6.6276, 6.6086, 6.5896, 
6.6466, 6.8935, 6.8365, 6.7985, 6.8365, 6.8175, 6.8555, 6.8935, 
6.9315, 6.8935, 6.7795, 6.7226, 6.7416, 6.8745, 6.8555, 6.8365, 
6.7795, 6.8365, 6.8175, 6.8175, 6.8745, 7.0454, 7.0264, 7.0264, 
7.1024, 7.0454, 7.0264, 7.0834, 7.0454, 6.9884, 7.0454, 7.1404, 
7.2353, 7.2923, 7.3872, 7.4062, 7.4252, 7.4062, 7.3872, 7.3113, 
7.3303, 7.4062, 7.4822, 7.5961, 7.6056, 7.6531, 7.7575, 7.7101, 
7.6721, 7.7196, 7.6816, 7.5771, 7.5297, 7.5581, 7.6341, 7.5012, 
7.5486, 7.4537, 7.5961, 7.5771, 7.4062, 7.4062, 7.4062, 7.3967, 
7.4062, 7.3777, 7.4062, 7.4062, 7.5012, 7.4822, 7.5486, 7.5297, 
7.5012, 7.5961, 7.7386, 7.805, 7.805, 7.748, 7.748, 7.7575, 7.6246, 
7.4442, 7.4442, 7.4442, 7.4442, 7.4632, 7.5771, 7.5771, 7.5581, 
7.5866, 7.6721, 7.6911, 7.7196, 7.767, 7.767, 7.6911, 7.6531, 
7.7196, 7.7196, 7.6816, 7.7196, 7.5961, 7.4727, 7.4157, 7.2163, 
7.2163, 7.2828, 7.2353, 7.1499, 7.2163, 7.1688, 7.0834, 7.1024, 
7.1214, 7.1214, 7.1594, 7.1973, 7.0929, 7.1024, 7.0454, 7.0739, 
7.0929, 7.0929, 7.0549, 7.0644, 7.0834, 7.1973, 7.2163, 7.1594, 
7.0929, 7.1973, 7.1309, 7.1024, 7.0929, 7.0644, 7.0739, 7.0929, 
7.0644, 6.9789, 6.9789, 6.9315, 6.884, 6.884, 6.9505, 6.9884, 
7.0264, 7.0454, 7.0169, 6.9979, 7.0454, 7.0264, 6.9884, 6.9315, 
6.8365, 6.8365, 6.8555, 6.8745, 6.8175, 6.8175, 6.9315, 6.8745, 
6.8365, 6.808, 6.789, 6.789, 6.789, 6.789, 6.827, 6.9315, 7.0739, 
6.9979, 7.0929, 7.1973, 7.2923, 7.3682, 7.3967, 7.3967, 7.4442, 
7.3872, 7.3587, 7.3113, 7.3777, 7.4062, 7.4537, 7.5202, 7.6436, 
7.5486, 7.5297, 7.5676, 7.5771, 7.5771, 7.5581, 7.5297, 7.5297, 
7.5961, 7.5771, 7.5866, 7.5676, 7.5392, 7.5297, 7.5297, 7.5676, 
7.5392, 7.2828, 7.2828, 7.2543, 7.2543, 7.1688, 7.1404, 7.2068, 
7.2163, 7.2353, 7.2638, 7.2733, 7.2163, 7.2638, 7.3208, 7.3872, 
7.4917, 7.5297, 7.5392, 7.5581, 7.5486, 7.5486, 7.5297, 7.5107, 
7.5392, 7.5581, 7.5676, 7.5012, 7.3967, 7.4727, 7.5012, 7.4252, 
7.4347, 7.3967, 7.4062, 7.4917, 7.5961, 7.5961, 7.5771, 7.5676, 
7.5392, 7.4632, 7.4062, 7.4537, 7.3872, 7.2543, 7.2163, 7.1309, 
7.1499, 7.1404, 7.1404, 7.0834, 7.0549, 6.9694, 6.8555, 6.96, 
7.0739, 7.1119, 7.0644, 7.1119, 7.0169, 7.0074, 6.9694, 6.8365, 
6.8365, 6.77, 6.789, 6.8365, 7.0264, 7.0644, 6.96, 6.9884, 6.9979, 
7.0169, 6.96, 6.865, 6.922, 6.8555, 6.865, 6.7985, 6.8745, 6.8745, 
6.884, 6.808, 6.808, 6.8555, 6.9125, 6.9315, 6.9315, 6.941, 6.941, 
6.903, 6.8365, 6.8175, 6.7795, 6.789, 6.789, 6.7606, 6.5991, 
6.5896, 6.5232, 6.5612, 6.5232, 6.4757, 6.4852, 6.6466, 6.6941, 
6.6846, 6.6846, 6.6276, 6.5517, 6.4092, 6.4282, 6.4852, 6.5327, 
6.5517, 6.6466, 6.6751, 6.8555, 6.9315, 6.9979, 6.903, 6.8935, 
6.8935, 6.8935, 6.7985, 6.903, 6.9979, 7.0454, 7.1309, 7.1783, 
7.2543, 7.1688, 7.3113, 7.3113, 7.1499, 7.1973, 6.9789, 7.1214, 
6.9789, 7.0264, 7.1214, 7.1214, 7.1214, 7.1309, 7.1404, 7.2163, 
7.2163, 7.3018, 7.3493, 7.3493, 7.2923, 7.1878, 7.1688, 7.0739, 
7.0074, 7.1688, 7.2163, 7.2163, 7.1783, 7.1688, 7.1404, 7.1119, 
7.1404, 7.1309, 7.1499, 7.1309, 7.1309, 7.1214, 7.1214, 7.1024, 
7.1878, 7.2638, 7.3493, 7.4822, 7.5012, 7.5961, 7.5771, 7.5771, 
7.5866, 7.5107, 7.5012, 7.3682, 7.5486, 7.5961, 7.5961, 7.6911, 
7.7386, 7.9759, 8.0329, 7.9759, 8.0234, 7.938, 7.9, 8.0234, 8.1468, 
8.2038, 8.1184, 8.0424, 8.1753, 8.3368, 8.3178, 8.3842, 8.2798, 
8.3273, 8.3747, 8.4032, 8.4887, 8.4697, 8.3747, 8.3178, 8.2988, 
8.3937, 8.3368, 8.2608, 8.3083, 8.4032, 8.4032, 8.4032, 8.5931, 
8.7545, 8.7166, 8.7071, 8.6121, 8.6406, 8.5931, 8.6406, 8.6121, 
8.5646, 8.5456, 8.5267, 8.5172, 8.5646, 8.5741, 8.5931, 8.6786, 
8.8875, 8.9254, 8.8305, 8.8305, 8.7166, 8.7071, 8.5931, 8.5646, 
8.6406, 8.7545, 8.9824, 9.2388, 9.2008, 9.3527, 9.4287, 9.5711, 
9.4667, 9.3622, 9.2103, 9.1913, 9.1818, 9.1154, 9.1628, 9.2008, 
8.9539, 9.1154, 9.2103, 9.2103, 9.4192, 9.2578, 9.3053, 9.3812, 
9.3337, 9.2293, 9.1343, 9.1248, 9.1438, 9.2103, 9.2863, 9.4477, 
9.5711, 9.5901, 9.5521, 9.5521, 9.5521, 9.5331, 9.4762, 9.5142, 
9.5521, 9.799, 9.761, 9.78, 9.6471, 9.6661, 9.837, 9.78, 9.837, 
9.761, 9.742, 9.723, 9.78, 9.837, 9.78, 9.837, 9.799, 9.875, 
9.856, 9.837, 10.0079, 10.1218, 10.3117, 10.3497, 10.4067, 10.6726, 
10.5776, 10.7295, 10.6916, 10.6346, 10.5206, 10.5396, 10.6346, 
10.6916, 10.8055, 10.9764, 11.1473, 11.0144, 10.7485, 10.7485, 
10.9384, 10.7675, 10.7295, 10.6536, 10.6726, 10.6536, 10.6156, 
10.5966, 10.7295, 10.7675, 10.8055, 10.9194, 11.0714, 11.2233, 
11.2233, 11.2613, 11.2992, 11.4702, 11.5271, 11.4891, 11.6221, 
11.9639, 11.869, 12.1158, 12.1538, 12.0019, 12.0019, 12.1348, 
12.1348, 12.1158, 11.869, 11.736, 11.831, 11.755, 11.6411, 11.679, 
11.774, 11.85, 12.0209, 12.1158, 12.0399, 11.869, 11.9829, 11.9639, 
11.869, 11.774, 11.5841, 11.5841, 11.717, 11.9829, 11.8879, 12.2108, 
12.2298, 12.1158, 11.736, 11.3942, 11.4891, 11.5841, 11.5841, 
11.3752, 11.3942, 11.3182, 11.4512, 11.4322, 11.4891, 11.679, 
11.6221, 11.6601, 11.698, 11.6601, 11.6221, 11.4512, 11.5081, 
11.4512, 11.1093, 11.1093, 11.0903, 11.0334, 11.0903, 11.0903, 
11.0903, 11.4322, 11.6411, 11.717, 11.717, 11.755, 11.774, 11.869, 
11.6601, 11.736, 11.793, 11.5841, 11.4891, 11.4891, 11.755, 11.698, 
11.85, 11.831, 11.6601, 11.6031, 11.736, 11.736, 11.793, 11.9069, 
12.2298, 12.2108, 12.2108, 12.3247, 12.1918, 12.0209, 11.9259, 
11.679, 11.5651, 11.5271, 11.5841, 11.6031, 11.679, 11.4891, 
11.4132, 11.3372), AUS.AND.NZ.BANKING.GP. = c(4.2255, 4.2632, 
4.2066, 4.1877, 4.1028, 4.0934, 4.0557, 4.0368, 3.9425, 3.9142, 
3.8293, 3.7727, 3.8388, 3.7916, 3.8293, 3.9236, 3.801, 3.7067, 
3.6784, 3.7727, 3.7916, 3.7633, 3.735, 3.7539, 3.735, 3.8293, 
3.8671, 3.9236, 3.9236, 3.9142, 3.9614, 3.8953, 3.8765, 3.7444, 
3.7444, 3.669, 3.6313, 3.5747, 3.5558, 3.6313, 3.6218, 3.6878, 
3.7727, 3.6313, 3.6596, 3.6407, 3.5558, 3.5086, 3.6218, 3.669, 
3.7539, 3.7161, 3.6313, 3.6218, 3.5464, 3.5558, 3.4803, 3.2728, 
3.2068, 3.2823, 3.254, 3.2823, 3.2823, 3.4143, 3.2634, 3.2728, 
3.3011, 3.3389, 3.32, 3.2351, 3.2446, 3.2351, 3.1785, 3.1408, 
3.1314, 3.1314, 3.1314, 3.2068, 3.0936, 3.0936, 3.0936, 3.1125, 
3.0465, 2.8956, 2.8296, 2.8296, 2.8578, 2.8673, 2.9427, 2.9239, 
2.8767, 2.8673, 3.0182, 3.1031, 3.1125, 3.1125, 3.1031, 3.1125, 
3.1785, 3.1785, 3.2068, 3.2351, 3.2446, 3.0748, 3.0842, 3.2163, 
3.1691, 3.0748, 3.0465, 3.1974, 3.2917, 3.2823, 3.2917, 3.3011, 
3.3766, 3.3577, 3.2351, 3.2728, 3.2917, 3.3672, 3.3672, 3.3106, 
3.3672, 3.188, 3.1125, 3.0559, 3.0559, 2.9805, 2.971, 3.0182, 
3.0465, 3.0465, 3.0653, 3.1597, 3.2446, 3.1785, 3.0936, 3.0748, 
3.1691, 3.1597, 3.1597, 3.1974, 3.1785, 3.1785, 3.1785, 3.1597, 
3.1691, 3.1314, 3.1219, 3.0842, 3.0559, 3.0276, 3.0842, 3.0842, 
3.1502, 3.2446, 3.3011, 3.4521, 3.5464, 3.4992, 3.4709, 3.5464, 
3.5464, 3.6784, 3.7539, 3.6784, 3.6501, 3.6124, 3.603, 3.5558, 
3.6407, 3.6218, 3.6407, 3.6784, 3.6501, 3.6313, 3.6407, 3.7633, 
3.6784, 3.603, 3.6313, 3.4992, 3.4143, 3.5275, 3.5369, 3.4709, 
3.4615, 3.4615, 3.3955, 3.3672, 3.3294, 3.2823, 3.254, 3.188, 
3.188, 3.2257, 3.2917, 3.3106, 3.3672, 3.3955, 3.4143, 3.4238, 
3.3955, 3.4803, 3.4709, 3.5275, 3.5558, 3.4709, 3.4238, 3.5369, 
3.6878, 3.6124, 3.6218, 3.5935, 3.5558, 3.603, 3.6313, 3.603, 
3.5747, 3.5652, 3.5747, 3.5558, 3.5275, 3.5558, 3.5841, 3.5747, 
3.5558, 3.5464, 3.5652, 3.6124, 3.6313, 3.6407, 3.6407, 3.7067, 
3.7067, 3.735, 3.8105, 3.8576, 3.8293, 3.801, 3.7539, 3.735, 
3.7539, 3.8671, 3.6218, 3.669, 3.6973, 3.7539, 3.7444, 3.7067, 
3.7067, 3.6313, 3.6218, 3.5652, 3.5652, 3.7161, 3.669, 3.6973, 
3.6784, 3.6313, 3.5841, 3.5747, 3.5464, 3.5464, 3.5558, 3.5275, 
3.5181, 3.5275, 3.5275, 3.603, 3.6313, 3.6596, 3.669, 3.6218, 
3.6313, 3.6973, 3.735, 3.7822, 3.8105, 3.801, 3.7161, 3.6596, 
3.6218, 3.6124, 3.6784, 3.7539, 3.7916, 3.8105, 3.7916, 3.8293, 
3.8671, 3.8388, 3.7822, 3.7067, 3.7067, 3.7727, 3.8482, 3.9519, 
3.9897, 3.9991, 4.0085, 3.9802, 3.9614, 4.0463, 4.1689, 4.1123, 
4.0085, 4.0085, 3.9802, 3.9331, 3.9236, 3.9425, 4.2632, 4.3198, 
4.2632, 4.2632, 4.2632, 4.2632, 4.2443, 4.2538, 4.2821, 4.2632, 
4.2443, 4.1594, 4.15, 4.1311, 4.1217, 4.3103, 4.3764, 4.3952, 
4.3952, 4.4141, 4.499, 4.5179, 4.4896, 4.4896, 4.4896, 4.4896, 
4.3858, 4.4141, 4.5367, 4.5367, 4.4801, 4.4707, 4.5273, 4.5367, 
4.5556, 4.565, 4.6027, 4.565, 4.4801, 4.5367, 4.4047, 4.3952, 
4.3669, 4.2349, 4.2066, 4.2349, 4.1217, 4.1217, 4.1972, 4.0368, 
3.9614, 3.9614, 3.9331, 3.8765, 3.8859, 3.8765, 3.8388, 3.8199, 
3.8671, 3.8671, 3.8829, 3.8256, 3.8447, 3.8542, 3.8638, 3.8734, 
3.8734, 3.8734, 3.9021, 3.9594, 3.9212, 3.9116, 3.9594, 3.9308, 
3.9021, 3.9021, 3.9021, 3.9021, 3.9021, 3.8829, 3.8064, 3.8064, 
3.8256, 3.7203, 3.7108, 3.7012, 3.7682, 3.7777, 3.7777, 3.7682, 
3.6917, 3.6821, 3.6821, 3.7299, 3.749, 3.7299, 3.7299, 3.8351, 
3.8542, 3.7873, 3.8064, 3.9403, 3.8925, 3.8542, 3.8925, 3.9403, 
3.9403, 3.9403, 3.9594, 3.9881, 3.9977, 3.9977, 4.0073, 4.0551, 
4.1699, 4.2464, 4.2846, 4.3229, 4.2655, 4.3229, 4.3229, 4.3037, 
4.2081, 4.2272, 4.2272, 4.2177, 4.2081, 4.2368, 4.1699, 4.2081, 
4.1985, 4.1794, 4.1125, 4.0647, 4.1125, 4.1029, 4.0647, 3.9881, 
4.0073, 3.9977, 3.9786, 3.9403, 3.9403, 3.9403, 3.9021, 3.9021, 
3.8256, 3.8256, 3.8064, 3.7299, 3.7108, 3.7203, 3.7395, 3.7299, 
3.7299, 3.7299, 3.6821, 3.6343, 3.6725, 3.7777, 3.8256, 3.8542, 
3.8734, 3.8638, 3.7777, 3.7203, 3.663, 3.6056, 3.5578, 3.5865, 
3.5195, 3.443, 3.443, 3.4334, 3.3569, 3.4047, 3.4143, 3.4239, 
3.4621, 3.4908, 3.5482, 3.5673, 3.5099, 3.3665, 3.3282, 3.2613, 
3.2804, 3.3187, 3.3474, 3.2804, 3.3187, 3.2326, 3.1943, 3.137, 
3.07, 2.9839, 3.07, 3.0604, 2.9839, 3.0796, 3.1465, 3.1178, 3.0796, 
3.07, 3.0222, 3.0126, 3.0222, 2.9457, 2.8596, 2.8213, 2.8309, 
2.8405, 2.9265, 2.9074, 2.8405, 2.8022, 2.85, 2.917, 2.917, 2.8692, 
2.8692, 2.8022, 2.7831, 2.7448, 2.7544, 2.7448, 2.7544, 2.6492, 
2.6396, 2.7066, 2.7066, 2.6779, 2.6588, 2.6683, 2.6779, 2.6396, 
2.5153, 2.5344, 2.5822, 2.6588, 2.7066, 2.8213, 2.8118, 2.764, 
2.6875, 2.697, 2.6588, 2.6301, 2.6683, 2.6779, 2.7161, 2.6396, 
2.6109, 2.5344, 2.5631, 2.4866, 2.4866, 2.4197, 2.4197, 2.5057, 
2.5822, 2.6683, 2.764, 2.764, 2.8118, 2.7927, 2.7735, 2.6875, 
2.6779, 2.6588, 2.5153, 2.4866, 2.477, 2.6492, 2.6492, 2.6779, 
2.6588, 2.6779, 2.6109, 2.5631, 2.5727, 2.6588, 2.7353, 2.7544, 
2.7544, 2.8022, 2.8022, 2.8022, 2.8309, 2.8787, 2.8692, 2.8692, 
2.9265, 2.9265, 2.9361, 2.9552, 2.9074, 2.9074, 2.917, 2.8787, 
2.8979, 2.9457, 2.9457, 2.9361, 2.9074, 2.8883, 2.9074, 2.8883, 
2.8883, 2.8787, 2.85, 2.85, 2.8692, 2.85, 2.85, 2.8692, 2.917, 
2.8596, 2.9457, 2.9648, 2.9935, 3.0604, 3.0987, 3.1656, 3.2422, 
3.2517, 3.223, 3.2422, 3.2804, 3.2517, 3.2422, 3.223, 3.29, 3.3474, 
3.3282, 3.3187, 3.29, 3.3187, 3.3378, 3.2995, 3.3569, 3.4239, 
3.1274, 3.1943, 3.29, 3.2995, 3.3091, 3.29, 3.3091, 3.3282, 3.3474, 
3.376, 3.3952, 3.3952, 3.376, 3.3569, 3.3378, 3.2326, 3.1561, 
3.1178, 3.2039, 3.2039, 3.2039, 3.2613, 3.2995, 3.3091, 3.3474, 
3.3378, 3.3856, 3.3665, 3.376, 3.3474, 3.376, 3.3569, 3.3378, 
3.2517, 3.2804, 3.2517, 3.2613, 3.3474, 3.4908, 3.4908, 3.5099, 
3.6343, 3.596, 3.4908, 3.443, 3.3952, 3.3952, 3.3569, 3.3952, 
3.4621, 3.3856, 3.4526, 3.5195, 3.5865, 3.5865, 3.5195, 3.5004, 
3.5482, 3.5099, 3.5578, 3.5673, 3.5482, 3.5482, 3.5673, 3.5386, 
3.5386, 3.5386, 3.5386, 3.5482, 3.5386, 3.5195, 3.5386, 3.5004, 
3.443, 3.4047, 3.4239, 3.4621, 3.5386, 3.6151, 3.5865, 3.5578, 
3.5865, 3.6151, 3.6343, 3.6534, 3.6534, 3.663, 3.7108, 3.7586, 
3.7969, 3.7682, 3.7586, 3.7969, 3.7873, 3.7777, 3.7395, 3.749, 
3.7299, 3.7777, 3.7682, 3.7395, 3.749, 3.7969, 3.816, 3.8064, 
3.7969, 3.816, 3.8829, 3.9308, 3.9594, 3.9977, 4.0551, 4.1029, 
4.1603, 4.1316, 4.1316, 4.0742, 4.122, 4.2081, 4.2081, 4.189, 
4.1699, 4.0838, 4.036, 3.9786, 3.9977, 3.9977, 3.9116, 3.9499, 
4.0168, 4.0168, 3.9786, 3.969, 3.9308, 3.9977, 3.9499, 3.9308, 
3.8542, 3.8447, 3.8447, 3.8542, 3.8256, 3.8447, 3.8638, 3.9116, 
3.9212, 4.0168, 4.0455, 4.0455, 4.0455, 4.0933, 4.0264, 4.0073, 
4.0455, 4.0455, 4.0073, 3.9308, 3.8734, 3.8256, 3.8925, 3.8542, 
3.8542, 3.8925, 3.9881, 4.1507, 4.2081, 4.2177, 4.2751, 4.2559, 
4.1603, 3.9499, 3.969, 3.9977, 4.122, 4.2081, 4.2559, 4.2464, 
4.5428, 4.6672, 4.5333, 4.4472, 4.3898, 4.5046, 4.5715, 4.5428, 
4.495, 4.4376, 4.5142, 4.6863, 4.7533, 4.6863, 4.7724, 4.8202, 
4.8585, 4.8393, 4.7819, 4.715, 4.6194, 4.5428, 4.6576, 4.6959, 
4.5907, 4.495, 4.562, 4.6289, 4.6289, 4.6289, 4.7246, 4.7628, 
4.7246, 4.7246, 4.715, 4.6767, 4.6289, 4.5715, 4.6098, 4.6385, 
4.6767, 4.648, 4.7819, 4.8011, 4.8202, 4.8967, 4.9828, 5.0402, 
5.1071, 5.0975, 5.0975, 5.1262, 5.2028, 5.3462, 5.1932, 5.2697, 
5.308, 5.3749, 5.1836, 5.241, 5.1741, 5.1932, 5.2793, 5.4323, 
5.3749, 5.4132, 5.3175, 5.2123, 5.0784)), row.names = c("1990-09-11", 
"1990-09-12", "1990-09-13", "1990-09-14", "1990-09-17", "1990-09-18", 
"1990-09-19", "1990-09-20", "1990-09-21", "1990-09-24", "1990-09-25", 
"1990-09-26", "1990-09-27", "1990-09-28", "1990-10-01", "1990-10-02", 
"1990-10-03", "1990-10-04", "1990-10-05", "1990-10-08", "1990-10-09", 
"1990-10-10", "1990-10-11", "1990-10-12", "1990-10-15", "1990-10-16", 
"1990-10-17", "1990-10-18", "1990-10-19", "1990-10-22", "1990-10-23", 
"1990-10-24", "1990-10-25", "1990-10-26", "1990-10-29", "1990-10-30", 
"1990-10-31", "1990-11-01", "1990-11-02", "1990-11-05", "1990-11-06", 
"1990-11-07", "1990-11-08", "1990-11-09", "1990-11-12", "1990-11-13", 
"1990-11-14", "1990-11-15", "1990-11-16", "1990-11-19", "1990-11-20", 
"1990-11-21", "1990-11-22", "1990-11-23", "1990-11-26", "1990-11-27", 
"1990-11-28", "1990-11-29", "1990-11-30", "1990-12-03", "1990-12-04", 
"1990-12-05", "1990-12-06", "1990-12-07", "1990-12-10", "1990-12-11", 
"1990-12-12", "1990-12-13", "1990-12-14", "1990-12-17", "1990-12-18", 
"1990-12-19", "1990-12-20", "1990-12-21", "1990-12-24", "1990-12-25", 
"1990-12-26", "1990-12-27", "1990-12-28", "1990-12-31", "1991-01-01", 
"1991-01-02", "1991-01-03", "1991-01-04", "1991-01-07", "1991-01-08", 
"1991-01-09", "1991-01-10", "1991-01-11", "1991-01-14", "1991-01-15", 
"1991-01-16", "1991-01-17", "1991-01-18", "1991-01-21", "1991-01-22", 
"1991-01-23", "1991-01-24", "1991-01-25", "1991-01-28", "1991-01-29", 
"1991-01-30", "1991-01-31", "1991-02-01", "1991-02-04", "1991-02-05", 
"1991-02-06", "1991-02-07", "1991-02-08", "1991-02-11", "1991-02-12", 
"1991-02-13", "1991-02-14", "1991-02-15", "1991-02-18", "1991-02-19", 
"1991-02-20", "1991-02-21", "1991-02-22", "1991-02-25", "1991-02-26", 
"1991-02-27", "1991-02-28", "1991-03-01", "1991-03-04", "1991-03-05", 
"1991-03-06", "1991-03-07", "1991-03-08", "1991-03-11", "1991-03-12", 
"1991-03-13", "1991-03-14", "1991-03-15", "1991-03-18", "1991-03-19", 
"1991-03-20", "1991-03-21", "1991-03-22", "1991-03-25", "1991-03-26", 
"1991-03-27", "1991-03-28", "1991-03-29", "1991-04-01", "1991-04-02", 
"1991-04-03", "1991-04-04", "1991-04-05", "1991-04-08", "1991-04-09", 
"1991-04-10", "1991-04-11", "1991-04-12", "1991-04-15", "1991-04-16", 
"1991-04-17", "1991-04-18", "1991-04-19", "1991-04-22", "1991-04-23", 
"1991-04-24", "1991-04-25", "1991-04-26", "1991-04-29", "1991-04-30", 
"1991-05-01", "1991-05-02", "1991-05-03", "1991-05-06", "1991-05-07", 
"1991-05-08", "1991-05-09", "1991-05-10", "1991-05-13", "1991-05-14", 
"1991-05-15", "1991-05-16", "1991-05-17", "1991-05-20", "1991-05-21", 
"1991-05-22", "1991-05-23", "1991-05-24", "1991-05-27", "1991-05-28", 
"1991-05-29", "1991-05-30", "1991-05-31", "1991-06-03", "1991-06-04", 
"1991-06-05", "1991-06-06", "1991-06-07", "1991-06-10", "1991-06-11", 
"1991-06-12", "1991-06-13", "1991-06-14", "1991-06-17", "1991-06-18", 
"1991-06-19", "1991-06-20", "1991-06-21", "1991-06-24", "1991-06-25", 
"1991-06-26", "1991-06-27", "1991-06-28", "1991-07-01", "1991-07-02", 
"1991-07-03", "1991-07-04", "1991-07-05", "1991-07-08", "1991-07-09", 
"1991-07-10", "1991-07-11", "1991-07-12", "1991-07-15", "1991-07-16", 
"1991-07-17", "1991-07-18", "1991-07-19", "1991-07-22", "1991-07-23", 
"1991-07-24", "1991-07-25", "1991-07-26", "1991-07-29", "1991-07-30", 
"1991-07-31", "1991-08-01", "1991-08-02", "1991-08-05", "1991-08-06", 
"1991-08-07", "1991-08-08", "1991-08-09", "1991-08-12", "1991-08-13", 
"1991-08-14", "1991-08-15", "1991-08-16", "1991-08-19", "1991-08-20", 
"1991-08-21", "1991-08-22", "1991-08-23", "1991-08-26", "1991-08-27", 
"1991-08-28", "1991-08-29", "1991-08-30", "1991-09-02", "1991-09-03", 
"1991-09-04", "1991-09-05", "1991-09-06", "1991-09-09", "1991-09-10", 
"1991-09-11", "1991-09-12", "1991-09-13", "1991-09-16", "1991-09-17", 
"1991-09-18", "1991-09-19", "1991-09-20", "1991-09-23", "1991-09-24", 
"1991-09-25", "1991-09-26", "1991-09-27", "1991-09-30", "1991-10-01", 
"1991-10-02", "1991-10-03", "1991-10-04", "1991-10-07", "1991-10-08", 
"1991-10-09", "1991-10-10", "1991-10-11", "1991-10-14", "1991-10-15", 
"1991-10-16", "1991-10-17", "1991-10-18", "1991-10-21", "1991-10-22", 
"1991-10-23", "1991-10-24", "1991-10-25", "1991-10-28", "1991-10-29", 
"1991-10-30", "1991-10-31", "1991-11-01", "1991-11-04", "1991-11-05", 
"1991-11-06", "1991-11-07", "1991-11-08", "1991-11-11", "1991-11-12", 
"1991-11-13", "1991-11-14", "1991-11-15", "1991-11-18", "1991-11-19", 
"1991-11-20", "1991-11-21", "1991-11-22", "1991-11-25", "1991-11-26", 
"1991-11-27", "1991-11-28", "1991-11-29", "1991-12-02", "1991-12-03", 
"1991-12-04", "1991-12-05", "1991-12-06", "1991-12-09", "1991-12-10", 
"1991-12-11", "1991-12-12", "1991-12-13", "1991-12-16", "1991-12-17", 
"1991-12-18", "1991-12-19", "1991-12-20", "1991-12-23", "1991-12-24", 
"1991-12-25", "1991-12-26", "1991-12-27", "1991-12-30", "1991-12-31", 
"1992-01-01", "1992-01-02", "1992-01-03", "1992-01-06", "1992-01-07", 
"1992-01-08", "1992-01-09", "1992-01-10", "1992-01-13", "1992-01-14", 
"1992-01-15", "1992-01-16", "1992-01-17", "1992-01-20", "1992-01-21", 
"1992-01-22", "1992-01-23", "1992-01-24", "1992-01-27", "1992-01-28", 
"1992-01-29", "1992-01-30", "1992-01-31", "1992-02-03", "1992-02-04", 
"1992-02-05", "1992-02-06", "1992-02-07", "1992-02-10", "1992-02-11", 
"1992-02-12", "1992-02-13", "1992-02-14", "1992-02-17", "1992-02-18", 
"1992-02-19", "1992-02-20", "1992-02-21", "1992-02-24", "1992-02-25", 
"1992-02-26", "1992-02-27", "1992-02-28", "1992-03-02", "1992-03-03", 
"1992-03-04", "1992-03-05", "1992-03-06", "1992-03-09", "1992-03-10", 
"1992-03-11", "1992-03-12", "1992-03-13", "1992-03-16", "1992-03-17", 
"1992-03-18", "1992-03-19", "1992-03-20", "1992-03-23", "1992-03-24", 
"1992-03-25", "1992-03-26", "1992-03-27", "1992-03-30", "1992-03-31", 
"1992-04-01", "1992-04-02", "1992-04-03", "1992-04-06", "1992-04-07", 
"1992-04-08", "1992-04-09", "1992-04-10", "1992-04-13", "1992-04-14", 
"1992-04-15", "1992-04-16", "1992-04-17", "1992-04-20", "1992-04-21", 
"1992-04-22", "1992-04-23", "1992-04-24", "1992-04-27", "1992-04-28", 
"1992-04-29", "1992-04-30", "1992-05-01", "1992-05-04", "1992-05-05", 
"1992-05-06", "1992-05-07", "1992-05-08", "1992-05-11", "1992-05-12", 
"1992-05-13", "1992-05-14", "1992-05-15", "1992-05-18", "1992-05-19", 
"1992-05-20", "1992-05-21", "1992-05-22", "1992-05-25", "1992-05-26", 
"1992-05-27", "1992-05-28", "1992-05-29", "1992-06-01", "1992-06-02", 
"1992-06-03", "1992-06-04", "1992-06-05", "1992-06-08", "1992-06-09", 
"1992-06-10", "1992-06-11", "1992-06-12", "1992-06-15", "1992-06-16", 
"1992-06-17", "1992-06-18", "1992-06-19", "1992-06-22", "1992-06-23", 
"1992-06-24", "1992-06-25", "1992-06-26", "1992-06-29", "1992-06-30", 
"1992-07-01", "1992-07-02", "1992-07-03", "1992-07-06", "1992-07-07", 
"1992-07-08", "1992-07-09", "1992-07-10", "1992-07-13", "1992-07-14", 
"1992-07-15", "1992-07-16", "1992-07-17", "1992-07-20", "1992-07-21", 
"1992-07-22", "1992-07-23", "1992-07-24", "1992-07-27", "1992-07-28", 
"1992-07-29", "1992-07-30", "1992-07-31", "1992-08-03", "1992-08-04", 
"1992-08-05", "1992-08-06", "1992-08-07", "1992-08-10", "1992-08-11", 
"1992-08-12", "1992-08-13", "1992-08-14", "1992-08-17", "1992-08-18", 
"1992-08-19", "1992-08-20", "1992-08-21", "1992-08-24", "1992-08-25", 
"1992-08-26", "1992-08-27", "1992-08-28", "1992-08-31", "1992-09-01", 
"1992-09-02", "1992-09-03", "1992-09-04", "1992-09-07", "1992-09-08", 
"1992-09-09", "1992-09-10", "1992-09-11", "1992-09-14", "1992-09-15", 
"1992-09-16", "1992-09-17", "1992-09-18", "1992-09-21", "1992-09-22", 
"1992-09-23", "1992-09-24", "1992-09-25", "1992-09-28", "1992-09-29", 
"1992-09-30", "1992-10-01", "1992-10-02", "1992-10-05", "1992-10-06", 
"1992-10-07", "1992-10-08", "1992-10-09", "1992-10-12", "1992-10-13", 
"1992-10-14", "1992-10-15", "1992-10-16", "1992-10-19", "1992-10-20", 
"1992-10-21", "1992-10-22", "1992-10-23", "1992-10-26", "1992-10-27", 
"1992-10-28", "1992-10-29", "1992-10-30", "1992-11-02", "1992-11-03", 
"1992-11-04", "1992-11-05", "1992-11-06", "1992-11-09", "1992-11-10", 
"1992-11-11", "1992-11-12", "1992-11-13", "1992-11-16", "1992-11-17", 
"1992-11-18", "1992-11-19", "1992-11-20", "1992-11-23", "1992-11-24", 
"1992-11-25", "1992-11-26", "1992-11-27", "1992-11-30", "1992-12-01", 
"1992-12-02", "1992-12-03", "1992-12-04", "1992-12-07", "1992-12-08", 
"1992-12-09", "1992-12-10", "1992-12-11", "1992-12-14", "1992-12-15", 
"1992-12-16", "1992-12-17", "1992-12-18", "1992-12-21", "1992-12-22", 
"1992-12-23", "1992-12-24", "1992-12-25", "1992-12-28", "1992-12-29", 
"1992-12-30", "1992-12-31", "1993-01-01", "1993-01-04", "1993-01-05", 
"1993-01-06", "1993-01-07", "1993-01-08", "1993-01-11", "1993-01-12", 
"1993-01-13", "1993-01-14", "1993-01-15", "1993-01-18", "1993-01-19", 
"1993-01-20", "1993-01-21", "1993-01-22", "1993-01-25", "1993-01-26", 
"1993-01-27", "1993-01-28", "1993-01-29", "1993-02-01", "1993-02-02", 
"1993-02-03", "1993-02-04", "1993-02-05", "1993-02-08", "1993-02-09", 
"1993-02-10", "1993-02-11", "1993-02-12", "1993-02-15", "1993-02-16", 
"1993-02-17", "1993-02-18", "1993-02-19", "1993-02-22", "1993-02-23", 
"1993-02-24", "1993-02-25", "1993-02-26", "1993-03-01", "1993-03-02", 
"1993-03-03", "1993-03-04", "1993-03-05", "1993-03-08", "1993-03-09", 
"1993-03-10", "1993-03-11", "1993-03-12", "1993-03-15", "1993-03-16", 
"1993-03-17", "1993-03-18", "1993-03-19", "1993-03-22", "1993-03-23", 
"1993-03-24", "1993-03-25", "1993-03-26", "1993-03-29", "1993-03-30", 
"1993-03-31", "1993-04-01", "1993-04-02", "1993-04-05", "1993-04-06", 
"1993-04-07", "1993-04-08", "1993-04-09", "1993-04-12", "1993-04-13", 
"1993-04-14", "1993-04-15", "1993-04-16", "1993-04-19", "1993-04-20", 
"1993-04-21", "1993-04-22", "1993-04-23", "1993-04-26", "1993-04-27", 
"1993-04-28", "1993-04-29", "1993-04-30", "1993-05-03", "1993-05-04", 
"1993-05-05", "1993-05-06", "1993-05-07", "1993-05-10", "1993-05-11", 
"1993-05-12", "1993-05-13", "1993-05-14", "1993-05-17", "1993-05-18", 
"1993-05-19", "1993-05-20", "1993-05-21", "1993-05-24", "1993-05-25", 
"1993-05-26", "1993-05-27", "1993-05-28", "1993-05-31", "1993-06-01", 
"1993-06-02", "1993-06-03", "1993-06-04", "1993-06-07", "1993-06-08", 
"1993-06-09", "1993-06-10", "1993-06-11", "1993-06-14", "1993-06-15", 
"1993-06-16", "1993-06-17", "1993-06-18", "1993-06-21", "1993-06-22", 
"1993-06-23", "1993-06-24", "1993-06-25", "1993-06-28", "1993-06-29", 
"1993-06-30", "1993-07-01", "1993-07-02", "1993-07-05", "1993-07-06", 
"1993-07-07", "1993-07-08", "1993-07-09", "1993-07-12", "1993-07-13", 
"1993-07-14", "1993-07-15", "1993-07-16", "1993-07-19", "1993-07-20", 
"1993-07-21", "1993-07-22", "1993-07-23", "1993-07-26", "1993-07-27", 
"1993-07-28", "1993-07-29", "1993-07-30", "1993-08-02", "1993-08-03", 
"1993-08-04", "1993-08-05", "1993-08-06", "1993-08-09", "1993-08-10", 
"1993-08-11", "1993-08-12", "1993-08-13", "1993-08-16", "1993-08-17", 
"1993-08-18", "1993-08-19", "1993-08-20", "1993-08-23", "1993-08-24", 
"1993-08-25", "1993-08-26", "1993-08-27", "1993-08-30", "1993-08-31", 
"1993-09-01", "1993-09-02", "1993-09-03", "1993-09-06", "1993-09-07", 
"1993-09-08", "1993-09-09", "1993-09-10", "1993-09-13", "1993-09-14", 
"1993-09-15", "1993-09-16", "1993-09-17", "1993-09-20", "1993-09-21", 
"1993-09-22", "1993-09-23", "1993-09-24", "1993-09-27", "1993-09-28", 
"1993-09-29", "1993-09-30", "1993-10-01", "1993-10-04", "1993-10-05", 
"1993-10-06", "1993-10-07", "1993-10-08", "1993-10-11", "1993-10-12", 
"1993-10-13", "1993-10-14", "1993-10-15", "1993-10-18", "1993-10-19", 
"1993-10-20", "1993-10-21", "1993-10-22", "1993-10-25", "1993-10-26", 
"1993-10-27", "1993-10-28", "1993-10-29", "1993-11-01", "1993-11-02", 
"1993-11-03", "1993-11-04", "1993-11-05", "1993-11-08", "1993-11-09", 
"1993-11-10", "1993-11-11", "1993-11-12", "1993-11-15", "1993-11-16", 
"1993-11-17", "1993-11-18", "1993-11-19", "1993-11-22", "1993-11-23", 
"1993-11-24", "1993-11-25", "1993-11-26", "1993-11-29", "1993-11-30", 
"1993-12-01", "1993-12-02", "1993-12-03", "1993-12-06", "1993-12-07", 
"1993-12-08", "1993-12-09", "1993-12-10", "1993-12-13", "1993-12-14", 
"1993-12-15", "1993-12-16", "1993-12-17", "1993-12-20", "1993-12-21", 
"1993-12-22", "1993-12-23", "1993-12-24", "1993-12-27", "1993-12-28", 
"1993-12-29", "1993-12-30", "1993-12-31", "1994-01-03", "1994-01-04", 
"1994-01-05", "1994-01-06", "1994-01-07", "1994-01-10", "1994-01-11", 
"1994-01-12", "1994-01-13", "1994-01-14", "1994-01-17", "1994-01-18", 
"1994-01-19", "1994-01-20", "1994-01-21", "1994-01-24", "1994-01-25", 
"1994-01-26", "1994-01-27", "1994-01-28", "1994-01-31", "1994-02-01", 
"1994-02-02", "1994-02-03", "1994-02-04", "1994-02-07", "1994-02-08", 
"1994-02-09", "1994-02-10", "1994-02-11", "1994-02-14", "1994-02-15", 
"1994-02-16", "1994-02-17", "1994-02-18", "1994-02-21"), class = "data.frame")

我想创建一个数据框,其中包含样本中每个公司的年度 return。

使用 quantmod 包中的 annualReturn 函数,我只能使用以下函数一次计算一家公司的年度 return:

yearly_return <- annualReturn(x[,1:2], type = "arithmetic", leading = FALSE)

我认为使用循环函数会生成一个数据框,其中包含 annualReturn 函数的所有结果,但它只有 returns 循环中最后一家公司的年度 returns。

for(i in 1:5) {
  yearly_return <- annualReturn(x[,i], type = "arithmetic", leading = FALSE)
}

我怎样才能获得结果,使输出是样本中所有公司的年度 return 数据框,而不是只有一个?

这是因为 'yearly_return' 在每次迭代时都会更新。相反,我们可以创建一个 list 然后根据序列号

更新 list
yearly_return <- vector('list', 5)
for(i in 1:5) {
    yearly_return[[i]] <- annualReturn(xtsprice[,i],
                type = "arithmetic", leading = FALSE)
   }

然后 merge 数据集

out <- do.call(merge, yearly_return)
colnames(out) <- colnames(xtsprice)