我的数据如下:
Date EUR HIGH EUR LOW EUR OPEN EUR CLOSE JPY CURNCY JPY LOW JPY OPEN JPY CLOSE GBP HIGH GBP LOW GBP OPEN GBP CLOSE
8/30/2016 1.12 1.12 1.12 102.56 101.76 102.50 1.31 1.31 1.31
8/29/2016 1.12 1.12 1.12 102.39 101.84 101.92 1.32 1.32 1.32
8/28/2016 1.13 1.12 1.12 101.94 100.06 101.84 1.33 1.33 1.33
8/27/2016 1.13 1.12 1.12 101.94 100.06 101.84 1.33 1.33 1.33
8/26/2016 1.13 1.12 1.12 101.94 100.06 101.84 1.33 1.33 1.33
8/25/2016 1.13 1.13 1.13 100.62 100.30 100.53 1.33 1.33 1.33
8/24/2016 1.13 1.12 1.13 100.61 100.10 100.45 1.33 1.33 1.33
8/23/2016 1.14 1.13 1.13 100.39 99.94 100.24 1.32 1.32 1.32
8/22/2016 1.13 1.13 1.13 100.93 100.21 100.33 1.32 1.32 1.32
8/21/2016 1.14 1.13 1.13 100.46 99.88 100.22 1.32 1.32 1.32
8/20/2016 1.14 1.13 1.13 100.46 99.88 100.22 1.32 1.32 1.32
8/19/2016 1.14 1.13 1.13 100.46 99.88 100.22 1.32 1.32 1.32
我使用了以下代码
cur<-c('EUR', 'JPY', 'GBP', 'CHF', 'AUD', 'CAD', 'NZD', 'SEK', 'NOK', 'CZK', 'HUF', 'ILS',
'PLN', 'RUB', 'TRY', 'ZAR', 'BRL', 'CLP', 'COP', 'MXN', 'PEN', 'CNY', 'IDR', 'INR',
'KRW', 'MYR', 'SGD', 'PHP', 'THB')
i <- sapply(cur, function(y) grep(y,names(x)), USE.NAMES = F)
MergedData <- sapply(i, function(i) x[c(1,i)])
这是什么将它分别安排到像这样的数据结构
>MergedData[[1]]
Date EUR High EUR Low EUR Close EUR OPEN
1/1 1.15 1.13 1.12 1.50
1/2 1.15 1.13 1.12 1.51
1/3 1.15 1.13 1.12 1.30
>MergedData[[2]]
Date JPY High JPY Low JPY Close JPY OPEN
1/1 1.15 1.13 1.12 1.50
1/2 1.15 1.13 1.12 1.51
1/3 1.15 1.13 1.12 1.30
但后来我注意到MergedData [[21]]返回:
> MergedData[[21]]
Date EUR OPEN JPY OPEN GBP OPEN
1/1 1.15 1.50 1.12
1/2 1.15 1.50 1.12
1/3 1.15 1.50 1.12
我真的会提供一些帮助。我已经被困了好几天了。
附件是我的数据的小样本输入
structure(list(Date = structure(c(1486620000, 1486533600, 1486447200,
1486360800, 1486274400, 1486188000, 1486101600, 1486015200, 1485928800
), tzone = "", class = c("POSIXct", "POSIXt")), EUR.HIGH = c(1.071,
1.0714, 1.075, 1.0802, 1.0798, 1.0798, 1.0798, 1.0829, 1.0807
), EUR.LOW = c(1.0651, 1.0641, 1.0656, 1.0706, 1.0713, 1.0713,
1.0713, 1.0756, 1.0732), EUR.OPEN = c(1.0698, 1.0683, 1.075,
1.0783, 1.0759, 1.0759, 1.0759, 1.0769, 1.0798), EUR.LAST = c(1.0655,
1.0698, 1.0683, 1.075, 1.0783, 1.0783, 1.0783, 1.0759, 1.0769
), JPY.HIGH = c(113.35, 112.54, 112.58, 112.78, 113.45, 113.45,
113.45, 113.36, 113.95), JPY.LOW = c(111.74, 111.63, 111.6, 111.63,
112.32, 112.32, 112.32, 112.06, 112.64), JPY.CLOSE = c(111.93,
112.39, 111.74, 112.44, 112.8, 112.8, 112.8, 113.25, 112.8),
JPY.OPEN = c(111.93, 112.39, 111.74, 112.61, 112.8, 112.8,
112.8, 113.25, 112.8), JPY.LAST = c(113.25, 111.93, 112.39,
111.74, 112.61, 112.61, 112.61, 112.8, 113.25), GBP.HIGH = c(1.2582,
1.255, 1.2546, 1.2509, 1.2538, 1.2538, 1.2538, 1.2706, 1.268
), GBP.LOW = c(1.2491, 1.2475, 1.2347, 1.2428, 1.246, 1.246,
1.246, 1.2518, 1.2543), GBP.OPEN = c(1.2541, 1.2509, 1.2469,
1.2497, 1.2527, 1.2527, 1.2527, 1.2659, 1.2579), GBP.LAST = c(1.2497,
1.2541, 1.2509, 1.2469, 1.2484, 1.2484, 1.2484, 1.2527, 1.2659
), AUD.HIGH = c(0.7664, 0.7666, 0.7681, 0.7684, 0.7695, 0.7695,
0.7695, 0.7696, 0.7597), AUD.LOW = c(0.7611, 0.7612, 0.7606,
0.763, 0.7622, 0.7622, 0.7622, 0.7578, 0.7552), AUD.OPEN = c(0.7645,
0.7628, 0.766, 0.7681, 0.7658, 0.7658, 0.7658, 0.7586, 0.7585
), AUD.LAST = c(0.7625, 0.7645, 0.7628, 0.766, 0.768, 0.768,
0.768, 0.7658, 0.7586), CAD.HIGH = c(1.3168, 1.32, 1.3212,
1.3135, 1.3076, 1.3076, 1.3076, 1.3052, 1.3102), CAD.LOW = c(1.3095,
1.3135, 1.3075, 1.3007, 1.2993, 1.2993, 1.2993, 1.2981, 1.3029
), CAD.OPEN = c(1.3144, 1.3185, 1.3084, 1.3038, 1.3027, 1.3027,
1.3027, 1.3049, 1.303), CAD.LAST = c(1.3145, 1.3144, 1.3185,
1.3084, 1.3023, 1.3023, 1.3023, 1.3027, 1.3049), NZD.HIGH = c(0.7266,
0.7334, 0.7376, 0.7327, 0.7331, 0.7331, 0.7331, 0.7338, 0.7313
), NZD.LOW = c(0.7174, 0.7245, 0.7279, 0.7285, 0.7249, 0.7249,
0.7249, 0.7243, 0.7243), NZD.OPEN = c(0.7266, 0.7301, 0.7321,
0.7314, 0.7288, 0.7288, 0.7288, 0.728, 0.7312), NZD.LAST = c(0.7187,
0.7266, 0.7301, 0.7321, 0.7315, 0.7315, 0.7315, 0.7288, 0.728
), SEK.HIGH = c(8.913, 8.8926, 8.9001, 8.8508, 8.8136, 8.8136,
8.8136, 8.7581, 8.7785), SEK.LOW = c(8.8341, 8.829, 8.8209,
8.7508, 8.7388, 8.7388, 8.7388, 8.697, 8.7177), SEK.OPEN = c(8.8359,
8.8704, 8.8233, 8.7682, 8.7513, 8.7513, 8.7513, 8.7528, 8.7424
), SEK.CLOSE = c(8.9044, 8.8359, 8.8706, 8.8234, 8.7648,
8.7648, 8.7648, 8.7513, 8.7527), NOK.HIGH = c(8.3451, 8.3486,
8.3375, 8.2733, 8.2712, 8.2712, 8.2712, 8.2538, 8.2672),
NOK.LOW = c(8.2947, 8.2885, 8.2529, 8.1923, 8.1892, 8.1892,
8.1892, 8.1762, 8.2189), NOK.OPEN = c(8.316, 8.3213, 8.2538,
8.2081, 8.2525, 8.2525, 8.2525, 8.2343, 8.2464), NOK.CLOSE = c(8.3383,
8.316, 8.3213, 8.2537, 8.2066, 8.2066, 8.2066, 8.2525, 8.2343
), CZK.HIGH = c(25.369, 25.394, 25.3584, 25.2388, 25.2215,
25.2215, 25.2215, 25.1215, 25.1755), CZK.LOW = c(25.23, 25.2239,
25.1323, 25.0175, 25.025, 25.025, 25.025, 24.954, 25.0023
), CZK.OPEN = c(25.2588, 25.2935, 25.136, 25.065, 25.1162,
25.1162, 25.1162, 25.0986, 25.0247), CZK.CLOSE = c(25.3617,
25.2588, 25.2935, 25.1381, 25.0547, 25.0547, 25.0547, 25.1162,
25.0986), HUF.HIGH = c(290.11, 290.86, 290.67, 289.12, 288.79,
288.79, 288.79, 287.88, 288.84), HUF.LOW = c(288.18, 288.6,
288.2, 286.86, 286.46, 286.46, 286.46, 285.01, 286.92), HUF.OPEN = c(288.72,
289.31, 288.23, 286.96, 287.45, 287.45, 287.45, 287.31, 287.01
), HUF.CLOSE = c(289.32, 288.72, 289.29, 288.23, 287.45,
287.45, 287.45, 287.45, 287.31), ILS.HIGH = c(3.7545, 3.7594,
3.7588, 3.7578, 3.7617, 3.7617, 3.7617, 3.7757, 3.784), ILS.LOW = c(3.741,
3.7445, 3.7409, 3.7384, 3.7429, 3.7429, 3.7429, 3.7508, 3.7638
), ILS.OPEN = c(3.7465, 3.7483, 3.7414, 3.752, 3.7598, 3.7598,
3.7598, 3.7757, 3.7663), ILS.CLOSE = c(3.7463, 3.7473, 3.7465,
3.7397, 3.7431, 3.7431, 3.7431, 3.7626, 3.7757), PLN.HIGH = c(4.0555,
4.0586, 4.0408, 4.0068, 4.0151, 4.0151, 4.0151, 4.0145, 4.0233
), PLN.LOW = c(4.0252, 4.0246, 3.9948, 3.9785, 3.9777, 3.9777,
3.9777, 3.9764, 3.9945), PLN.OPEN = c(4.0288, 4.0316, 3.9966,
3.9954, 4.0124, 4.0124, 4.0124, 3.9977, 4.0029), PLN.CLOSE = c(4.0473,
4.0288, 4.0316, 3.9966, 3.99, 3.99, 3.99, 4.0124, 3.998),
RUB.HIGH = c(59.1241, 59.6218, 59.4953, 59.0979, 59.616,
59.616, 59.616, 60.1122, 60.4219), RUB.LOW = c(58.6965, 59.0965,
59.0077, 58.5903, 58.7511, 58.7511, 58.7511, 58.585, 60.0606
), RUB.OPEN = c(59.0818, 59.4892, 59.2045, 58.9665, 59.3936,
59.3936, 59.3936, 60.022, 60.1993), RUB.CLOSE = c(58.9703,
59.2339, 59.3441, 58.9062, 58.9622, 58.9622, 58.9622, 59.4135,
60.1237), TRY.HIGH = c(3.7239, 3.7591, 3.7503, 3.7071, 3.7587,
3.7587, 3.7587, 3.7743, 3.7946), TRY.LOW = c(3.6752, 3.6965,
3.6788, 3.6618, 3.6869, 3.6869, 3.6869, 3.7191, 3.7592),
TRY.OPEN = c(3.719, 3.7455, 3.6838, 3.6999, 3.7371, 3.7371,
3.7371, 3.7695, 3.7734), TRY.CLOSE = c(3.6822, 3.719, 3.7455,
3.6838, 3.6969, 3.6969, 3.6969, 3.7372, 3.7695), ZAR.HIGH = c(13.4843,
13.5265, 13.47, 13.415, 13.4984, 13.4984, 13.4984, 13.4772,
13.5324), ZAR.LOW = c(13.3549, 13.3554, 13.2864, 13.2339,
13.2083, 13.2083, 13.2083, 13.3191, 13.3621), ZAR.OPEN = c(13.4142,
13.4502, 13.3106, 13.265, 13.3995, 13.3995, 13.3995, 13.4724,
13.4751), ZAR.CLOSE = c(13.4035, 13.4139, 13.4502, 13.3167,
13.2667, 13.2667, 13.2667, 13.3995, 13.4719), BRL.HIGH = c(3.133,
3.1295, 3.1367, 3.1267, 3.1387, 3.1387, 3.1387, 3.1297, 3.1627
), BRL.LOW = c(3.1136, 3.1128, 3.1174, 3.107, 3.1056, 3.1056,
3.1056, 3.1082, 3.1277), BRL.OPEN = c(3.1228, 3.12, 3.1296,
3.1185, 3.13, 3.13, 3.13, 3.1193, 3.142), BRL.CLOSE = c(3.127,
3.1156, 3.1203, 3.1182, 3.123, 3.123, 3.123, 3.1214, 3.1282
), CLP.HIGH = c(650.5, 648.85, 648.53, 640.78, 645.15, 645.15,
645.15, 646.97, 648.77), CLP.LOW = c(643.58, 643.77, 640.97,
637.29, 635.8, 635.8, 635.8, 642, 645.26), CLP.OPEN = c(650.42,
644.96, 644.03, 639.75, 644.36, 644.36, 644.36, 642.68, 648.06
), CLP.CLOSE = c(646.27, 648.03, 647.45, 640.25, 638.88,
638.88, 638.88, 643.6, 647.32), COP.HIGH = c(2881, 2889.4,
2879.05, 2862.9, 2883.75, 2883.75, 2883.75, 2905.15, 2920.5
), COP.LOW = c(2858.21, 2851.8, 2851.27, 2842.8, 2844.61,
2844.61, 2844.61, 2871.73, 2903.25), COP.OPEN = c(2880.5,
2856, 2856.81, 2861.5, 2870.63, 2870.63, 2870.63, 2898, 2920.5
), COP.CLOSE = c(2860.81, 2880.69, 2856.27, 2856.4, 2851.81,
2851.81, 2851.81, 2877.9, 2905.33), MXN.HIGH = c(20.5377,
20.6891, 20.7469, 20.648, 20.6013, 20.6013, 20.6013, 20.7467,
20.8452), MXN.LOW = c(20.3062, 20.4437, 20.5432, 20.3093,
20.2845, 20.2845, 20.2845, 20.4369, 20.6601), MXN.OPEN = c(20.4792,
20.6208, 20.5618, 20.3722, 20.5602, 20.5602, 20.5602, 20.7074,
20.8325), MXN.CLOSE = c(20.3504, 20.4793, 20.6208, 20.5634,
20.3631, 20.3631, 20.3631, 20.5602, 20.7074), PEN.HIGH = c(3.279,
3.301, 3.297, 3.3055, 3.2615, 3.2615, 3.2615, 3.267, 3.2755
), PEN.LOW = c(3.2635, 3.2805, 3.287, 3.2665, 3.237, 3.237,
3.237, 3.243, 3.264), PEN.OPEN = c(3.2745, 3.299, 3.297,
3.268, 3.246, 3.246, 3.246, 3.267, 3.27), PEN.CLOSE = c(3.268,
3.2855, 3.296, 3.288, 3.26, 3.26, 3.26, 3.2485, 3.2705),
CNY.HIGH = c(6.8764, 6.8923, 6.8853, 6.8683, 6.8742, 6.8742,
6.8742, 6.8842, 6.8842), CNY.LOW = c(6.86, 6.8638, 6.8625,
6.8562, 6.864, 6.864, 6.864, 6.8715, 6.8715), CNY.OPEN = c(6.8702,
6.885, 6.8642, 6.8562, 6.864, 6.864, 6.864, 6.8715, 6.8715
), CNY.CLOSE = c(6.8692, 6.8638, 6.8853, 6.863, 6.8672, 6.8672,
6.8672, 6.884, 6.884), IDR.HIGH = c(13325, 13343, 13338,
13350, 13375, 13375, 13375, 13380, 13387), IDR.LOW = c(13290,
13325, 13313, 13319, 13340, 13340, 13340, 13350, 13338),
IDR.OPEN = c(13315, 13338, 13325, 13330, 13370, 13370, 13370,
13373, 13340), IDR.CLOSE = c(13297, 13327, 13329, 13320,
13345, 13345, 13345, 13353, 13372), INR.HIGH = c(67.065,
67.375, 67.4413, 67.2325, 67.42, 67.42, 67.42, 67.5088, 67.6825
), INR.LOW = c(66.845, 67.185, 67.2888, 67.1475, 67.2863,
67.2863, 67.2863, 67.3725, 67.465), INR.OPEN = c(67.0475,
67.3712, 67.2938, 67.2175, 67.345, 67.345, 67.345, 67.4238,
67.645), INR.CLOSE = c(66.85, 67.1888, 67.41, 67.2188, 67.3188,
67.3188, 67.3188, 67.3737, 67.4738), KRW.HIGH = c(1148.5,
1147.9, 1145.65, 1140.25, 1149.07, 1149.07, 1149.07, 1154.63,
1158.9), KRW.LOW = c(1142.85, 1142.07, 1135, 1135.75, 1143.5,
1143.5, 1143.5, 1145.47, 1151.35), KRW.OPEN = c(1146, 1145,
1135, 1140.03, 1143.5, 1143.5, 1143.5, 1154, 1151.4), KRW.CLOSE = c(1146.15,
1147.07, 1144.18, 1138.07, 1147.4, 1147.4, 1147.4, 1146.93,
1158.22), MYR.HIGH = c(4.4447, 4.4447, 4.4418, 4.4288, 4.428,
4.428, 4.428, 4.4415, 4.4305), MYR.LOW = c(4.4352, 4.4352,
4.4275, 4.4198, 4.4213, 4.4213, 4.4213, 4.4218, 4.426), MYR.OPEN = c(4.4402,
4.4402, 4.4275, 4.4268, 4.4213, 4.4213, 4.4213, 4.4305, 4.4279
), MYR.CLOSE = c(4.4395, 4.4395, 4.4353, 4.4265, 4.428, 4.428,
4.428, 4.423, 4.4285), SGD.HIGH = c(1.4206, 1.4197, 1.4199,
1.4128, 1.4155, 1.4155, 1.4155, 1.4134, 1.4168), SGD.LOW = c(1.4136,
1.4137, 1.4087, 1.4054, 1.4055, 1.4055, 1.4055, 1.4079, 1.4084
), SGD.OPEN = c(1.4166, 1.4181, 1.4092, 1.4095, 1.4114, 1.4114,
1.4114, 1.4129, 1.4098), SGD.CLOSE = c(1.4205, 1.4166, 1.418,
1.4092, 1.4099, 1.4099, 1.4099, 1.4114, 1.4128), PHP.HIGH = c(49.985,
49.87, 49.783, 49.81, 49.885, 49.885, 49.885, 49.83, 49.838
), PHP.LOW = c(49.767, 49.645, 49.62, 49.692, 49.735, 49.735,
49.735, 49.723, 49.695), PHP.OPEN = c(49.83, 49.8, 49.705,
49.725, 49.775, 49.775, 49.775, 49.78, 49.735), PHP.CLOSE = c(49.873,
49.645, 49.78, 49.695, 49.78, 49.78, 49.78, 49.745, 49.83
), THB.HIGH = c(35.065, 35.079, 35.062, 35.08, 35.118, 35.118,
35.118, 35.135, 35.215), THB.LOW = c(34.99, 35.01, 34.99,
34.954, 35.035, 35.035, 35.035, 35.06, 35.105), THB.OPEN = c(35.065,
35.03, 35.04, 35.08, 35.06, 35.06, 35.06, 35.109, 35.195),
THB.CLOSE = c(35.004, 35.05, 35.052, 35.011, 35.07, 35.07,
35.07, 35.072, 35.122)), .Names = c("Date", "EUR.HIGH", "EUR.LOW",
"EUR.OPEN", "EUR.LAST", "JPY.HIGH", "JPY.LOW", "JPY.CLOSE", "JPY.OPEN",
"JPY.LAST", "GBP.HIGH", "GBP.LOW", "GBP.OPEN", "GBP.LAST", "AUD.HIGH",
"AUD.LOW", "AUD.OPEN", "AUD.LAST", "CAD.HIGH", "CAD.LOW", "CAD.OPEN",
"CAD.LAST", "NZD.HIGH", "NZD.LOW", "NZD.OPEN", "NZD.LAST", "SEK.HIGH",
"SEK.LOW", "SEK.OPEN", "SEK.CLOSE", "NOK.HIGH", "NOK.LOW", "NOK.OPEN",
"NOK.CLOSE", "CZK.HIGH", "CZK.LOW", "CZK.OPEN", "CZK.CLOSE",
"HUF.HIGH", "HUF.LOW", "HUF.OPEN", "HUF.CLOSE", "ILS.HIGH", "ILS.LOW",
"ILS.OPEN", "ILS.CLOSE", "PLN.HIGH", "PLN.LOW", "PLN.OPEN", "PLN.CLOSE",
"RUB.HIGH", "RUB.LOW", "RUB.OPEN", "RUB.CLOSE", "TRY.HIGH", "TRY.LOW",
"TRY.OPEN", "TRY.CLOSE", "ZAR.HIGH", "ZAR.LOW", "ZAR.OPEN", "ZAR.CLOSE",
"BRL.HIGH", "BRL.LOW", "BRL.OPEN", "BRL.CLOSE", "CLP.HIGH", "CLP.LOW",
"CLP.OPEN", "CLP.CLOSE", "COP.HIGH", "COP.LOW", "COP.OPEN", "COP.CLOSE",
"MXN.HIGH", "MXN.LOW", "MXN.OPEN", "MXN.CLOSE", "PEN.HIGH", "PEN.LOW",
"PEN.OPEN", "PEN.CLOSE", "CNY.HIGH", "CNY.LOW", "CNY.OPEN", "CNY.CLOSE",
"IDR.HIGH", "IDR.LOW", "IDR.OPEN", "IDR.CLOSE", "INR.HIGH", "INR.LOW",
"INR.OPEN", "INR.CLOSE", "KRW.HIGH", "KRW.LOW", "KRW.OPEN", "KRW.CLOSE",
"MYR.HIGH", "MYR.LOW", "MYR.OPEN", "MYR.CLOSE", "SGD.HIGH", "SGD.LOW",
"SGD.OPEN", "SGD.CLOSE", "PHP.HIGH", "PHP.LOW", "PHP.OPEN", "PHP.CLOSE",
"THB.HIGH", "THB.LOW", "THB.OPEN", "THB.CLOSE"), row.names = c(NA,
-9L), class = "data.frame")
答案 0 :(得分:1)
试试这个:
cur<-c('EUR', 'JPY', 'GBP', 'CHF', 'AUD', 'CAD', 'NZD', 'SEK', 'NOK', 'CZK', 'HUF', 'ILS',
'PLN', 'RUB', 'TRY', 'ZAR', 'BRL', 'CLP', 'COP', 'MXN', 'PEN', 'CNY', 'IDR', 'INR',
'KRW', 'MYR', 'SGD', 'PHP', 'THB')
cur <- paste0("^", cur)
i <- sapply(cur, function(y) grep(y,names(x)), USE.NAMES = F)
MergedData <- sapply(i, function(i) x[c(1,i)])
> MergedData[[21]]
Date PEN.HIGH PEN.LOW PEN.OPEN PEN.CLOSE
1 2017-02-08 20:00:00 3.2790 3.2635 3.2745 3.2680
2 2017-02-07 20:00:00 3.3010 3.2805 3.2990 3.2855
3 2017-02-06 20:00:00 3.2970 3.2870 3.2970 3.2960
4 2017-02-05 20:00:00 3.3055 3.2665 3.2680 3.2880
5 2017-02-04 20:00:00 3.2615 3.2370 3.2460 3.2600
6 2017-02-03 20:00:00 3.2615 3.2370 3.2460 3.2600
7 2017-02-02 20:00:00 3.2615 3.2370 3.2460 3.2600
8 2017-02-01 20:00:00 3.2670 3.2430 3.2670 3.2485
9 2017-01-31 20:00:00 3.2755 3.2640 3.2700 3.2705