有什么更好的传播时间序列数据的方法。我有重复的观察结果,所以我包含了i = row_number()
,但这带来了引入NA和现在重复的日期的新问题。
x <- portfolio_prices %>%
mutate(i = row_number()) %>%
spread(cusip, prc)
应用上面的代码后,我的数据如下所示:
date_ret i 92220P10 G7665A10
1 2010-06-02 1 NA 24.68
2 2010-06-02 252 51.11 NA
3 2010-06-03 2 NA 23.79
4 2010-06-03 253 51.85 NA
5 2010-06-04 3 NA 22.71
6 2010-06-04 254 49.50 NA
我想先查询数据,但只有1个日期,没有重复的日期。
数据:
portfolio_prices <- structure(list(cusip = c("G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10", "G7665A10",
"G7665A10", "G7665A10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10", "92220P10",
"92220P10", "92220P10", "92220P10", "92220P10", "92220P10"),
date_ret = structure(c(14762, 14763, 14764, 14767, 14768,
14769, 14770, 14771, 14774, 14775, 14776, 14777, 14778, 14781,
14782, 14783, 14784, 14785, 14788, 14789, 14790, 14791, 14792,
14796, 14797, 14798, 14799, 14802, 14803, 14804, 14805, 14806,
14809, 14810, 14811, 14812, 14813, 14816, 14817, 14818, 14819,
14820, 14823, 14824, 14825, 14826, 14827, 14830, 14831, 14832,
14833, 14834, 14837, 14838, 14839, 14840, 14841, 14844, 14845,
14846, 14847, 14848, 14851, 14852, 14853, 14854, 14855, 14859,
14860, 14861, 14862, 14865, 14866, 14867, 14868, 14869, 14872,
14873, 14874, 14875, 14876, 14879, 14880, 14881, 14882, 14883,
14886, 14887, 14888, 14889, 14890, 14893, 14894, 14895, 14896,
14897, 14900, 14901, 14902, 14903, 14904, 14907, 14908, 14909,
14910, 14911, 14914, 14915, 14916, 14917, 14918, 14921, 14922,
14923, 14924, 14925, 14928, 14929, 14930, 14931, 14932, 14935,
14936, 14937, 14939, 14942, 14943, 14944, 14945, 14946, 14949,
14950, 14951, 14952, 14953, 14956, 14957, 14958, 14959, 14960,
14963, 14964, 14965, 14966, 14970, 14971, 14972, 14973, 14974,
14977, 14978, 14979, 14980, 14981, 14984, 14985, 14986, 14987,
14988, 14992, 14993, 14994, 14995, 14998, 14999, 15000, 15001,
15002, 15005, 15006, 15007, 15008, 15009, 15012, 15013, 15014,
15015, 15016, 15019, 15020, 15021, 15022, 15023, 15027, 15028,
15029, 15030, 15033, 15034, 15035, 15036, 15037, 15040, 15041,
15042, 15043, 15044, 15047, 15048, 15049, 15050, 15051, 15054,
15055, 15056, 15057, 15058, 15061, 15062, 15063, 15064, 15065,
15068, 15069, 15070, 15071, 15072, 15075, 15076, 15077, 15078,
15079, 15082, 15083, 15084, 15085, 15089, 15090, 15091, 15092,
15093, 15096, 15097, 15098, 15099, 15100, 15103, 15104, 15105,
15106, 15107, 15110, 15111, 15112, 15113, 15114, 15117, 15118,
15119, 15120, 15121, 14762, 14763, 14764, 14767, 14768, 14769,
14770, 14771, 14774, 14775, 14776, 14777, 14778, 14781, 14782,
14783, 14784, 14785, 14788, 14789, 14790, 14791, 14792, 14796,
14797, 14798, 14799, 14802, 14803, 14804, 14805, 14806, 14809,
14810, 14811, 14812, 14813, 14816, 14817, 14818, 14819, 14820,
14823, 14824, 14825, 14826, 14827, 14830, 14831, 14832, 14833,
14834, 14837, 14838, 14839, 14840, 14841, 14844, 14845, 14846,
14847, 14848, 14851, 14852, 14853, 14854, 14855, 14859, 14860,
14861, 14862, 14865, 14866, 14867, 14868, 14869, 14872, 14873,
14874, 14875, 14876, 14879, 14880, 14881, 14882, 14883, 14886,
14887, 14888, 14889, 14890, 14893, 14894, 14895, 14896, 14897,
14900, 14901, 14902, 14903, 14904, 14907, 14908, 14909, 14910,
14911, 14914, 14915, 14916, 14917, 14918, 14921, 14922, 14923,
14924, 14925, 14928, 14929, 14930, 14931, 14932, 14935, 14936,
14937, 14939, 14942, 14943, 14944, 14945, 14946, 14949, 14950,
14951, 14952, 14953, 14956, 14957, 14958, 14959, 14960, 14963,
14964, 14965, 14966, 14970, 14971, 14972, 14973, 14974, 14977,
14978, 14979, 14980, 14981, 14984, 14985, 14986, 14987, 14988,
14992, 14993, 14994, 14995, 14998, 14999, 15000, 15001, 15002,
15005, 15006, 15007, 15008, 15009, 15012, 15013, 15014, 15015,
15016, 15019, 15020, 15021, 15022, 15023, 15027, 15028, 15029,
15030, 15033, 15034, 15035, 15036, 15037, 15040, 15041, 15042,
15043, 15044, 15047, 15048, 15049, 15050, 15051, 15054, 15055,
15056, 15057, 15058, 15061, 15062, 15063, 15064, 15065, 15068,
15069, 15070, 15071, 15072, 15075, 15076, 15077, 15078, 15079,
15082, 15083, 15084, 15085, 15089, 15090, 15091, 15092, 15093,
15096, 15097, 15098, 15099, 15100, 15103, 15104, 15105, 15106,
15107, 15110, 15111, 15112, 15113, 15114, 15117, 15118, 15119
), class = "Date"), prc = c(24.6800003051758, 23.7900009155273,
22.7099990844727, 22.2099990844727, 22.9300003051758, 22.9099998474121,
24.2800006866455, 24.1800003051758, 23.6000003814697, 25.1700000762939,
25.0699996948242, 24.6800003051758, 25.2600002288818, 25.2700004577637,
24.4200000762939, 23.6100006103516, 22.9899997711182, 23.6100006103516,
22.8700008392334, 22.0699996948242, 21.9400005340576, 21.6900005340576,
22.8199996948242, 23.1100006103516, 24.0799999237061, 24.5400009155273,
24.4699993133545, 24.5300006866455, 24.8600006103516, 24.6700000762939,
24.6800003051758, 24.0200004577637, 24.0499992370605, 24.75,
24.4300003051758, 24.9300003051758, 25.3400001525879, 25.3999996185303,
25.0799999237061, 25.1499996185303, 25.1700000762939, 25.2600002288818,
26.0300006866455, 26.9699993133545, 27.6900005340576, 27.2000007629395,
27.0799999237061, 27.1499996185303, 26.9599990844727, 25.7399997711182,
25.5300006866455, 25.7600002288818, 26.5900001525879, 26.4899997711182,
26.2600002288818, 25.7399997711182, 25.2099990844727, 25.6000003814697,
25.6000003814697, 25.0200004577637, 24.5300006866455, 25.6000003814697,
25.7800006866455, 25.7099990844727, 28.0200004577637, 28.0699996948242,
28.3999996185303, 28.1900005340576, 28.6700000762939, 28.9699993133545,
29.5900001525879, 30.1299991607666, 29.6900005340576, 30.1000003814697,
29.5900001525879, 29.5900001525879, 30.8999996185303, 30.8899993896484,
30.0300006866455, 29.5400009155273, 29.7900009155273, 29.4599990844727,
29.7600002288818, 30.3500003814697, 30.3600006103516, 31.0200004577637,
30.0799999237061, 31.25, 31.6000003814697, 31.4699993133545,
31.9500007629395, 31.9099998474121, 31.8400001525879, 31.7900009155273,
32.3600006103516, 33.0400009155273, 32.7099990844727, 31.9300003051758,
32.2000007629395, 32.1599998474121, 32.2400016784668, 32.6100006103516,
32.6800003051758, 32.3699989318848, 32.2299995422363, 32.9000015258789,
32.8400001525879, 31.6000003814697, 31.0400009155273, 32.2700004577637,
32.1800003051758, 32.5699996948242, 31.3600006103516, 31.8899993896484,
32.0699996948242, 31.0599994659424, 30.7000007629395, 29.7199993133545,
30.1700000762939, 31.3999996185303, 31.5900001525879, 31.4699993133545,
30.4799995422363, 31.0499992370605, 30.3199996948242, 30.7600002288818,
30.1499996185303, 31.0599994659424, 32.0299987792969, 32.2000007629395,
32.0299987792969, 31.6399993896484, 31.7199993133545, 32.75,
33.5999984741211, 33.5800018310547, 33.5499992370605, 33.5200004577637,
33.7900009155273, 34.3899993896484, 34.8400001525879, 34.9099998474121,
34.2799987792969, 34.6399993896484, 34.0200004577637, 34.0900001525879,
34.8199996948242, 34.7799987792969, 34.9099998474121, 34.2000007629395,
33.2000007629395, 33.4599990844727, 32.8600006103516, 33.2200012207031,
33.4099998474121, 34.4500007629395, 34.689998626709, 34.8499984741211,
35.25, 34.7999992370605, 34.0900001525879, 33.5099983215332,
33.4900016784668, 33.0999984741211, 33.0800018310547, 34.7900009155273,
34.9799995422363, 34.310001373291, 34.2799987792969, 35.060001373291,
36.25, 36.810001373291, 36.5800018310547, 37.7700004577637,
37.5099983215332, 37.6500015258789, 38.0299987792969, 38.0099983215332,
39.1100006103516, 38.4700012207031, 38.7599983215332, 40.1100006103516,
39.7000007629395, 39.5900001525879, 39.7000007629395, 39.8300018310547,
42.4500007629395, 42.6699981689453, 41.7400016784668, 42.7000007629395,
42.7599983215332, 42.9000015258789, 41.6500015258789, 41.4599990844727,
41.3499984741211, 39.8600006103516, 40.8899993896484, 41.1100006103516,
40.5499992370605, 39.6199989318848, 41.5099983215332, 40.4300003051758,
42.6500015258789, 42.1800003051758, 41.3699989318848, 41.7200012207031,
41.3499984741211, 41.310001373291, 43.4599990844727, 42.8899993896484,
44.1800003051758, 44.189998626709, 43.939998626709, 43.9599990844727,
42.7900009155273, 42.5299987792969, 42.560001373291, 41.25,
40.6300010681152, 40.7200012207031, 40.7900009155273, 40.7299995422363,
39.9199981689453, 41.2099990844727, 42.0400009155273, 42.2099990844727,
41.810001373291, 41.7000007629395, 40.8800010681152, 40.9500007629395,
41.7000007629395, 40.5299987792969, 38.7099990844727, 37.9199981689453,
37.5900001525879, 37.6199989318848, 38.1599998474121, 38.5800018310547,
37.4900016784668, 37.8899993896484, 38.0400009155273, 37.9599990844727,
37.7299995422363, 39.4500007629395, 39.2599983215332, 39.3499984741211,
38.2700004577637, 38.8499984741211, 40.0299987792969, 39.9700012207031,
39.7299995422363, 51.1100006103516, 51.8499984741211, 49.5,
48.7999992370605, 48.6800003051758, 48.8699989318848, 50.5400009155273,
50.4700012207031, 50.310001373291, 51.810001373291, 52.4900016784668,
52.6300010681152, 53.3800010681152, 54.1300010681152, 53.2200012207031,
52.9099998474121, 51.939998626709, 52.6699981689453, 53.2799987792969,
52.25, 52.2799987792969, 51.7700004577637, 51.5299987792969,
52.3800010681152, 53.5400009155273, 53.3600006103516, 53.3800010681152,
53.3800010681152, 54.6800003051758, 54.7099990844727, 54.5400009155273,
53.5099983215332, 53.9000015258789, 54.5299987792969, 53.1599998474121,
54.8699989318848, 55.7999992370605, 56.25, 55.5299987792969,
54.6300010681152, 54.7900009155273, 55.2000007629395, 55.0900001525879,
55.9799995422363, 56.9700012207031, 56.2099990844727, 55.7999992370605,
56.4000015258789, 56.5900001525879, 55.3899993896484, 55.560001373291,
55.3300018310547, 54.6599998474121, 56.4500007629395, 55.810001373291,
54.75, 54.2200012207031, 53.9099998474121, 52.0800018310547,
52.8300018310547, 53.060001373291, 54.2000007629395, 53.2799987792969,
53.2400016784668, 54.6300010681152, 55.4500007629395, 56.1800003051758,
56.0800018310547, 56.310001373291, 57.0800018310547, 58.3499984741211,
58.9000015258789, 58.8600006103516, 59.9300003051758, 59.0400009155273,
59.189998626709, 59.8199996948242, 60.2999992370605, 59.9199981689453,
59.5999984741211, 60.7999992370605, 60, 60.3800010681152,
60.5900001525879, 60.5, 60.6800003051758, 60.5699996948242,
61.7799987792969, 61.7999992370605, 61.9300003051758, 61.5800018310547,
61.4599990844727, 61.6800003051758, 61.9799995422363, 61.9099998474121,
62.1199989318848, 62.25, 61.4000015258789, 61.3300018310547,
61.3899993896484, 61.0299987792969, 61.1500015258789, 61.1699981689453,
61.0699996948242, 60.7099990844727, 63.2200012207031, 62.689998626709,
62.560001373291, 63.2000007629395, 64.4100036621094, 65,
64.8000030517578, 64.8000030517578, 64.8300018310547, 64.6600036621094,
64.0100021362305, 64.0899963378906, 63.25, 63.5099983215332,
64.5, 65, 65.0400009155273, 64.4599990844727, 65.629997253418,
65.5199966430664, 65.7300033569336, 65.8300018310547, 67.7200012207031,
68.5999984741211, 69.2300033569336, 68.870002746582, 67.629997253418,
67.370002746582, 67.2200012207031, 67.7300033569336, 67.620002746582,
68.370002746582, 68.3399963378906, 69.7300033569336, 70.370002746582,
70.4499969482422, 70.1600036621094, 70.5999984741211, 70.6900024414062,
70.6399993896484, 70.5599975585938, 69.5899963378906, 70.0299987792969,
69.2799987792969, 69.7900009155273, 69.4599990844727, 69.5899963378906,
69, 68.5599975585938, 68.6699981689453, 69.4599990844727,
69.5800018310547, 69.9800033569336, 70.4700012207031, 71.7300033569336,
70.8399963378906, 70.5999984741211, 71.4100036621094, 71.3499984741211,
71.3099975585938, 71.9599990844727, 68.6999969482422, 68.6699981689453,
67.5699996948242, 66.9000015258789, 67.1999969482422, 67.9800033569336,
67.4000015258789, 67.8099975585938, 68.3600006103516, 68.4599990844727,
69.0800018310547, 69.1100006103516, 71.3600006103516, 70.75,
70.9100036621094, 70.5699996948242, 70, 68.4499969482422,
67.0899963378906, 67.879997253418, 68.6600036621094, 69.2799987792969,
69.0800018310547, 68.7200012207031, 71.4599990844727, 70.5,
69.7799987792969, 69.6900024414062, 68.4499969482422, 67.5500030517578,
67.3199996948242, 66.5199966430664, 65.5400009155273, 64.5999984741211,
65.3399963378906, 65.7699966430664, 66.3300018310547, 66.2699966430664,
66.1699981689453, 66.4499969482422, 66.5199966430664, 66.9599990844727,
67.3499984741211, 67.1800003051758, 67.6399993896484, 68.6800003051758,
68.6399993896484, 69.0699996948242, 68.8499984741211, 68.7799987792969,
68.7699966430664, 68.8000030517578, 69.3300018310547, 69.0400009155273,
69.1900024414062, 69.75, 69.1600036621094, 69.6399993896484,
70.5500030517578, 70.3399963378906, 69.2200012207031, 70.2600021362305,
71.4300003051758, 70.4599990844727, 70.1999969482422, 70.4700012207031,
70.3099975585938, 69.6600036621094, 69.5100021362305, 70.2399978637695,
70.4499969482422, 71.1600036621094, 70.1800003051758, 69.9100036621094,
69.0899963378906, 68.8099975585938, 68.4199981689453, 68.7099990844727,
69.1399993896484, 68.9199981689453, 67.370002746582, 66.9000015258789,
66.9300003051758)), .internal.selfref = <pointer: 0x0000000002511ef0>, na.action = structure(c(`1` = 1L,
`3014` = 3014L), class = "omit"), row.names = 2:501, class = "data.frame")
答案 0 :(得分:1)
考虑到您要传播的变量,我在提供的数据集中没有看到重复项
library(dplyr)
library(tidyr)
portfolio_prices %>%
nrow()
portfolio_prices %>%
as_tibble() %>%
distinct(cusip, date_ret) %>%
nrow()
以便您可以直接传播它们
portfolio_prices %>%
as_tibble() %>%
spread(cusip, prc)
如果您确实有重复项,则可能要在进行突变之前使用group_by
,如下所示:
portfolio_prices %>%
as_tibble() %>%
group_by(date_ret) %>%
mutate(row = 1:n()) %>%
ungroup() %>%
spread(cusip, prc)