我有一個看起來像這樣的數據:通過使用sapply和grep按行名分組數據來組織數據?
EUR.DATE EUR.HIGH EUR.LOW EUR.CLOSE EUR.OPEN EUR.20D EUR.50D JPY.DATE JPY. HIGH...
01/01 1.3133, 1.3128, 1.3132, 1.3182, 1.327, 1.3282, 1.3294, 01/01 112...
樣品dput連接如下:
structure(list(EUR.DATE = structure(c(1409720400, 1409634000,
1409547600, 1409288400, 1409202000), class = c("POSIXct", "POSIXt"
), tzone = ""), EUR.HIGH = c(1.316, 1.3137, 1.3145, 1.3196, 1.3221
), EUR.LOW = c(1.3122, 1.311, 1.3119, 1.3132, 1.316), EUR.OPEN = c(1.3133,
1.3128, 1.3138, 1.3182, 1.3193), EUR.CLOSE = c(1.315, 1.3133,
1.3128, 1.3132, 1.3182), EUR.20D = c(1.327, 1.3282, 1.3294, 1.3309,
1.3323), EUR.50D = c(1.3427, 1.3436, 1.3446, 1.3455, 1.3465),
JPY.DATE = structure(c(1409720400, 1409634000, 1409547600,
1409288400, 1409202000), class = c("POSIXct", "POSIXt"), tzone = ""),
JPY.HIGH = c(105.31, 105.21, 104.35, 104.11, 103.92), JPY.LOW = c(104.74,
104.3, 104.06, 103.66, 103.56), JPY.OPEN = c(105.09, 104.35,
104.08, 103.72, 103.88), JPY.CLOSE = c(104.79, 105.09, 104.35,
104.09, 103.72), JPY.20D = c(103.34, 103.21, 103.09, 103,
102.92), JPY.50D = c(102.43, 102.37, 102.31, 102.26, 102.22
), GBP.DATE = structure(c(1409720400, 1409634000, 1409547600,
1409288400, 1409202000), class = c("POSIXct", "POSIXt"), tzone = ""),
GBP.HIGH = c(1.6497, 1.6615, 1.6644, 1.6614, 1.6614), GBP.LOW = c(1.644,
1.6468, 1.6586, 1.6563, 1.6567), GBP.OPEN = c(1.647, 1.6608,
1.6593, 1.6586, 1.6576), GBP.CLOSE = c(1.6461, 1.647, 1.6608,
1.6598, 1.6586), GBP.20D = c(1.6639, 1.6659, 1.668, 1.6692,
1.6703), GBP.50D = c(1.6882, 1.6892, 1.6903, 1.6911, 1.6919
), CHF.DATE = structure(c(1409720400, 1409634000, 1409547600,
1409288400, 1409202000), class = c("POSIXct", "POSIXt"), tzone = ""),
CHF.HIGH = c(0.9201, 0.9212, 0.9198, 0.9184, 0.9162), CHF.LOW = c(0.9176,
0.9188, 0.9177, 0.9139, 0.9126), CHF.HIGH.1 = c(0.919, 0.9196,
0.9179, 0.9151, 0.9148), CHF.OPEN = c(0.9177, 0.919, 0.9196,
0.9182, 0.9151), CHF.CLOSE = c(0.9118, 0.9113, 0.9108, 0.9102,
0.9096), CHF50D = c(0.9037, 0.9032, 0.9027, 0.9022, 0.9017
), AUD.DATE = structure(c(1409720400, 1409634000, 1409547600,
1409288400, 1409202000), class = c("POSIXct", "POSIXt"), tzone = ""),
AUD.HIGH = c(0.9351, 0.9337, 0.9352, 0.9363, 0.9374), AUD.LOW = c(0.9263,
0.9268, 0.9319, 0.9332, 0.9332), AUD.OPEN = c(0.9274, 0.9332,
0.9329, 0.9357, 0.9337), AUD.CLOSE = c(0.9347, 0.9274, 0.9332,
0.9339, 0.9357), AUD.20D = c(0.9307, 0.9308, 0.9309, 0.9309,
0.9308), AUD.50D = c(0.9354, 0.9355, 0.9357, 0.9358, 0.9359
), CAD.DATE = structure(c(1409720400, 1409634000, 1409547600,
1409288400, 1409202000), class = c("POSIXct", "POSIXt"), tzone = ""),
CAD.HIGH = c(1.0943, 1.0935, 1.0878, 1.0879, 1.0869), CAD.LOW = c(1.0871,
1.0867, 1.0857, 1.0811, 1.0837), CAD.OPEN = c(1.0929, 1.0871,
1.0869, 1.0861, 1.0865), CAD.CLOSE = c(1.0888, 1.0929, 1.0871,
1.0878, 1.0861), CAD.20D = c(1.0918, 1.092, 1.0921, 1.0923,
1.0925), CAD.50D = c(1.082, 1.0817, 1.0813, 1.081, 1.0808
), NZD.DATE = structure(c(1409720400, 1409634000, 1409547600,
1409288400, 1409202000), class = c("POSIXct", "POSIXt"), tzone = ""),
NZD.HIGH = c(0.8332, 0.8391, 0.8389, 0.8398, 0.8408), NZD.LOW = c(0.8288,
0.8292, 0.8348, 0.8356, 0.8364), NZD.OPEN = c(0.8316, 0.8377,
0.8356, 0.8383, 0.8374), NZD.CLOSE = c(0.8326, 0.8316, 0.8377,
0.8362, 0.8383), NZD.20D = c(0.8408, 0.8416, 0.8424, 0.8431,
0.8439), NZD.50D = c(0.8568, 0.8576, 0.8583, 0.859, 0.8596
), SEK.DATE = structure(c(1409720400, 1409634000, 1409547600,
1409288400, 1409202000), class = c("POSIXct", "POSIXt"), tzone = ""),
SEK.HIGH = c(7.0249, 7.0242, 7.0346, 6.9967, 6.9859), SEK.LOW = c(6.9894,
6.9945, 6.9795, 6.943, 6.9279), SEK.CLOSE = c(7.0167, 7.0045,
6.9795, 6.9717, 6.9495), SEK.OPEN = c(7.0038, 7.0167, 7.0045,
6.9918, 6.9717), SEK.20D = c(6.9201, 6.9146, 6.908, 6.9019,
6.8953), SEK.50D = c(6.8588, 6.8534, 6.8476, 6.8414, 6.8358
), NOK.DATE = structure(c(1409720400, 1409634000, 1409547600,
1409288400, 1409202000), class = c("POSIXct", "POSIXt"), tzone = ""),
NOK.HIGH = c(6.2314, 6.2054, 6.2094, 6.2021, 6.1938), NOK.LOW = c(6.1855,
6.1832, 6.1776, 6.1697, 6.166), NOK.OPEN = c(6.1874, 6.1919,
6.1939, 6.1795, 6.1804), NOK.CLOSE = c(6.2179, 6.1874, 6.1919,
6.1975, 6.1795), NOK.20D = c(7.5563, 7.5738, 7.5975, 7.5975,
7.5975), NOK.50D = c(6.1941, 6.1924, 6.1909, 6.1892, 6.1876
), CZK.DATE = structure(c(1409720400, 1409634000, 1409547600,
1409288400, 1409202000), class = c("POSIXct", "POSIXt"), tzone = ""),
CZK.HIGH = c(21.1358, 21.2163, 21.1489, 21.1551, 21.1434),
CZK.LOW = c(21.017, 21.1095, 21.0906, 21.013, 20.9375), CZK.OPEN = c(21.1247,
21.1359, 21.1065, 21.1101, 20.9924), CZK.CLOSE = c(21.0528,
21.1247, 21.1359, 21.1194, 21.11), CZK.20D = c(21.1247, 21.1359,
21.1065, 21.1101, 20.9924), CZK.50D = c(21.0528, 21.1247,
21.1359, 21.1194, 21.11), HUF.DATE = structure(c(1409720400,
1409634000, 1409547600, 1409288400, 1409202000), class = c("POSIXct",
"POSIXt"), tzone = ""), HUF.HIGH = c(240.5, 240.92, 240.55,
240.35, 240.3), HUF.LOW = c(238.35, 239.2, 238.96, 238.51,
236.56), HUF.HIGH.1 = c(240.31, 239.49, 240.09, 239.06, 237
), HUF.LOW.1 = c(238.76, 240.31, 239.49, 239.77, 239.06),
HUF.20D = c(236.55, 236.41, 236.2, 235.9, 235.57), HUF.50D = c(232.32,
232.05, 231.74, 231.44, 231.14), ILS.DATE = structure(c(1409115600,
1409029200, 1408942800, 1408683600, 1408597200), class = c("POSIXct",
"POSIXt"), tzone = ""), ILS.HIGH = c(3.576, 3.588, 3.5775,
3.543, 3.5505), ILS.LOW = c(3.5615, 3.563, 3.534, 3.514,
3.5185), ILS.HIGH.1 = c(3.5703, 3.579, 3.5345, 3.525, 3.545
), ILS.LOW.1 = c(3.563, 3.57, 3.5705, 3.533, 3.526), ILS.20D = c(3.4915,
3.4849, 3.4779, 3.4707, 3.4654), ILS.50D = c(3.4526, 3.4503,
3.4481, 3.4458, 3.4442), PLN.DATE = structure(c(1409720400,
1409634000, 1409547600, 1409288400, 1409202000), class = c("POSIXct",
"POSIXt"), tzone = ""), PLN.HIGH = c(3.2095, 3.2148, 3.213,
3.2164, 3.2101), PLN.LOW = c(3.182, 3.2005, 3.2003, 3.193,
3.1767), PLN.HIGH.1 = c(3.2058, 3.2017, 3.2089, 3.2036, 3.1831
), PLN.LOW.1 = c(3.1909, 3.2058, 3.2017, 3.2056, 3.2036),
PLN.20D = c(3.1628, 3.1603, 3.1565, 3.152, 3.1475), PLN.50D = c(3.1061,
3.103, 3.1, 3.0971, 3.0943), RUB.DATE = structure(c(1406696400,
1406610000, 1406523600, 1406264400, 1406178000), class = c("POSIXct",
"POSIXt"), tzone = ""), RUB.HIGH = c(35.9303, 35.758, 35.56,
35.1373, 35.1463), RUB.LOW = c(35.4986, 35.5684, 35.2557,
35.0105, 34.9402), RUB.HIGH.1 = c(35.9106, 35.6705, 35.2578,
35.0906, 35.0165), RUB.LOW.1 = c(35.614, 35.6753, 35.5252,
35.0817, 35.0413), RUB.20D = c(34.7338, 34.6695, 34.6022,
34.5282, 34.4608), RUB.50D = c(34.5266, 34.5052, 34.4821,
34.4666, 34.4596), TRY.DATE = structure(c(1409720400, 1409634000,
1409547600, 1409288400, 1409202000), class = c("POSIXct",
"POSIXt"), tzone = ""), TRY.HIGH = c(2.1742, 2.1782, 2.1657,
2.1652, 2.1727), TRY.LOW = c(2.1563, 2.1595, 2.1569, 2.1537,
2.1536), TRY.LOW.1 = c(2.1725, 2.1602, 2.1619, 2.1581, 2.154
), TRY.HIGH.1 = c(2.1572, 2.1727, 2.1603, 2.163, 2.1581),
TRY.20D = c(2.1637, 2.1638, 2.1628, 2.1613, 2.1599), TRY.50D = c(2.1392,
2.1387, 2.1381, 2.1376, 2.1372), ZAR.DATE = structure(c(1409720400,
1409634000, 1409547600, 1409288400, 1409202000), class = c("POSIXct",
"POSIXt"), tzone = ""), ZAR.HIGH = c(10.748, 10.7486, 10.6852,
10.6735, 10.6868), ZAR.LOW = c(10.6707, 10.6713, 10.6448,
10.5904, 10.6034), ZAR.HIGH.1 = c(10.7402, 10.6747, 10.6636,
10.6511, 10.6129), ZAR.LOW.1 = c(10.6886, 10.7402, 10.6747,
10.6666, 10.6511), ZAR.20D = c(10.6601, 10.6608, 10.6616,
10.6606, 10.6612), ZAR.50D = c(10.6612, 10.6593, 10.6573,
10.6558, 10.6554), BRL.DATE = structure(c(1406869200, 1406782800,
1406696400, 1406610000, 1406523600), class = c("POSIXct",
"POSIXt"), tzone = ""), BRL.HIGH = c(2.282, 2.2748, 2.26,
2.2321, 2.2348), BRL.LOW = c(2.2508, 2.253, 2.233, 2.224,
2.2222), BRL.HIGH.1 = c(2.2723, 2.2551, 2.2366, 2.2276, 2.2294
), BRL.LOW.1 = c(2.2573, 2.2638, 2.2456, 2.2315, 2.2225),
BRL.20D = c(2.2279, 2.2261, 2.2234, 2.2224, 2.2209), BRL.50D = c(2.2301,
2.2293, 2.2282, 2.2276, 2.2271), CLP.DATE = structure(c(1406091600,
1406005200, 1405918800, 1405659600, 1405573200), class = c("POSIXct",
"POSIXt"), tzone = ""), CLP.HIGH = c(568.55, 568.55, 568.55,
568.56, 565.75), CLP.LOW = c(562.82, 562.16, 559.45, 564.55,
557.75), CLP.HIGH.1 = c(564.51, 563.91, 568.55, 564.8, 559.51
), CLP.LOW.1 = c(563.57, 564.65, 564.57, 567.61, 564.54),
CLP.20D = c(555.58, 554.99, 554.39, 553.96, 553.43), CLP.50D = c(554.1,
553.87, 553.69, 553.47, 553.42), COP.DATE = structure(c(1406264400,
1406178000, 1406091600, 1406005200, 1405918800), class = c("POSIXct",
"POSIXt"), tzone = ""), COP.HIGH = c(1849.5, 1847.5, 1847.7,
1856.5, 1868.5), COP.LOW = c(1846.5, 1842.76, 1845.5, 1856.5,
1866.7), COP.OPEN = c(1847.7, 1842.76, 1847.7, 1856.5, 1866.7
), COP.CLOSE = c(1849.5, 1846.5, 1846.5, 1856.5, 1868.5),
COP.20D = c(1860.69, 1862.74, 1864.64, 1866.49, 1867.79),
COP.50D = c(1884.4, 1885.48, 1886.76, 1888.04, 1889.27),
MXN.DATE = structure(c(1409720400, 1409634000, 1409547600,
1409288400, 1409202000), class = c("POSIXct", "POSIXt"), tzone = ""),
MXN.HIGH = c(13.1196, 13.1476, 13.0999, 13.1009, 13.1309),
MXN.LOW = c(13.0732, 13.0912, 13.0575, 13.0464, 13.0689),
MXN.HIGH.1 = c(13.109, 13.0959, 13.0633, 13.0856, 13.0844
), MXN.LOW.1 = c(13.0986, 13.109, 13.0959, 13.0847, 13.0856
), MXN.20D = c(13.1203, 13.1271, 13.1363, 13.1413, 13.1462
), MXN.50D = c(13.0609, 13.059, 13.058, 13.0567, 13.0548),
PEN.DATE = structure(c(1407214800, 1407128400, 1406869200,
1406782800, 1406696400), class = c("POSIXct", "POSIXt"), tzone = ""),
PEN.HIGH = c(2.816, 2.8091, 2.799, 2.802, 2.7935), PEN.LOW = c(2.8089,
2.7973, 2.795, 2.7926, 2.7866), PEN.LOW.1 = c(2.8128, 2.7983,
2.798, 2.793, 2.7873), PEN.HIGH.1 = c(2.8149, 2.808, 2.7982,
2.8011, 2.7895), PEN.20D = c(2.79, 2.7881, 2.7862, 2.7851,
2.7846), PEN.50D = c(2.7899, 2.7893, 2.7889, 2.7887, 2.7885
), CNY.DATE = structure(c(1404277200, 1404190800, 1404104400,
1403845200, 1403758800), class = c("POSIXct", "POSIXt"), tzone = ""),
CNY.HIGH = c(6.2134, 6.2066, 6.2167, 6.2258, 6.2337), CNY.LOW = c(6.198,
6.1973, 6.2, 6.216, 6.2243), CNY.OPEN = c(6.198, 6.2023,
6.2166, 6.2258, 6.229), CNY.CLOSE = c(6.2101, 6.2009, 6.2033,
6.2179, 6.2248), CNY.20D = c(6.2256, 6.2276, 6.2303, 6.2325,
6.2336), CNY.50D = c(6.2344, 6.2347, 6.2351, 6.2354, 6.2355
), IDR.DATE = structure(c(1405314000, 1405054800, 1404968400,
1404795600, 1404709200), class = c("POSIXct", "POSIXt"), tzone = ""),
IDR.HIGH = c(11670, 11637, 11595, 11781, 11870), IDR.LOW = c(11609,
11579, 11513, 11605, 11683), IDR.HIGH.1 = c(11610, 11605,
11571, 11708, 11829), IDR.LOW.1 = c(11670, 11590, 11580,
11630, 11710), IDR.20D = c(11869, 11876, 11886, 11897, 11906
), IDR.50D = c(11724, 11722, 11722, 11722, 11722), INR.DATE = structure(c(1402981200,
1402894800, 1402635600, 1402549200, 1402462800), class = c("POSIXct",
"POSIXt"), tzone = ""), INR.HIGH = c(60.525, 60.225, 59.8,
59.3625, 59.36), INR.LOW = c(60.35, 59.84, 59.3037, 59.325,
59.325), INR.HIGH.1 = c(59.1977, 59.1287, 59.0502, 59.0007,
59.0025), INR.LOW.1 = c(59.7126, 59.7186, 59.7186, 59.7327,
59.7634), X67.7266 = c(62.8711, 62.8345, 62.7951, 62.7951,
62.7951), INR.20D = c(1031.35, 1029.65, 1030.57, 1032.55,
1041.23), INR.50D = c(1031, 1028.9, 1029.28, 1030.5, 1037
), KRW.DATE = structure(c(1407992400, 1407906000, 1407819600,
1407733200, 1407474000), class = c("POSIXct", "POSIXt"), tzone = ""),
KRW.HIGH = c(1028.77, 1029.18, 1029.31, 1029.35, 1028.73),
KRW.LOW = c(1021.92, 1021.91, 1021.78, 1021.72, 1021.58),
KRW.HIGH.1 = c(3.205, 3.2137, 3.1978, 3.184, 3.1748), KRW.LOW.1 = c(3.205,
3.208, 3.1805, 3.1745, 3.1745), KRW.20D = c(3.1813, 3.1806,
3.1793, 3.1793, 3.1805), KRW.50D = c(3.2015, 3.2017, 3.2017,
3.202, 3.2027)), .Names = c("EUR.DATE", "EUR.HIGH", "EUR.LOW",
"EUR.OPEN", "EUR.CLOSE", "EUR.20D", "EUR.50D", "JPY.DATE", "JPY.HIGH",
"JPY.LOW", "JPY.OPEN", "JPY.CLOSE", "JPY.20D", "JPY.50D", "GBP.DATE",
"GBP.HIGH", "GBP.LOW", "GBP.OPEN", "GBP.CLOSE", "GBP.20D", "GBP.50D",
"CHF.DATE", "CHF.HIGH", "CHF.LOW", "CHF.HIGH.1", "CHF.OPEN",
"CHF.CLOSE", "CHF50D", "AUD.DATE", "AUD.HIGH", "AUD.LOW", "AUD.OPEN",
"AUD.CLOSE", "AUD.20D", "AUD.50D", "CAD.DATE", "CAD.HIGH", "CAD.LOW",
"CAD.OPEN", "CAD.CLOSE", "CAD.20D", "CAD.50D", "NZD.DATE", "NZD.HIGH",
"NZD.LOW", "NZD.OPEN", "NZD.CLOSE", "NZD.20D", "NZD.50D", "SEK.DATE",
"SEK.HIGH", "SEK.LOW", "SEK.CLOSE", "SEK.OPEN", "SEK.20D", "SEK.50D",
"NOK.DATE", "NOK.HIGH", "NOK.LOW", "NOK.OPEN", "NOK.CLOSE", "NOK.20D",
"NOK.50D", "CZK.DATE", "CZK.HIGH", "CZK.LOW", "CZK.OPEN", "CZK.CLOSE",
"CZK.20D", "CZK.50D", "HUF.DATE", "HUF.HIGH", "HUF.LOW", "HUF.HIGH.1",
"HUF.LOW.1", "HUF.20D", "HUF.50D", "ILS.DATE", "ILS.HIGH", "ILS.LOW",
"ILS.HIGH.1", "ILS.LOW.1", "ILS.20D", "ILS.50D", "PLN.DATE",
"PLN.HIGH", "PLN.LOW", "PLN.HIGH.1", "PLN.LOW.1", "PLN.20D",
"PLN.50D", "RUB.DATE", "RUB.HIGH", "RUB.LOW", "RUB.HIGH.1", "RUB.LOW.1",
"RUB.20D", "RUB.50D", "TRY.DATE", "TRY.HIGH", "TRY.LOW", "TRY.LOW.1",
"TRY.HIGH.1", "TRY.20D", "TRY.50D", "ZAR.DATE", "ZAR.HIGH", "ZAR.LOW",
"ZAR.HIGH.1", "ZAR.LOW.1", "ZAR.20D", "ZAR.50D", "BRL.DATE",
"BRL.HIGH", "BRL.LOW", "BRL.HIGH.1", "BRL.LOW.1", "BRL.20D",
"BRL.50D", "CLP.DATE", "CLP.HIGH", "CLP.LOW", "CLP.HIGH.1", "CLP.LOW.1",
"CLP.20D", "CLP.50D", "COP.DATE", "COP.HIGH", "COP.LOW", "COP.OPEN",
"COP.CLOSE", "COP.20D", "COP.50D", "MXN.DATE", "MXN.HIGH", "MXN.LOW",
"MXN.HIGH.1", "MXN.LOW.1", "MXN.20D", "MXN.50D", "PEN.DATE",
"PEN.HIGH", "PEN.LOW", "PEN.LOW.1", "PEN.HIGH.1", "PEN.20D",
"PEN.50D", "CNY.DATE", "CNY.HIGH", "CNY.LOW", "CNY.OPEN", "CNY.CLOSE",
"CNY.20D", "CNY.50D", "IDR.DATE", "IDR.HIGH", "IDR.LOW", "IDR.HIGH.1",
"IDR.LOW.1", "IDR.20D", "IDR.50D", "INR.DATE", "INR.HIGH", "INR.LOW",
"INR.HIGH.1", "INR.LOW.1", "X67.7266", "INR.20D", "INR.50D",
"KRW.DATE", "KRW.HIGH", "KRW.LOW", "KRW.HIGH.1", "KRW.LOW.1",
"KRW.20D", "KRW.50D"), row.names = 647:651, class = "data.frame")
而且我想我的數據使用下面的代碼組成有序dataframes列表('EUR','JPY','GBP','CHF','AUD','CAD','NZD','SEK','NOK','CZK'的貨幣: ,'HUF','ILS', 'PLN','RUB','TRY','ZAR','BRL','CLP','COP','MXN','筆','CNY',' 'IDR','INR', 'KRW') i < - sapply(cur,function(y)grep(paste0(「^」,y),names(x)),USE.NAMES = FALSE) MergedData < - sapply(i,function(i)x [i ])
其中x來自樣本dput。然而,這將返回
MergedData[[1]]
Eur.Date Eur.High Eur.Low Eur.Open Eur.Close Eur.20D Eur.50D
01/01
MergedData[[2]]
JPY.Date JPY.High JPY.Low JPY.Open JPY.Close
但相反的代碼返回
$Eur.Date
[1] "2017-02-23 CST" "2017-02-22 CST" "2017-02-21 CST" "2017-02-20 CST" "2017-02-17 CST"
等。直到我在Excel中更改了一些錯誤命名的變量(我的原始數據來自Excel)之前,我的代碼才工作。幫助將不勝感激。
您dput實際上並不包括人名幣。你是否手動將它們附加到你的data.frame?此外,我不太清楚這段代碼是做什麼的,我的< - sapply(cur,function(y)grep(paste0(「^」,y),names(x)),USE.NAMES = FALSE) MergedData < - sapply(i,function(i)x [i])'在'x [i]'中哪個'x'是指最後一行? – useR
@useR哦,我很抱歉。 x實際上是指我的原始進口。我也意識到,在我匆忙中,我已經附加了轉換數據的輸入。編輯並修復了這個問題。感謝您指出了這一點! – Nikitau