将基于每日日期的两个数据框合并到一个数据框中以获取面板数据

Combine two data frames based on daily dates in one data frame for panel data

我有以下面板数据作为数据框(称为 returns),其中包含从 2018 年 8 月 1 日到 2019 年 12 月 31 日对三种产品的每日观察以及相应的每日观察 return:

structure(list(Product_Name = c("A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C"), Date = structure(c(17744, 
17745, 17746, 17749, 17750, 17751, 17752, 17753, 17756, 17757, 
17759, 17760, 17774, 17777, 17778, 17779, 17780, 17781, 17784, 
17785, 17786, 17787, 17788, 17791, 17792, 17793, 17794, 17795, 
17798, 17799, 17800, 17801, 17802, 17805, 17806, 17807, 17808, 
17809, 17812, 17813, 17814, 17815, 17816, 17819, 17820, 17821, 
17822, 17823, 17826, 17827, 17828, 17829, 17830, 17833, 17834, 
17835, 17837, 17840, 17841, 17842, 17843, 17844, 17847, 17848, 
17849, 17850, 17851, 17854, 17855, 17856, 17857, 17858, 17861, 
17862, 17863, 17872, 17875, 17876, 17877, 17878, 17879, 17882, 
17883, 17884, 17885, 17886, 17892, 17893, 17896, 17898, 17899, 
17900, 17903, 17904, 17905, 17906, 17907, 17910, 17911, 17912, 
17913, 17914, 17917, 17918, 17919, 17920, 17921, 17925, 17926, 
17927, 17928, 17931, 17932, 17933, 17934, 17935, 17938, 17939, 
17940, 17941, 17942, 17945, 17946, 17947, 17948, 17949, 17952, 
17953, 17963, 17966, 17967, 17968, 17969, 17970, 17973, 17974, 
17975, 17976, 17977, 17980, 17981, 17982, 17983, 17984, 17987, 
17988, 17989, 17990, 17991, 17994, 17995, 17996, 17997, 17998, 
18001, 18002, 18003, 18004, 18009, 18010, 18011, 18012, 18015, 
18016, 18018, 18019, 18022, 18023, 18024, 18026, 18029, 18030, 
18031, 18032, 18033, 18036, 18037, 18038, 18039, 18040, 18043, 
18044, 18045, 18086, 18087, 18088, 18089, 18092, 18093, 18094, 
18095, 18096, 18099, 18100, 18101, 18102, 18103, 18106, 18107, 
18108, 18109, 18110, 18113, 18114, 18115, 18116, 18117, 18120, 
18121, 18122, 18124, 18127, 18128, 18129, 18130, 18131, 18134, 
18135, 18136, 18137, 18138, 18141, 18142, 18143, 18144, 18145, 
18148, 18149, 18150, 18151, 18152, 18155, 18156, 18157, 18158, 
18159, 18162, 18163, 18228, 18229, 18232, 18233, 18234, 18235, 
18236, 18239, 18240, 18241, 18242, 18243, 18246, 18247, 18248, 
18249, 18250, 18253, 18257, 18260, 18261, 17744, 17745, 17746, 
17749, 17750, 17751, 17752, 17753, 17756, 17757, 17759, 17760, 
17763, 17764, 17765, 17766, 17767, 17770, 17771, 17772, 17773, 
17774, 17777, 17778, 17779, 17780, 17781, 17784, 17785, 17786, 
17787, 17788, 17791, 17792, 17793, 17794, 17795, 17798, 17799, 
17800, 17801, 17802, 17805, 17806, 17807, 17808, 17809, 17812, 
17813, 17814, 17815, 17816, 17819, 17820, 17821, 17822, 17869, 
17870, 17871, 17872, 17875, 17876, 17877, 17878, 17879, 17882, 
17883, 17884, 17885, 17886, 17892, 17893, 17896, 17898, 17899, 
17900, 17903, 17904, 17905, 17906, 17907, 17910, 17911, 17912, 
17913, 17914, 17917, 17918, 17919, 17920, 17921, 17924, 17925, 
17926, 17927, 17928, 17931, 17932, 17933, 17934, 17935, 17938, 
17939, 17940, 17941, 17942, 17945, 17946, 17947, 17948, 17949, 
17952, 17953, 17954, 17955, 17956, 17959, 17960, 17961, 17962, 
17963, 17966, 17967, 17968, 17969, 17970, 17973, 17974, 17975, 
17976, 17977, 17980, 17981, 17982, 17983, 17984, 17987, 18107, 
18108, 18109, 18110, 18113, 18114, 18115, 18116, 18117, 18120, 
18121, 18122, 18124, 18127, 18128, 18129, 18130, 18131, 18134, 
18135, 18136, 18137, 18138, 18141, 18142, 18143, 18144, 18145, 
18148, 18149, 18150, 18151, 18152, 18155, 18156, 18157, 18158, 
18159, 18162, 18163, 18164, 18165, 18166, 18169, 18170, 18171, 
18172, 18173, 18176, 18177, 18178, 18179, 18180, 18183, 18184, 
18185, 18186, 18187, 18190, 18191, 18192, 18193, 18194, 18197, 
18198, 18199, 18200, 18204, 18205, 18206, 18207, 18208, 18211, 
18212, 18213, 18214, 18215, 18218, 18219, 18220, 18221, 18222, 
18225, 18226, 18227, 18228, 18229, 18232, 18233, 18234, 18235, 
18236, 18239, 18240, 18241, 18242, 18243, 18246, 18247, 18248, 
18249, 18250, 18253, 18257, 18260, 18261, 17744, 17745, 17774, 
17777, 17778, 17779, 17780, 17781, 17784, 17785, 17786, 17787, 
17788, 17791, 17792, 17793, 17794, 17795, 17798, 17799, 17800, 
17801, 17802, 17805, 17806, 17807, 17808, 17809, 17812, 17813, 
17814, 17815, 17816, 17819, 17820, 17821, 17822, 17823, 17826, 
17827, 17868, 17869, 17870, 17871, 17872, 17875, 17876, 17877, 
17878, 17879, 17882, 17883, 17884, 17885, 17886, 17892, 17893, 
17896, 17898, 17899, 17900, 17903, 17904, 17905, 17906, 17907, 
17910, 17911, 17912, 17913, 17914, 17917, 17918, 17919, 17920, 
17921, 17924, 17925, 17926, 17927, 17928, 17931, 17932, 17933, 
17934, 17935, 17938, 17939, 17940, 17941, 17942, 17945, 17946, 
17947, 17948, 17949, 17952, 17953, 17954, 17955, 17956, 17959, 
17960, 17961, 17962, 17963, 17966, 17968, 17969, 17970, 17973, 
17974, 17975, 17976, 17977, 17980, 17981, 17982, 17983, 17984, 
17987, 17988, 17989, 17990, 17991, 17994, 17995, 17997, 17998, 
18001, 18002, 18003, 18024, 18026, 18029, 18030, 18031, 18032, 
18033, 18036, 18037, 18038, 18039, 18040, 18043, 18044, 18045, 
18047, 18050, 18051, 18052, 18053, 18054, 18058, 18059, 18060, 
18061, 18064, 18065, 18066, 18067, 18068, 18071, 18072, 18073, 
18074, 18075, 18078, 18079, 18080, 18081, 18082, 18085, 18086, 
18087, 18088, 18089, 18092, 18093, 18116, 18117, 18120, 18121, 
18122, 18124, 18127, 18128, 18129, 18130, 18131, 18134, 18135, 
18136, 18137, 18138, 18141, 18142, 18143, 18144, 18145, 18148, 
18179, 18180, 18183, 18184, 18185, 18186, 18187, 18190, 18191, 
18192, 18193, 18194, 18197, 18198, 18199, 18200, 18204, 18205, 
18206, 18207, 18208, 18211, 18212, 18213, 18214, 18215, 18218, 
18219, 18220, 18221, 18222, 18225, 18226, 18248, 18249, 18250, 
18253, 18257, 18260, 18261), class = "Date"), Return = c(NA, 
-0.021053409, 0.005850216, -0.005968756, 0.012370563, 0.00076079, 
-0.005278914, -0.023443687, -0.006765762, -0.002609779, -0.004019004, 
-0.00158769, -0.008455835, -0.002497177, -0.009750922, 0.000480798, 
-0.002948523, -0.00914133, 0.00883997, -0.002716327, 0.006266591, 
0.001140627, 0.001798346, 0.005792617, 0.004989319, 0.005730675, 
0.012411611, 0.002962965, -0.000638329, 0.005614903, -0.002600932, 
-0.004582508, -0.020380701, 0.00041527, -0.00702342, 0.003815433, 
-0.004771571, -0.012090009, -0.016596879, 0.002396683, -0.003812808, 
-0.017966975, -0.003708014, 0.001949993, 0.012303812, -0.011487203, 
-0.00838619, -0.008520972, -0.009431914, -0.015985785, -0.012325086, 
0, -0.014293591, 0.016545844, -0.000198524, 0.013998191, 0.019901154, 
-0.00249816, -0.000577386, 0.014398822, 0.000948377, -0.017853063, 
-0.019554331, 0.011544987, -0.002207363, -0.013610994, -0.002572815, 
-0.000528576, -0.020228334, 0.010799313, -0.005418617, -0.005110973, 
0.024047787, -0.003957006, -0.001521013, 0.009014339, -0.025813257, 
0.012712332, 0.022713434, -0.004011019, -0.010821282, -0.011636092, 
-0.003000177, 0.005227039, -0.020648251, -0.001420253, -0.024603788, 
0.026165955, 0.00453066, -0.009295792, -0.002712737, 0.033253213, 
0.010181706, 0.004030198, 0.013743827, 0.004026311, -0.002279585, 
-0.002621233, -0.004248868, 0.009149687, -0.004564379, 0.014692397, 
-0.005051187, -0.00937214, 0.002149961, -0.000201363, 0.015784018, 
0.003196806, 0.005702177, 0.000859135, 0.002770633, -0.007140054, 
0.006678815, 0.003946595, -0.028296552, -0.010249538, 0.007680059, 
0.006882154, 0.003356384, -0.002684205, 0.01480718, 0.009291971, 
0.002489519, 0.008535088, -0.008404232, -0.001636929, 0.006075869, 
0.012042098, -0.014874304, 0.009003445, 0.006781013, 0.015283225, 
0.006887782, 0.001206847, 0.006643282, 0.004404462, -0.010793856, 
-0.00547284, -0.024482055, -0.003209435, -0.00256183, 0.003152091, 
-0.009751033, 0.006270027, 0.01592619, 0.003555157, 0.017778815, 
-0.00019018, 0.001900178, 0.001580728, -0.005384701, 0.001206158, 
0.006450411, 0.010596707, 0.004729024, 0.003408425, 0.007027956, 
-0.004001113, -0.004017187, -0.005713591, -0.009386215, 0.003577148, 
0.005310687, 0.005592508, -0.002792345, 0.002110491, -0.009970794, 
-0.023381391, 0.001665493, -0.015025271, -0.02140875, 0.007999778, 
-0.000856418, 0.007419844, -0.011646784, -0.009442815, 0.011558518, 
-0.014971834, -0.020522919, 0.006548008, 0.00040783, 0.000950958, 
-0.016152565, -0.01084645, 0.001980852, 0.001779419, 0.004139704, 
-0.002823655, 0.000394477, -0.011304037, -0.007272759, -0.00127316, 
0.000268168, 0.00741313, 0.000266117, 0.003187889, -0.002057682, 
-0.004662013, -0.019821319, -0.000272405, -0.009099991, -0.027454344, 
-0.019907189, -0.003682182, -0.002607375, 0.015257583, -0.013229038, 
-0.011867199, 0.00707903, -0.022801812, 0.005564432, 0.015636215, 
-0.014600942, 0.009123077, 0.006278768, -0.009065715, 7.34403e-05, 
0.002420509, -0.006688988, 0.015950002, 0.003622667, -0.005802584, 
-0.004374141, 0.01974962, 0.023083939, -0.003154908, 0.0140155, 
0.017910927, 0.006980949, -0.002163625, 0.026780458, -0.011065945, 
-0.010246883, -0.000673401, 0.005910421, -0.001809713, -0.016095566, 
-0.009865797, 0.001211851, -0.010895448, -0.008932676, -0.011904124, 
0.015344234, 0.004267432, 0.010334185, 0.003696862, -0.004080077, 
0.002488278, 0.018248336, 0.020866984, 0.016108302, -0.011889015, 
-0.00293327, 0.000672968, -0.003246657, 0.001103821, 0.008786974, 
-0.001459144, 0.001762865, NA, -0.01065063, -0.00274148, 0.000670834, 
-0.006667303, -0.004551893, -0.004820478, 0.000866927, -0.004280005, 
-0.008113888, 0.002316047, 0, -0.001251251, -0.006468856, -0.005560133, 
-0.010893562, 0.005174904, -0.023794526, 0.00410277, 0.00220722, 
-0.006505781, -0.00858712, -0.003429178, 0.018197792, 0.007560862, 
0.002122256, -0.003410666, -0.006791527, 0.011550378, 0.010974397, 
-0.000190386, -0.000952472, 0.002981384, -0.013197935, -0.000385159, 
-0.001735052, -0.003543704, -0.041782567, -0.023285366, -0.00255199, 
-0.03128543, -0.033547794, 0.001104688, 0.007990952, 0.003935291, 
-0.02206134, -0.026216191, -0.096402296, -0.010137791, -0.011357451, 
-0.166406282, 0.011596964, -0.065889611, 0.005659687, -0.053475813, 
0.001234915, 0.015363363, 0.017395049, -0.023743666, 0.001157296, 
-0.04202754, -0.022671636, 0.007988572, 0.061230495, -0.0014403, 
0.01136973, -0.020346191, 0.012333939, 0.040354542, 0.023273778, 
0.010103354, -0.027416272, 0.05738697, 0.045813402, 0.008049325, 
0.041737805, 0.014333812, -0.02943351, -0.016950239, -0.062256431, 
0.001550789, -0.000258298, 0.031780989, -0.00443089, -0.008245041, 
-0.007547206, -0.000170256, -0.016567614, 0.005180017, -0.011083324, 
0.019401341, -0.00068341, 0.00230464, 0.022342117, -0.006356662, 
0.028210499, -0.009672207, -0.011098342, -0.018238098, 0.019070599, 
0.015932974, -0.003527919, 0.020419664, 0.002653908, -0.004830538, 
0.003383825, 0.016116513, -0.003488468, -0.003341292, -0.006555828, 
0.003523104, -0.006012765, -0.002818378, -0.030041138, -0.007674379, 
0.016032241, 0.009513727, 0.026262183, -0.010309782, -0.006528333, 
0.016758601, 0.026366876, 0.008636698, -0.012827624, -0.011577101, 
0.00400456, 0.002191782, 0.006779687, -0.020240747, -0.008835878, 
0.022765982, -0.007846254, 0.0101102, -0.009795165, 0.01554901, 
-0.002950542, -0.010003209, 0.004733585, 0.025828207, 0.005024669, 
0.00241031, 0.003389312, -0.009146594, 0.008223375, 0.005587807, 
0.033715794, 0.015916702, -0.002391857, -0.015421141, 0.01699691, 
-0.006552797, 0.004918619, -0.017143782, 0.003973319, -0.009275842, 
0.001313355, 0.005236237, -0.009840545, -0.024757044, 0.012151708, 
-0.017758106, 0.008122122, -0.008616498, 0.005486221, 0.007654895, 
0.00935097, 0.004221706, 0.005042633, 0.007338278, 0.000891239, 
0.0040895, 0.001654749, 0.003830858, 0.022221844, 0.002929774, 
-0.012409614, -0.010450514, 0.01444994, 0.007893792, 0.004866468, 
0.009888692, 0.007100221, -0.023176272, 0.00977905, 0.00335199, 
0.013128798, -0.007416598, -0.005371949, 0.015471911, 0.001171712, 
0.002339313, 0.004385363, 0.005523655, -0.006576608, 0.003155537, 
0.00342107, 0.00948446, -0.011469463, 0.001709544, -0.016724006, 
0.001623513, -0.004653649, 0.015391783, -0.009787643, 0.012054027, 
-0.006147731, 0.008684403, 0.0116083, 0.005970491, -0.0022212, 
-0.002334864, -0.003212548, 0.001961426, 0.006185922, 0.000162272, 
0.013003945, 0.002452811, -0.024148212, 0.000709007, 0.001253167, 
0.004021307, 0.012396408, -0.003165324, 0.004984868, 0.002456479, 
-0.012883317, 0.010319898, -0.017422701, 0.022862134, 0.008051317, 
0.014611797, 0.011835681, -0.021553118, -0.00157621, 0.007438873, 
0.001512426, 0.007889997, -0.017946123, 0.005773688, 0.003917579, 
0.001094178, -0.003338379, 0.011119308, NA, 0.001235776, 0.005850898, 
-0.011045164, 0.011675648, -0.003999795, 0.012993319, -0.018810979, 
0.001272197, -0.013546874, 0.021461647, 0.001627938, -0.005366457, 
0.001054519, -0.010861897, -0.009366622, -0.032796993, 0.006259541, 
0.025299861, 0.000161512, 0.006546141, 0.013229041, -0.002695348, 
-0.003605136, 0.018470807, -0.011431692, 0.004892815, 0.00507788, 
0.000574218, 0.003802385, 0.001506533, -0.00176651, 0.004410437, 
0.010250161, 0.001075572, 0.007750791, -0.008723877, -0.009938546, 
0.000310463, -0.023927217, -0.012158526, 5.31844e-05, -0.001277207, 
-0.003894896, 0.006075806, 0.014506387, -0.018819607, 0.01798134, 
0.008299236, 0.000467715, -0.003904323, -0.00161825, 0.008324277, 
0.011026493, -0.007509551, 0.005971089, 0.001897096, -0.008539586, 
0.001393944, 0.008220773, 0.003371478, -0.000765248, -0.001021242, 
-0.011768167, 0.000413458, 0.003456017, -0.009000216, 0.004355046, 
0.004233138, -0.008640145, -0.002966203, -0.002504698, -0.005553532, 
0.000315176, -0.003841402, -0.004120885, 0.000105876, -0.016869028, 
0.000484405, -0.004368114, -0.003627606, 5.42402e-05, -0.042774234, 
0.001979359, -0.007713287, 0.027128113, -0.006281113, -0.006601423, 
0.015151805, 0.01710193, 0.003905407, -0.006190625, -0.003984395, 
-0.011993982, 0.000774593, -0.005601634, 0.022655758, -0.009341012, 
-0.014761844, 0.016845178, 0.01349093, 0.012030866, 0.004793362, 
0.003288779, 0.007281586, -0.002790283, 0.000790493, -0.014637154, 
0.005279047, -0.003390472, 0.006394605, -0.003756578, 0.003381555, 
-0.023530497, -0.001152738, -0.00501061, -0.00853711, -0.001560063, 
0.008937304, 0.010007782, 0.006109221, -0.00152381, 0.004943916, 
0.007182038, 0.00937214, -0.002615357, -0.003372684, -0.009565789, 
-0.003466208, -0.002317498, -0.003874472, 0.007348708, -0.006313856, 
-0.00373274, -0.012039273, -0.001136149, -0.003796512, 0.001520335, 
-0.011459255, -0.010038694, -0.016432295, 0.001182732, -0.016286358, 
-0.015742, 0.003654826, 0, 0.010483967, -0.004019298, -0.01174816, 
-0.004492554, -0.006570866, -0.025455161, -0.010622577, 0.007235614, 
-0.014093763, 0.008138833, 0.002130834, 0.017721983, 0.010815413, 
-0.004145943, -0.001247142, 0.016502025, -0.003278691, -0.011560822, 
-0.018441428, 0.007585371, -0.000419903, -0.012679798, 0.003820849, 
-0.005097717, -0.021959978, 0.01082496, -0.004748552, 0.012043156, 
-0.00772536, -0.001293382, 0.015835983, -0.007244864, 0.015701574, 
0.004564134, -0.009149195, -0.007380107, 0.033237751, 0.004914015, 
0.011960275, 0.016161086, 0.003028339, -0.000864304, -0.009120584, 
0.00174368, 0.004779501, 0.001299545, 0.015890392, -0.009417878, 
0, 0.006003449, 0.010208511, -0.008073126, 0.00595493, 0.011804522, 
0.002093365, -0.001622718, -0.015548595, -0.015375329, 0.006261761, 
0.007875688, 0.009860392, 0.008143367, 0.006467282, 0.005224042, 
0.00838161, -0.009183534, 0.002003607, -0.022263619, -0.002048761, 
0.004909993, -0.00122524, -0.001226743, 0.008150004, 0.013303966, 
-0.00200441, 0.011966637, -0.00516797, 0.002786071, 0.001985309, 
-0.006366914, 0.000798085, 0.00636185, 0.009860068, 0.000784621, 
0, 0.001958864, -0.004707737, 0, -0.004509128, -0.006595242, 
0.005773212, -0.005773212, -0.018364456, 0.012141661, -0.006680609
)), row.names = c(NA, -743L), class = "data.frame")

现在我有了另一个包含两列的数据框。在一栏中有每日无风险利率,在一栏中显示日期。 数据帧(称为RF)如下:

structure(list(Date = structure(c(17744, 17745, 17746, 17749, 
17750, 17751, 17752, 17753, 17756, 17757, 17758, 17759, 17760, 
17763, 17764, 17765, 17766, 17767, 17770, 17771, 17772, 17773, 
17774, 17777, 17778, 17779, 17780, 17781, 17784, 17785, 17786, 
17787, 17788, 17791, 17792, 17793, 17794, 17795, 17798, 17799, 
17800, 17801, 17802, 17805, 17806, 17807, 17808, 17809, 17812, 
17813, 17814, 17815, 17816, 17819, 17820, 17821, 17822, 17823, 
17826, 17827, 17828, 17829, 17830, 17833, 17834, 17835, 17836, 
17837, 17840, 17841, 17842, 17843, 17844, 17847, 17848, 17849, 
17850, 17851, 17854, 17855, 17856, 17857, 17858, 17861, 17862, 
17863, 17864, 17865, 17868, 17869, 17870, 17871, 17872, 17875, 
17876, 17877, 17878, 17879, 17882, 17883, 17884, 17885, 17886, 
17889, 17890, 17891, 17892, 17893, 17896, 17897, 17898, 17899, 
17900, 17903, 17904, 17905, 17906, 17907, 17910, 17911, 17912, 
17913, 17914, 17917, 17918, 17919, 17920, 17921, 17924, 17925, 
17926, 17927, 17928, 17931, 17932, 17933, 17934, 17935, 17938, 
17939, 17940, 17941, 17942, 17945, 17946, 17947, 17948, 17949, 
17952, 17953, 17954, 17955, 17956, 17959, 17960, 17961, 17962, 
17963, 17966, 17967, 17968, 17969, 17970, 17973, 17974, 17975, 
17976, 17977, 17980, 17981, 17982, 17983, 17984, 17987, 17988, 
17989, 17990, 17991, 17994, 17995, 17996, 17997, 17998, 18001, 
18002, 18003, 18004, 18005, 18008, 18009, 18010, 18011, 18012, 
18015, 18016, 18017, 18018, 18019, 18022, 18023, 18024, 18025, 
18026, 18029, 18030, 18031, 18032, 18033, 18036, 18037, 18038, 
18039, 18040, 18043, 18044, 18045, 18046, 18047, 18050, 18051, 
18052, 18053, 18054, 18057, 18058, 18059, 18060, 18061, 18064, 
18065, 18066, 18067, 18068, 18071, 18072, 18073, 18074, 18075, 
18078, 18079, 18080, 18081, 18082, 18085, 18086, 18087, 18088, 
18089, 18092, 18093, 18094, 18095, 18096, 18099, 18100, 18101, 
18102, 18103, 18106, 18107, 18108, 18109, 18110, 18113, 18114, 
18115, 18116, 18117, 18120, 18121, 18122, 18123, 18124, 18127, 
18128, 18129, 18130, 18131, 18134, 18135, 18136, 18137, 18138, 
18141, 18142, 18143, 18144, 18145, 18148, 18149, 18150, 18151, 
18152, 18155, 18156, 18157, 18158, 18159, 18162, 18163, 18164, 
18165, 18166, 18169, 18170, 18171, 18172, 18173, 18176, 18177, 
18178, 18179, 18180, 18183, 18184, 18185, 18186, 18187, 18190, 
18191, 18192, 18193, 18194, 18197, 18198, 18199, 18200, 18201, 
18204, 18205, 18206, 18207, 18208, 18211, 18212, 18213, 18214, 
18215, 18218, 18219, 18220, 18221, 18222, 18225, 18226, 18227, 
18228, 18229, 18232, 18233, 18234, 18235, 18236, 18239, 18240, 
18241, 18242, 18243, 18246, 18247, 18248, 18249, 18250, 18253, 
18254, 18255, 18256, 18257, 18260, 18261), class = "Date"), RF = c(0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01)), row.names = c(NA, -370L
), class = "data.frame")

谁能帮我用代码来执行以下操作:

我希望 RF 数据框中的无风险利率 (RF) 位于 Return 数据框中。因此,对于 Return 数据框中的每个日期,我希望从 RF 数据框中获得相应的无风险利率。由于 RF 数据框在 2018-08-01 到 2019-12-31 之间的每日日期比 Return 数据框多,我在如何匹配这两个数据框方面遇到了困难。

您可以通过 Date 使用 left_join 加入两个数据帧。您可以使用以下代码:

library(dplyr)
df <- left_join(returns, rf, by = "Date")

输出 head(df) 如下所示:

  Product_Name       Date       Return   RF
1            A 2018-08-01           NA 0.01
2            A 2018-08-02 -0.021053409 0.01
3            A 2018-08-03  0.005850216 0.01
4            A 2018-08-06 -0.005968756 0.01
5            A 2018-08-07  0.012370563 0.01
6            A 2018-08-08  0.000760790 0.01

除了@PaulS 在评论中建议的 right_join 之外,另一种方法是将 RF 数据框中的无风险利率 (RF) 置于 Return 数据框中就是用full_joinfull_join 如果你想检查哪些数据在一个数据框中可用但在另一个数据框中不可用,对于相同的 Date.

来说,full_join 是非常合适的选项
# Your data frames are Return and RF.
glimpse(Return)
# Rows: 743
# Columns: 3
# $ Product_Name <chr> "A", "A", "A", "A", "A", "A", "A", "A", "A", "~
# $ Date         <date> 2018-08-01, 2018-08-02, 2018-08-03, 2018-08-0~
# $ Return       <dbl> NA, -0.021053409, 0.005850216, -0.005968756, 0~
glimpse(RF)
# Rows: 370
# Columns: 2
# $ Date <date> 2018-08-01, 2018-08-02, 2018-08-03, 2018-08-06, 2018-~
# $ RF   <dbl> 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, ~

#Join Return with RF

library(dplyr)
joined <- Return %>% full_join(RF, by = "Date")
head(joined)
# Product_Name       Date       Return   RF
# 1            A 2018-08-01           NA 0.01
# 2            A 2018-08-02 -0.021053409 0.01
# 3            A 2018-08-03  0.005850216 0.01
# 4            A 2018-08-06 -0.005968756 0.01
# 5            A 2018-08-07  0.012370563 0.01
# 6            A 2018-08-08  0.000760790 0.01

tail(joined)
# Product_Name       Date Return   RF
# 757         <NA> 2019-06-10     NA 0.01
# 758         <NA> 2019-08-15     NA 0.01
# 759         <NA> 2019-11-01     NA 0.01
# 760         <NA> 2019-12-24     NA 0.01
# 761         <NA> 2019-12-25     NA 0.01
# 762         <NA> 2019-12-26     NA 0.01

如果要获取所有 Return 数据不可用的行,可以使用 joined %>% filter(is.na(Return)),这将导致:

# Product_Name       Date Return   RF
# 1             A 2018-08-01     NA 0.01
# 2             B 2018-08-01     NA 0.01
# 3             C 2018-08-01     NA 0.01
# 4          <NA> 2018-08-15     NA 0.01
# 5          <NA> 2018-11-01     NA 0.01
# 6          <NA> 2018-11-29     NA 0.01
# 7          <NA> 2018-11-30     NA 0.01
# 8          <NA> 2018-12-24     NA 0.01
# 9          <NA> 2018-12-25     NA 0.01
# 10         <NA> 2018-12-26     NA 0.01
# 11         <NA> 2019-01-01     NA 0.01
# 12         <NA> 2019-04-19     NA 0.01
# 13         <NA> 2019-04-22     NA 0.01
# 14         <NA> 2019-05-01     NA 0.01
# 15         <NA> 2019-05-09     NA 0.01
# 16         <NA> 2019-05-30     NA 0.01
# 17         <NA> 2019-06-10     NA 0.01
# 18         <NA> 2019-08-15     NA 0.01