Ordering column based on some strings - r

I have a data in columns I have characters part of which are TRG1, TRG2, TRG3, TRG4 and TRG5
How I can order this data frame based on TRG so that first TRG1 ....finally TRG5 are placed in the columns?
My data is
> dput(head(result))
structure(list(`Sample Name` = c("ACTB", "ATP5F1", "DDX5", "EEF1G",
"GAPDH", "NCL"), `31-10TRG3R` = c(15723, 1682, 16598, 17240,
38686, 10670), `31-11TRG4R` = c(24846, 3294, 25522, 38914, 73022,
14628), `31-12TRG4R` = c(7812, 1326, 5750, 9204, 12352, 5489),
`31-13TRG1R` = c(15332, 1162, 18268, 20875, 62257, 10614),
`31-14TRG4R` = c(7644, 1435, 16822, 13731, 26244, 10548),
`31-15TRG4R` = c(6501, 947, 10320, 7285, 10538, 4638), `31-16TRG4R` = c(5428,
825, 11789, 12018, 6812, 5954), `31-17TRG3R` = c(10074, 1056,
7966, 12489, 26819, 6404), `31-18TRG1R` = c(12487, 567, 13945,
16474, 43309, 11831), `31-19TRG4R` = c(5211, 917, 9144, 8024,
8200, 3935), `31-1TRG3R` = c(9928, 1112, 5726, 6227, 12942,
3644), `31-21TRG3R` = c(6806, 1460, 7472, 12420, 46378, 5871
), `31-22TRG3R` = c(4834, 640, 9807, 7082, 14823, 4594),
`31-23TRG1R` = c(3156, 765, 18034, 18982, 17237, 18880),
`31-24TRG4R` = c(6990, 761, 4440, 2833, 8150, 1340), `31-25TRG2R` = c(60621,
6290, 47502, 135948, 233717, 37583), `31-26TRG3R` = c(4198,
718, 2564, 3830, 5790, 1258), `31-27TRG2R` = c(10815, 1010,
8694, 11868, 18684, 5706), `31-28TRG4R` = c(7980, 1343, 7342,
9874, 14286, 4255), `31-29TRG1R` = c(3854, 748, 9314, 9132,
25546, 7852), `31-2TRG1R` = c(7653, 1495, 12238, 12568, 11296,
11256), `31-30TRG5R` = c(24358, 2091, 15594, 26998, 91442,
20914), `31-31TRG4R` = c(6796, 940, 12752, 11642, 41967,
12922), `31-32TRG2R` = c(127379, 11541, 90020, 74881, 234454,
51464), `31-33TRG1R` = c(4139, 338, 8260, 8650, 13916, 8000
), `31-34TRG3R` = c(37303, 2998, 22122, 30431, 51981, 11737
), `31-35TRG4R` = c(32279, 2718, 42178, 36956, 115962, 21194
), `31-36TRG3R` = c(12424, 1134, 8177, 14462, 20147, 6648
), `31-37TRG2R` = c(7031, 690, 8208, 17495, 28514, 7058),
`31-38TRG3R` = c(3645, 698, 16117, 11122, 25739, 7031), `31-39TRG3R` = c(28273,
2169, 14697, 20890, 68353, 25293), `31-3TRG4R` = c(9250,
1335, 24776, 14674, 31266, 8732), `31-40TRG1R` = c(28858,
2100, 26910, 43331, 104235, 19544), `31-41TRG1R` = c(13980,
1184, 13204, 13624, 47414, 11870), `31-42TRG2R` = c(22697,
2401, 16326, 22962, 40136, 11796), `31-43TRG3R` = c(13820,
797, 16245, 7827, 38292, 6206), `31-44TRG2R` = c(9477, 1244,
7140, 6580, 12457, 5176), `31-45TRG3R` = c(12182, 573, 2818,
3699, 4365, 1639), `31-46TRG1R` = c(5438, 997, 9226, 26045,
17740, 8628), `31-47TRG3R` = c(14419, 1927, 7350, 10375,
15736, 3415), `31-48TRG2R` = c(8758, 1002, 8044, 6677, 17354,
7355), `31-49TRG4R` = c(7738, 792, 13920, 15589, 42536, 14056
), `31-4TRG3R` = c(9947, 1115, 7267, 5957, 13831, 2793),
`31-50TRG4R` = c(6660, 701, 4092, 16796, 7958, 2408), `31-51TRG2R` = c(151880,
16572, 93610, 110556, 303604, 57029), `31-52TRG2R` = c(7184,
1396, 12785, 11124, 13050, 8934), `31-53TRG2R` = c(9012,
1118, 7786, 11482, 19512, 9143), `31-5TRG2R` = c(5479, 440,
8913, 7103, 15886, 5801), `31-6TRG4R` = c(6716, 677, 8812,
12184, 14380, 7684), `31-7TRG3R` = c(16192, 1155, 9405, 11930,
30034, 7726), `31-8TRG1R` = c(11408, 1007, 11396, 20424,
38188, 9570), `31-9TRG1R` = c(9468, 812, 10774, 8504, 15464,
4606)), row.names = c(NA, 6L), class = "data.frame")
>

May be, we extract the digits after the 'TRG' and use that in order
result2 <- result[c(1, order(as.numeric(sub(".*TRG(\\d+)\\D+", "\\1",
names(result)[-1])))+1)]

Related

how to extract fitted values in a forecast model after multiple model simulations

This is my original df and fitted model
library(tsibble)
library(tibble)
library(ISOweek)
library(fable)
library(forecast)
library(fpp3)
library(dplyr)
library(tidyverse)
Original.df <- structure(list(YearWeek = c("201901", "201902", "201903", "201904",
"201905", "201906", "201907", "201908", "201909", "201910", "201911",
"201912", "201913", "201914", "201915", "201916", "201917", "201918",
"201919", "201920", "201921", "201922", "201923", "201924", "201925",
"201926", "201927", "201928", "201929", "201930", "201931", "201932",
"201933", "201934", "201935", "201936", "201937", "201938", "201939",
"201940", "201941", "201942", "201943", "201944", "201945", "201946",
"201947", "201948", "201949", "201950", "201951", "201952", "202001",
"202002", "202003", "202004", "202005", "202006", "202007", "202008",
"202009", "202010", "202011", "202012", "202013", "202014", "202015",
"202016", "202017", "202018", "202019", "202020", "202021", "202022",
"202023", "202024", "202025", "202026", "202027", "202028", "202029",
"202030", "202031", "202032", "202033", "202034", "202035", "202036",
"202037", "202038", "202039", "202040", "202041", "202042", "202043",
"202044", "202045", "202046", "202047", "202048", "202049", "202050",
"202051", "202052", "202053", "202101", "202102", "202103", "202104",
"202105", "202106", "202107", "202108", "202109", "202110", "202111",
"202112", "202113", "202114", "202115", "202116", "202117", "202118",
"202119", "202120", "202121", "202122", "202123", "202124", "202125",
"202126", "202127", "202128", "202129", "202130", "202131", "202132",
"202133", "202134", "202135", "202136", "202137", "202138", "202139",
"202140", "202141", "202142", "202143"), Shipment = c(418, 1442,
1115, 1203, 1192, 1353, 1191, 1411, 933, 1384, 1362, 1353, 1739,
1751, 1595, 1380, 1711, 2058, 1843, 1602, 2195, 2159, 2009, 1812,
2195, 1763, 821, 1892, 1781, 2071, 1789, 1789, 1732, 1384, 1435,
1247, 1839, 2034, 1963, 1599, 1596, 1548, 1084, 1350, 1856, 1882,
1979, 1021, 1311, 2031, 1547, 591, 724, 1535, 1268, 1021, 1269,
1763, 1275, 1411, 1847, 1379, 1606, 1473, 1180, 926, 800, 840,
1375, 1755, 1902, 1921, 1743, 1275, 1425, 1088, 1416, 1168, 842,
1185, 1570, 1435, 1209, 1470, 1368, 1926, 1233, 1189, 1245, 1465,
1226, 887, 1489, 1369, 1358, 1179, 1200, 1226, 1066, 823, 1913,
2308, 1842, 910, 794, 1098, 1557, 1417, 1851, 1876, 1010, 160,
1803, 1607, 1185, 1347, 1700, 981, 1191, 1058, 1464, 1513, 1333,
1169, 1294, 978, 962, 1254, 987, 1290, 758, 436, 579, 636, 614,
906, 982, 649, 564, 502, 274, 473, 506, 902, 639, 810, 398, 488
), Production = c(0, 198, 1436, 1055, 1396, 1330, 1460, 1628,
1513, 1673, 1737, 1274, 1726, 1591, 2094, 1411, 2009, 1909, 1759,
1693, 1748, 1455, 2078, 1717, 1737, 1886, 862, 1382, 1779, 1423,
1460, 1454, 1347, 1409, 1203, 1235, 1397, 1563, 1411, 1455, 1706,
688, 1446, 1336, 1618, 1404, 1759, 746, 1560, 1665, 1317, 0,
441, 1390, 1392, 1180, 1477, 1265, 1485, 1495, 1543, 1584, 1575,
1609, 1233, 1420, 908, 1008, 1586, 1392, 1385, 1259, 1010, 973,
1053, 905, 1101, 1196, 891, 1033, 925, 889, 1136, 1058, 1179,
1047, 967, 900, 904, 986, 1014, 945, 1030, 1066, 1191, 1143,
1292, 574, 1174, 515, 1296, 1315, 1241, 0, 0, 1182, 1052, 1107,
1207, 1254, 1055, 258, 1471, 1344, 1353, 1265, 1444, 791, 1397,
1186, 1264, 1032, 949, 1059, 954, 798, 956, 1074, 1136, 1209,
975, 833, 994, 1127, 1153, 1202, 1234, 1336, 1484, 1515, 1151,
1175, 976, 1135, 1272, 869, 1900, 1173), Net.Production.Qty = c(22,
188, 1428, 1031, 1382, 1368, 1456, 1578, 1463, 1583, 1699, 1318,
1582, 1537, 2118, 1567, 1961, 1897, 1767, 1603, 1666, 1419, 2186,
1621, 1677, 1840, 698, 1290, 1411, 927, 1754, 1222, 1411, 1549,
1491, 1359, 1179, 1945, 1463, 1465, 1764, 764, 810, 1308, 1830,
1542, 1695, 544, 1482, 1673, 1659, 0, 445, 1358, 1364, 1224,
1417, 1239, 1387, 1595, 1469, 1624, 1643, 1763, 1217, 1456, 568,
1290, 1666, 1428, 1327, 773, 1118, 1231, 1143, 921, 1083, 1124,
935, 903, 937, 849, 1132, 1032, 1143, 1081, 891, 886, 880, 1002,
1072, 969, 1000, 996, 1243, 1183, 1306, 650, 1226, 553, 1306,
1379, 1359, 0, 0, 1182, 988, 1099, 1173, 1244, 1039, 254, 1425,
1318, 1385, 1221, 1364, 739, 1397, 1112, 1160, 924, 971, 1015,
978, 828, 868, 994, 1090, 1165, 783, 887, 934, 1023, 1045, 1114,
1052, 1186, 1456, 1401, 1249, 779, 430, 1625, 1498, 883, 1860,
1101), isoweek = c("2019-W01-1", "2019-W02-1", "2019-W03-1",
"2019-W04-1", "2019-W05-1", "2019-W06-1", "2019-W07-1", "2019-W08-1",
"2019-W09-1", "2019-W10-1", "2019-W11-1", "2019-W12-1", "2019-W13-1",
"2019-W14-1", "2019-W15-1", "2019-W16-1", "2019-W17-1", "2019-W18-1",
"2019-W19-1", "2019-W20-1", "2019-W21-1", "2019-W22-1", "2019-W23-1",
"2019-W24-1", "2019-W25-1", "2019-W26-1", "2019-W27-1", "2019-W28-1",
"2019-W29-1", "2019-W30-1", "2019-W31-1", "2019-W32-1", "2019-W33-1",
"2019-W34-1", "2019-W35-1", "2019-W36-1", "2019-W37-1", "2019-W38-1",
"2019-W39-1", "2019-W40-1", "2019-W41-1", "2019-W42-1", "2019-W43-1",
"2019-W44-1", "2019-W45-1", "2019-W46-1", "2019-W47-1", "2019-W48-1",
"2019-W49-1", "2019-W50-1", "2019-W51-1", "2019-W52-1", "2020-W01-1",
"2020-W02-1", "2020-W03-1", "2020-W04-1", "2020-W05-1", "2020-W06-1",
"2020-W07-1", "2020-W08-1", "2020-W09-1", "2020-W10-1", "2020-W11-1",
"2020-W12-1", "2020-W13-1", "2020-W14-1", "2020-W15-1", "2020-W16-1",
"2020-W17-1", "2020-W18-1", "2020-W19-1", "2020-W20-1", "2020-W21-1",
"2020-W22-1", "2020-W23-1", "2020-W24-1", "2020-W25-1", "2020-W26-1",
"2020-W27-1", "2020-W28-1", "2020-W29-1", "2020-W30-1", "2020-W31-1",
"2020-W32-1", "2020-W33-1", "2020-W34-1", "2020-W35-1", "2020-W36-1",
"2020-W37-1", "2020-W38-1", "2020-W39-1", "2020-W40-1", "2020-W41-1",
"2020-W42-1", "2020-W43-1", "2020-W44-1", "2020-W45-1", "2020-W46-1",
"2020-W47-1", "2020-W48-1", "2020-W49-1", "2020-W50-1", "2020-W51-1",
"2020-W52-1", "2020-W53-1", "2021-W01-1", "2021-W02-1", "2021-W03-1",
"2021-W04-1", "2021-W05-1", "2021-W06-1", "2021-W07-1", "2021-W08-1",
"2021-W09-1", "2021-W10-1", "2021-W11-1", "2021-W12-1", "2021-W13-1",
"2021-W14-1", "2021-W15-1", "2021-W16-1", "2021-W17-1", "2021-W18-1",
"2021-W19-1", "2021-W20-1", "2021-W21-1", "2021-W22-1", "2021-W23-1",
"2021-W24-1", "2021-W25-1", "2021-W26-1", "2021-W27-1", "2021-W28-1",
"2021-W29-1", "2021-W30-1", "2021-W31-1", "2021-W32-1", "2021-W33-1",
"2021-W34-1", "2021-W35-1", "2021-W36-1", "2021-W37-1", "2021-W38-1",
"2021-W39-1", "2021-W40-1", "2021-W41-1", "2021-W42-1", "2021-W43-1"
), date = structure(c(17896, 17903, 17910, 17917, 17924, 17931,
17938, 17945, 17952, 17959, 17966, 17973, 17980, 17987, 17994,
18001, 18008, 18015, 18022, 18029, 18036, 18043, 18050, 18057,
18064, 18071, 18078, 18085, 18092, 18099, 18106, 18113, 18120,
18127, 18134, 18141, 18148, 18155, 18162, 18169, 18176, 18183,
18190, 18197, 18204, 18211, 18218, 18225, 18232, 18239, 18246,
18253, 18260, 18267, 18274, 18281, 18288, 18295, 18302, 18309,
18316, 18323, 18330, 18337, 18344, 18351, 18358, 18365, 18372,
18379, 18386, 18393, 18400, 18407, 18414, 18421, 18428, 18435,
18442, 18449, 18456, 18463, 18470, 18477, 18484, 18491, 18498,
18505, 18512, 18519, 18526, 18533, 18540, 18547, 18554, 18561,
18568, 18575, 18582, 18589, 18596, 18603, 18610, 18617, 18624,
18631, 18638, 18645, 18652, 18659, 18666, 18673, 18680, 18687,
18694, 18701, 18708, 18715, 18722, 18729, 18736, 18743, 18750,
18757, 18764, 18771, 18778, 18785, 18792, 18799, 18806, 18813,
18820, 18827, 18834, 18841, 18848, 18855, 18862, 18869, 18876,
18883, 18890, 18897, 18904, 18911, 18918, 18925), class = "Date")), row.names = c(NA,
148L), class = "data.frame")
# Converting the df to accomodate leap year for weekly observations
Original.df <- Original.df %>%
mutate(
isoweek =stringr::str_replace(YearWeek, "^(\\d{4})(\\d{2})$", "\\1-W\\2-1"),
date = ISOweek::ISOweek2date(isoweek)
)
# creating test and train data
Original.train.df <- Original.df %>%
filter(date >= "2018-12-31", date <= "2021-03-29")
Original.test.df <- Original.df %>%
filter(date >= "2021-04-05", date <= "2021-10-25")
# splitting the original train data to contain only Week, Dependent and Independent variables
Total.train.df<-Original.train.df %>%
mutate(Week.1 = yearweek(ISOweek::ISOweek(date))) %>%
select(-YearWeek, -Production, -date,-isoweek) %>%
as_tsibble(index = Week.1)
#Fitting forecast model(Arima with Fourier terms) to Net.Production.qty
fit_all_models.Prod.1 <- list()
for(K in seq(25)){
fit.Prod.1 <- Total.train.df %>%
model(ARIMA(Net.Production.Qty ~ fourier(K = K),stepwise = FALSE, approximation = FALSE))
names(fit.Prod.1) <- paste0("arima_", K)
fit_all_models.Prod.1 <- bind_cols(fit_all_models.Prod.1, fit.Prod.1)
}
glance(fit_all_models.Prod.1) %>% arrange(AICc) %>% select(.model:BIC)
best_model.Prod.1 <- glance(fit_all_models.Prod.1) %>%
filter(AICc == min(AICc)) %>%
select(.model) %>%
as.character
#Forecasting Net.Production.Qty for 30 steps using the fitted model above-Model.1
Forecast.Net.Prod.1<-fit_all_models.Prod.1 %>%
select(all_of(best_model.Prod.1)) %>%
forecast(h = 30)
#To extract fitted values from the model which has min AICc
fitted.Prod.1<-fit.Prod.1 %>%
filter(AICc == min(AICc)) %>% fitted()
As you see from the last step above I'm trying to extract the fitted values from the model which has minimum AICc- which is not working though
If anyone could help me getting the fitted values from the model above which has min.AICc would be really helpful
Thank you
You are almost there:
# your code .....
# get the fitted based on the selection in best_model.Prod.1
fitted.Prod.1 <- fit_all_models.Prod.1 %>%
select(all_of(best_model.Prod.1)) %>%
fitted()
fitted.Prod.1
# A tsibble: 118 x 3 [1W]
# Key: .model [1]
.model Week.1 .fitted
<chr> <week> <dbl>
1 arima_13 2019 W01 21.0
2 arima_13 2019 W02 486.
3 arima_13 2019 W03 1007.
4 arima_13 2019 W04 965.
5 arima_13 2019 W05 1012.
6 arima_13 2019 W06 1088.
7 arima_13 2019 W07 1175.
8 arima_13 2019 W08 1166.
9 arima_13 2019 W09 1305.
10 arima_13 2019 W10 1613.
# ... with 108 more rows

"Can't recycle" error when using for loop in r

I have two data frames which I want to process with a for loop. Their structures are the following:
> m_ivae
structure(list(fecha = structure(c(17805, 17836, 17866, 17897,
17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170,
18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444,
18475, 18506, 18536, 18567, 18597, 18628, 18659, 18687, 18718,
18748, 18779, 18809), class = "Date"), IVAE = c(109.19, 110.09,
111.34, 111.84, 112.49, 111.99, 113.11, 111.89, 112.11, 112.75,
113.7, 112.93, 112.43, 114.88, 114.5, 114.93, 115.13, 105.54,
91.71, 87.93, 93.06, 96.74, 103.26, 106.76, 109.6, 110.74, 112,
112.73, 114.97, 115.01, 114.67, 115.78, 114.52, 111.91), `Agricultura, Ganadería, Silvicultura y Pesca` = c(99.58,
98.71, 103.44, 101.83, 101.31, 98.87, 99.06, 99.46, 96.55, 100.47,
98.79, 98.91, 100.17, 101.98, 100.48, 99.64, 96.04, 92.42, 97.21,
96.11, 100.57, 94.82, 99.07, 103.63, 97.34, 97.17, 95.46, 98.46,
101.02, 100.24, 100.6, 99.95, 103.07, 98.23), `Índice de Producción Industrial (IPI): Industrias Manufactureras, Explotación de Minas y Canteras y Otras Actividades Industriales` = c(101.4,
103.4, 105.07, 106.72, 108.45, 107.76, 107.25, 105.75, 107.03,
107.31, 106.61, 106.95, 106.61, 110.18, 108.68, 109.66, 111.32,
100.02, 76.77, 73.46, 81.99, 94.83, 100.64, 104.51, 106.74, 107.04,
108.75, 110.8, 110.59, 111.25, 108.82, 110.03, 111.32, 107.61
), Construcción = c(112.25, 117.5, 121.37, 124.32, 122.64, 121.21,
128.69, 122.28, 126.55, 120.13, 137.47, 129.82, 126.83, 132.92,
131.72, 137.56, 130.89, 117.08, 87.62, 67.49, 79.56, 88.97, 117.57,
110.01, 118.02, 117.61, 121.64, 120.76, 120.99, 118.96, 122.7,
122.59, 101.2, 106.3), `Comercio, Transporte y Almacenamiento, Actividades de Alojamiento y de Servicio de Comidas` = c(112.2,
113.03, 113.03, 115.69, 113.74, 114.7, 115.93, 115.3, 114.25,
115.05, 116.68, 114.84, 114.56, 116.58, 117.77, 119.19, 119.15,
103.41, 76.66, 75.21, 90.32, 91.72, 97.53, 105.21, 110.43, 109.72,
112.41, 114.05, 115.88, 117.29, 115.05, 114.69, 116.79, 109.68
), `Información y Comunicaciones` = c(115.49, 116.57, 116.18,
114.29, 113.92, 113.82, 116.45, 115.96, 114.81, 115.72, 116.07,
115.42, 115.32, 115.59, 114.22, 114.21, 113.05, 112.42, 111.52,
108.77, 113.92, 114.07, 115.02, 115.79, 117.78, 117.02, 119.21,
119.56, 125.27, 123.15, 118.56, 119.68, 120.02, 127.68), `Actividades Financieras y de Seguros` = c(117.96,
122.17, 120.93, 119.53, 121.15, 122.17, 125.01, 121.22, 127.48,
124.1, 124.56, 126.86, 124.59, 129.96, 131.74, 131.56, 138.4,
134.4, 131.6, 127.16, 124.61, 116.65, 120.28, 119.57, 127.23,
138.75, 141.25, 138.8, 138.79, 141.28, 141.62, 143.53, 137.62,
139.72), `Actividades Inmobiliarias` = c(113.31, 113.83, 114.41,
114.69, 114.97, 115.98, 116.2, 116.22, 115.64, 115.79, 115.95,
116.24, 117.6, 117.84, 115.35, 108.98, 105.89, 103.74, 103.16,
102.5, 102.42, 102.41, 104.16, 107.74, 112.87, 116.57, 115.68,
113.47, 112.41, 112.08, 112.42, 112.74, 113.21, 112.56), `Actividades Profesionales, Científicas, Técnicas, Administrativas, de Apoyo y Otros Servicios` = c(111.84,
111.92, 114.11, 116.44, 117.77, 112.96, 114.64, 113.67, 112.33,
115.12, 113.31, 114.14, 115.46, 117.17, 120.57, 124.26, 122.68,
99.51, 86.36, 79.21, 81.56, 83.6, 88.71, 97.76, 98.16, 101.04,
102.68, 108.37, 113.64, 114.82, 115.91, 118.35, 118.74, 109.14
), `Actividades de Administración Pública y Defensa, Enseñanza, Salud y Asistencia Social` = c(110.04,
108.07, 109.24, 105.85, 108.99, 109.12, 109.6, 109.31, 108.63,
111.22, 111.25, 109.67, 107.59, 108.8, 106.9, 105.82, 108.24,
107.71, 106.75, 104.67, 98.47, 102.09, 108.94, 109.34, 110.3,
110.01, 109.3, 107.24, 113.46, 111.17, 113.44, 116.42, 112.98,
114.37)), row.names = c(NA, -34L), class = c("tbl_df", "tbl",
"data.frame"))
> m_ipc
structure(list(fecha = structure(c(17805, 17836, 17866, 17897,
17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170,
18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444,
18475, 18506, 18536, 18567, 18597, 18628, 18659, 18687, 18718,
18748, 18779, 18809, 18840, 18871), class = "Date"), `Índice General` = c(113.02,
112.82, 112.3, 112.24, 112.44, 112.69, 112.87, 113.01, 112.85,
112.56, 112.16, 111.99, 112.04, 112.17, 112.29, 112.15, 112,
112.09, 111.69, 111.94, 112.59, 112.49, 111.82, 111.56, 111.81,
111.98, 112.2, 112.49, 113.19, 114.08, 114.81, 114.84, 115.51,
116.36, 116.63, 117.1), `Alimentos y Bebidas no Alcohólicas` = c(120.22,
120.56, 120.44, 120.81, 121.12, 121.39, 121.71, 122.29, 122.61,
121.82, 120.79, 120.64, 121.08, 121.48, 121.88, 122.35, 122.22,
122.68, 124.24, 125.06, 126.14, 125.84, 123.33, 122.36, 121.89,
122.24, 122.33, 122.5, 123.12, 124.09, 124.19, 123.97, 124.85,
125.76, 125.93, 127.18), `Bebidas Alcohólicas, Tabaco` = c(146,
145.59, 145.84, 147.3, 146.86, 146.84, 147.11, 147.74, 148.21,
149.24, 150.04, 150.05, 150.11, 149.9, 150.54, 151.89, 151.81,
152.29, 152.01, 153.09, 152.72, 154.65, 154.56, 152.64, 153.4,
153.59, 153.87, 154.49, 155.21, 155.63, 155.4, 155.2, 156.36,
156.2, 156, 157.11), `Prendas de Vestir y Calzado` = c(92.82,
92.77, 92.74, 92.76, 92.93, 92.89, 92.9, 92.69, 92.57, 92.42,
92.13, 91.42, 91.44, 91.17, 91.03, 91.09, 91.43, 91.88, 91.84,
91.84, 91.84, 91.84, 91.84, 92.05, 92.55, 92.6, 92.75, 93, 93.5,
93.84, 93.98, 94.35, 94.5, 94.71, 94.86, 94.85), `Alojamiento, Agua, Electricidad, Gas y otros Combustibles` = c(140.49,
139.57, 138.12, 137.52, 137.35, 137.51, 136.16, 135.75, 135.34,
134.77, 134.82, 134.79, 133.85, 134.04, 134.93, 132.51, 131.61,
131.68, 131.02, 131.03, 131.83, 129.07, 128.61, 129, 131.34,
131.41, 131.97, 132.01, 134.25, 135.03, 137.66, 136.74, 136.96,
140.04, 141.58, 141.93), `Muebles, Artículos para el Hogar y para la Conservación Ordinaria del Hogar` = c(100.24,
100.36, 100.14, 100.29, 100.52, 100.16, 100.25, 100.3, 99.86,
99.73, 99.64, 99.63, 99.48, 99.16, 98.94, 99.16, 99.54, 99.98,
100.08, 100.13, 100.02, 99.83, 100.23, 100.39, 100.07, 100.17,
100.92, 101, 101.98, 102.74, 103.46, 103.81, 104.38, 105.06,
105.3, 106.45), Salud = c(99.37, 99.28, 99.29, 99.29, 99.27,
99.27, 99.34, 99.44, 99.54, 99.6, 99.77, 100.06, 100.07, 100.14,
100.12, 100.17, 100.01, 99.98, 99.96, 100.19, 100.22, 100.9,
100.97, 101.13, 101.24, 101.9, 101.88, 102.04, 102.93, 103.14,
103.37, 103.83, 104.14, 104.19, 104.45, 104.53), Transporte = c(112.15,
110.75, 108.27, 106.83, 107.41, 108.94, 111.01, 111.41, 110.51,
110.51, 109.34, 108.64, 109.05, 109.47, 108.79, 108.56, 107.88,
106.73, 100.48, 100.6, 102.77, 104.29, 103.76, 103.45, 103.59,
103.53, 103.64, 105.12, 105.76, 109.23, 111.09, 111.72, 112.93,
113.5, 112.71, 112.13), Comunicaciones = c(84.77, 84.69, 84.69,
84.64, 84.32, 84.32, 84.32, 84.31, 84.1, 83.78, 83.78, 83.78,
83.89, 83.89, 83.7, 83.2, 83.16, 83.16, 83.2, 83.17, 83.17, 82.99,
82.99, 83.03, 83.19, 83.19, 83.17, 83.12, 83.12, 83.12, 83.12,
83.11, 83.11, 83.09, 83.09, 83.09), `Recreación y Cultura` = c(87.35,
87.37, 87.4, 87.77, 88.71, 88.48, 88.72, 88.75, 88.08, 88.14,
88.18, 87.97, 87.81, 87.72, 87.58, 87.63, 87.89, 87.74, 87.67,
87.6, 87.65, 87.81, 88.29, 87.68, 88.02, 88.08, 88.14, 88.06,
87.86, 88.11, 88.51, 88.77, 89.12, 89.11, 88.98, 89.14), Educación = c(112.83,
112.83, 112.83, 113.27, 113.27, 113.27, 113.27, 113.27, 113.27,
113.27, 113.27, 113.27, 113.65, 113.65, 113.65, 114.06, 114.06,
114.06, 114.06, 114.06, 114.06, 114.06, 114.06, 114.06, 114.26,
114.26, 114.26, 114.26, 114.26, 114.26, 114.26, 114.26, 114.26,
114.26, 114.26, 114.26), `Restaurantes y Hoteles` = c(122.94,
122.7, 122.81, 123.41, 123.37, 123.54, 123.49, 123.57, 123.55,
123.63, 123.59, 123.5, 123.58, 123.54, 123.93, 124.32, 124.44,
124.44, 124.5, 124.61, 124.7, 125.04, 125.34, 125.52, 125.52,
125.8, 126.01, 126.36, 126.65, 126.97, 127.49, 127.95, 129.19,
129.73, 130.46, 131.3), `Bienes y Servicios Diversos` = c(107.55,
107.75, 107.6, 107.39, 107.4, 107.55, 107.36, 107.13, 107.22,
107.26, 107.4, 107.48, 107.42, 107.4, 107.3, 107.37, 107.55,
108.21, 108.38, 108.39, 108.46, 109.45, 109.67, 109.42, 109.65,
109.65, 109.99, 110.25, 110.37, 110.19, 110.34, 110.36, 111.16,
111.8, 112.28, 112.23)), row.names = c(NA, -36L), class = c("tbl_df",
"tbl", "data.frame"))
And I am using the following code:
library(janitor)
wide_dataframes = list(m_ivae,m_ipc)
names(wide_dataframes) = c('m_ivae','m_ipc')
for (nm in names(wide_dataframes)){
df = get(nm)
df = clean_names(df)
df[paste0("lag", 1:3)] = lapply(1:3, lag, x=df[,2:ncol(df)])
df[,2:ncol(df)] = apply(df[,2:ncol(df)],2,function(x) as.numeric(as.character(x)))
assign(nm, df)
}
However, after I run the for loop, I get the following error message:
Error: Can't recycle `apply(df[, 2:ncol(df)], 2, function(x) as.numeric(as.character(x)))` (size 40) to size 13.
I tried to fix it by removing the column specifications in the fifth line of the for loop, like this:
for (nm in names(wide_dataframes)){
df = get(nm)
df = clean_names(df)
df[paste0("lag", 1:3)] = lapply(1:3, lag, x=df[,2:ncol(df)])
df = apply(df[,2:ncol(df)],2,function(x) as.numeric(as.character(x)))
assign(nm, df)
}
This solves the error, but removes the first column, which I need to keep in order to perform a left join with a different data frame later on.
The issue seems to be assigning the column names df[paste0("lag", 1:3)] i.e. when we do the lag on the whole data or a part of it df[,2:ncol(df)], the assignment to the lhs of = is not of the same length i.e. it is just of length 3 compared to the original ncol(df)-1. As we are using a for loop, the inner lag can also be in a for loop
for (nm in names(wide_dataframes)){
df <- get(nm)
df <- clean_names(df)
nm1 <- names(df)[2:ncol(df)] # get the names of the columns to be lagged
for(i in 1:3) {
nm2 <- paste0(nm1, "lag", i)
df[nm2] <- lag(df[, nm1], n = i)
}
df[,2:ncol(df)] <- lapply(df[,2:ncol(df)],
function(x) as.numeric(as.character(x)))
assign(nm, df)
}
-checking
> ncol(m_ivae)
[1] 41
> ncol(m_ipc)
[1] 53
compare with original number of columns
> sapply(wide_dataframes, ncol)
m_ivae m_ipc
11 14

From 201906141930 to "2019-16-14 19:30:00"

I've a data frame with a column dateHourMinute that I need it as POSIXct to make a plot.
For example this dateHourMinute 201906141930, I'd like to get: 2019-06-14 19:30:00 as a POSIXct element.
data:
structure(list(dateHourMinute = c("201906141930", "201906141931",
"201906141932", "201906141933", "201906141934", "201906141935",
"201906141936", "201906141937", "201906141938", "201906141939",
"201906141940", "201906141941", "201906141942", "201906141943",
"201906141944", "201906141945", "201906141946", "201906141947",
"201906141948", "201906141949", "201906141950", "201906141951",
"201906141952", "201906141953", "201906141954", "201906141955",
"201906141956", "201906141957", "201906141958", "201906141959",
"201906142000", "201906142001", "201906142002", "201906142003",
"201906142004", "201906142005", "201906142006", "201906142007",
"201906142008", "201906142009", "201906142010", "201906142011",
"201906142012", "201906142013", "201906142014", "201906142015",
"201906142016", "201906142017", "201906142018", "201906142019",
"201906142020", "201906142021", "201906142022", "201906142023",
"201906142024", "201906142025", "201906142026", "201906142027",
"201906142028", "201906142029", "201906142030", "201906142031",
"201906142032", "201906142033", "201906142034", "201906142035",
"201906142036", "201906142037", "201906142038", "201906142039",
"201906142040", "201906142041", "201906142042", "201906142043",
"201906142044", "201906142045", "201906142046", "201906142047",
"201906142048", "201906142049", "201906142050", "201906142051",
"201906142052", "201906142053", "201906142054", "201906142055",
"201906142056", "201906142057", "201906142058", "201906142059",
"201906142100", "201906142101", "201906142102", "201906142103",
"201906142104", "201906142105", "201906142106", "201906142107",
"201906142108", "201906142109", "201906142110", "201906142111",
"201906142112", "201906142113", "201906142114", "201906142115",
"201906142116", "201906142117", "201906142118", "201906142119",
"201906142120", "201906142121", "201906142122", "201906142123",
"201906142124", "201906142125", "201906142126", "201906142127",
"201906142128", "201906142129", "201906142130", "201906142131",
"201906142132", "201906142133", "201906142134", "201906142135",
"201906142136", "201906142137", "201906142138", "201906142139",
"201906142140", "201906142141", "201906142142", "201906142143",
"201906142144", "201906142145", "201906142146", "201906142147",
"201906142148", "201906142149", "201906142150", "201906142151",
"201906142152", "201906142153", "201906142154", "201906142155"
), users = c(2894, 2969, 3031, 2912, 2845, 2837, 2832, 2731,
2784, 2681, 2682, 2614, 2569, 2551, 2580, 2588, 2574, 2458, 2419,
2504, 2430, 2401, 2322, 2252, 2329, 2374, 2201, 2142, 2163, 2133,
2087, 2078, 2053, 2206, 2093, 2091, 2045, 2059, 1945, 1943, 1951,
1972, 1899, 1822, 1841, 1906, 1778, 2148, 3297, 2098, 1801, 1650,
1630, 1626, 1674, 1647, 1633, 1671, 1757, 1862, 1968, 2045, 2119,
2396, 2513, 2394, 2375, 2492, 2488, 2381, 2417, 2337, 2243, 2211,
1999, 2021, 2037, 2418, 2254, 2050, 2004, 1944, 1802, 1718, 1726,
1725, 1641, 1657, 1592, 1604, 1551, 1553, 1486, 1481, 1518, 1479,
1310, 1317, 1329, 1259, 1255, 1259, 1407, 1352, 1250, 1250, 1223,
1149, 1103, 1108, 1025, 1165, 1870, 1452, 1418, 1469, 1522, 1303,
1147, 1060, 1004, 1001, 1003, 983, 894, 870, 882, 863, 832, 790,
819, 732, 751, 752, 694, 692, 926, 862, 755, 736, 796, 803, 771,
869, 745, 709)), row.names = c(NA, -146L), totals = list(list(
users = "2016665")), minimums = list(list(users = "1")), maximums = list(
list(users = "11863")), isDataGolden = TRUE, rowCount = 2875L, class = "data.frame")
You could use :
df$dateHourMinute <- as.POSIXct(df$dateHourMinute,format = "%Y%m%d%H%M", tz = "UTC")
#Or with `strptime`
#df$dateHourMinute <- strptime(df$dateHourMinute, format = "%Y%m%d%H%M", tz = "UTC")
head(df)
# dateHourMinute users
#1 2019-06-14 19:30:00 2894
#2 2019-06-14 19:31:00 2969
#3 2019-06-14 19:32:00 3031
#4 2019-06-14 19:33:00 2912
#5 2019-06-14 19:34:00 2845
#6 2019-06-14 19:35:00 2837
Or with lubridate
df$dateHourMinute <- lubridate::ymd_hm(df$dateHourMinute))

How do I make a single graph of multiple columns from a dataset?

So I have a large dataset with the daily price of gold in various currencies. The columns in the dataset are Date, USD, GBP, EURO. I want to represent each currency in a single graph. I have this so far:
p1 <- ggplot(data = GOLD) +
geom_line(aes(x = Date, y = USD, GBP, EURO), col ="red") +
labs(title = "Daily price of Gold",
x = "Date", y = "Price in Various Currencies")
But, having y represent each of the currencies is erroring out.
If this is the structure of your data frame, called gold:
str(gold)
'data.frame': 602 obs. of 5 variables:
$ Date: Date, format: "1969-12-31" "1970-01-30" "1970-02-27" "1970-03-31" ...
$ USD : num 35.2 35 35 35.3 35.9 ...
$ EUR : num 22.2 23.2 23.3 23.4 23.8 ...
$ GBP : num 14.7 14.5 14.6 14.7 14.9 ...
$ JPY : num 12592 12496 12530 12628 12858 ...
Then the following commands will produce the desired graph:
library(tidyr) # Needed to reshape the data to longer format
library(ggplot2) # Needed to plot the data
library(scales) # Needed to modify the y-axis tick labels
gold %>%
pivot_longer(cols=USD:JPY, names_to="Currency", values_to="Price") %>%
ggplot(aes(x = Date, y = Price, color=Currency)) +
geom_line() +
labs(title = "Daily price of gold",
x = "Date", y = "Price in various currencies") +
scale_y_log10(labels=comma) # Otherwise you get scientific notation
Data: (available from https://www.gold.org/goldhub/data/gold-prices)
gold <- structure(list(Date = structure(c(-1, 29, 57, 89, 119, 148, 180,
211, 242, 272, 302, 333, 364, 393, 421, 454, 484, 515, 545, 575,
607, 637, 666, 698, 729, 760, 789, 820, 848, 881, 911, 942, 973,
1002, 1034, 1064, 1093, 1126, 1154, 1184, 1215, 1246, 1275, 1307,
1338, 1366, 1399, 1429, 1460, 1491, 1519, 1548, 1580, 1611, 1639,
1672, 1702, 1733, 1764, 1793, 1825, 1856, 1884, 1915, 1945, 1975,
2006, 2037, 2066, 2098, 2129, 2157, 2190, 2220, 2248, 2281, 2311,
2342, 2372, 2402, 2434, 2464, 2493, 2525, 2556, 2587, 2615, 2646,
2675, 2707, 2737, 2766, 2799, 2829, 2860, 2890, 2920, 2952, 2980,
3011, 3039, 3072, 3102, 3133, 3164, 3193, 3225, 3255, 3284, 3317,
3345, 3375, 3406, 3437, 3466, 3498, 3529, 3557, 3590, 3620, 3651,
3682, 3711, 3742, 3772, 3802, 3833, 3864, 3893, 3925, 3956, 3984,
4017, 4047, 4075, 4107, 4137, 4166, 4198, 4229, 4260, 4290, 4320,
4351, 4382, 4411, 4439, 4472, 4502, 4533, 4563, 4593, 4625, 4655,
4684, 4716, 4747, 4778, 4806, 4837, 4866, 4898, 4928, 4957, 4990,
5020, 5051, 5081, 5111, 5143, 5172, 5202, 5233, 5264, 5293, 5325,
5356, 5384, 5417, 5447, 5478, 5509, 5537, 5566, 5598, 5629, 5657,
5690, 5720, 5751, 5782, 5811, 5843, 5874, 5902, 5933, 5963, 5993,
6024, 6055, 6084, 6116, 6147, 6175, 6208, 6238, 6266, 6298, 6328,
6357, 6389, 6420, 6451, 6481, 6511, 6542, 6573, 6602, 6633, 6664,
6693, 6725, 6755, 6784, 6817, 6847, 6878, 6908, 6938, 6970, 6998,
7029, 7057, 7090, 7120, 7151, 7182, 7211, 7243, 7273, 7302, 7335,
7363, 7393, 7424, 7455, 7484, 7516, 7547, 7575, 7608, 7638, 7669,
7700, 7728, 7757, 7789, 7820, 7848, 7881, 7911, 7942, 7973, 8002,
8034, 8065, 8093, 8125, 8155, 8184, 8216, 8247, 8278, 8308, 8338,
8369, 8400, 8429, 8457, 8490, 8520, 8551, 8581, 8611, 8643, 8673,
8702, 8734, 8765, 8796, 8824, 8855, 8884, 8916, 8946, 8975, 9008,
9038, 9069, 9099, 9129, 9161, 9189, 9220, 9248, 9281, 9311, 9342,
9373, 9402, 9434, 9464, 9493, 9526, 9555, 9584, 9616, 9647, 9675,
9708, 9738, 9769, 9800, 9829, 9861, 9892, 9920, 9951, 9981, 10011,
10042, 10073, 10102, 10134, 10165, 10193, 10226, 10256, 10284,
10316, 10346, 10375, 10407, 10438, 10469, 10499, 10529, 10560,
10591, 10620, 10648, 10681, 10711, 10742, 10772, 10802, 10834,
10864, 10893, 10925, 10956, 10987, 11016, 11047, 11075, 11108,
11138, 11169, 11200, 11229, 11261, 11291, 11320, 11353, 11381,
11411, 11442, 11473, 11502, 11534, 11565, 11593, 11626, 11656,
11687, 11718, 11746, 11775, 11807, 11838, 11866, 11899, 11929,
11960, 11991, 12020, 12052, 12083, 12111, 12142, 12172, 12202,
12233, 12264, 12293, 12325, 12356, 12384, 12417, 12447, 12475,
12508, 12538, 12569, 12599, 12629, 12661, 12691, 12720, 12752,
12783, 12814, 12842, 12873, 12902, 12934, 12964, 12993, 13026,
13056, 13087, 13117, 13147, 13179, 13207, 13238, 13266, 13299,
13329, 13360, 13391, 13420, 13452, 13482, 13511, 13544, 13572,
13602, 13633, 13664, 13693, 13725, 13756, 13784, 13817, 13847,
13878, 13909, 13938, 13969, 13999, 14029, 14060, 14091, 14120,
14152, 14183, 14211, 14244, 14274, 14302, 14334, 14364, 14393,
14425, 14456, 14487, 14517, 14547, 14578, 14609, 14638, 14666,
14699, 14729, 14760, 14790, 14820, 14852, 14882, 14911, 14943,
14974, 15005, 15033, 15064, 15093, 15125, 15155, 15184, 15217,
15247, 15278, 15308, 15338, 15370, 15399, 15429, 15460, 15491,
15520, 15552, 15583, 15611, 15644, 15674, 15705, 15736, 15764,
15793, 15825, 15856, 15884, 15917, 15947, 15978, 16009, 16038,
16070, 16101, 16129, 16160, 16190, 16220, 16251, 16282, 16311,
16343, 16374, 16402, 16435, 16465, 16493, 16525, 16555, 16584,
16616, 16647, 16678, 16708, 16738, 16769, 16800, 16829, 16860,
16891, 16920, 16952, 16982, 17011, 17044, 17074, 17105, 17135,
17165, 17197, 17225, 17256, 17284, 17317, 17347, 17378, 17409,
17438, 17470, 17500, 17529, 17562, 17590, 17620, 17651, 17682,
17711, 17743, 17774, 17802, 17835, 17865, 17896, 17927, 17955,
17984, 18016, 18047, 18075, 18108, 18138, 18169, 18200, 18229,
18261, 18292), class = "Date"), USD = c(35.2, 34.99, 35.02, 35.3,
35.85, 35.45, 35.49, 35.3, 35.8, 36.4, 37.25, 37.54, 37.38, 38.05,
38.8, 38.88, 39.7, 40.84, 40.1, 42.4, 40.65, 42.6, 42.34, 43.6,
43.55, 47.15, 48.2, 48.38, 49.6, 59.45, 64.65, 68.3, 66.88, 64.2,
64.39, 63.6, 64.9, 66, 85, 90, 90.72, 114.75, 123.25, 115.6,
103.5, 100, 98, 101, 112.25, 132.5, 162.5, 173, 169.25, 156.75,
144.25, 156, 156, 151.25, 167, 184, 186.5, 175.8, 181.75, 177.25,
167, 167, 166.25, 166.7, 159.8, 141.25, 142.9, 138.15, 140.25,
128.15, 132.3, 129.6, 128.4, 125.5, 123.8, 112.5, 104, 116, 123.15,
130.25, 134.5, 132.3, 142.75, 148.9, 147.25, 142.95, 143, 144.1,
146, 154.05, 161.5, 160.05, 164.95, 175.75, 182.25, 181.6, 170.85,
184.15, 183.05, 200.25, 208.7, 217.1, 242.6, 193.4, 226, 233.7,
251.3, 240.1, 245.3, 274.6, 277.5, 296.45, 315.1, 397.25, 382,
415.65, 512, 653, 637, 494.5, 518, 535.5, 653.5, 614.25, 631.25,
666.75, 629, 619.75, 589.75, 506.5, 489, 513.75, 482.75, 479.25,
426, 406, 425, 428.75, 427, 414.5, 397.5, 387, 362.6, 320, 361.25,
325.25, 317.5, 342.9, 411.5, 397, 423.25, 436, 456.9, 499.5,
408.5, 414.75, 429.25, 437.5, 416, 422, 414.25, 405, 382, 405,
382.4, 373.75, 394.25, 388.5, 375.8, 384.25, 373.05, 342.35,
348.25, 343.75, 333.5, 329, 308.3, 306.65, 287.75, 329.25, 321.35,
314, 317.75, 327.5, 333.25, 325.75, 325.1, 325.3, 326.8, 350.5,
338.15, 344, 345.75, 343.2, 346.75, 357.5, 384.7, 423.2, 401,
389.5, 388.75, 400.5, 405.85, 421, 453.25, 451, 447.3, 462.5,
453.4, 459.5, 468.8, 492.5, 484.1, 458, 426.15, 456.95, 449,
455.5, 436.55, 436.8, 427.75, 396.7, 412.4, 422.6, 410.25, 394,
387, 383.2, 377.55, 361.8, 373, 368.3, 359.8, 366.5, 375.3, 408.15,
398.6, 415.05, 407.7, 368.5, 367.75, 363.05, 352.2, 372.3, 387.75,
408.4, 379.5, 384.85, 386.2, 366, 362.7, 355.65, 357.75, 360.4,
368.35, 362.85, 347.4, 354.9, 357.45, 366.3, 353.15, 354.1, 353.1,
341.7, 336.35, 337.5, 343.4, 357.85, 340, 349, 339.25, 334.2,
332.9, 330.45, 327.6, 337.8, 354.3, 377.45, 378.45, 401.75, 371.55,
355.5, 369.6, 370.9, 391.75, 377.9, 381.55, 389.2, 376.45, 387.6,
388.25, 384, 385.75, 394.85, 383.85, 383.1, 383.25, 374.9, 376.4,
392, 389.75, 384.3, 387.05, 383.35, 382.35, 384, 382.65, 387.8,
387, 405.55, 400.65, 396.35, 391.3, 390.55, 382, 385.3, 386.45,
379, 379.5, 371.3, 369.25, 345.5, 358.6, 348.15, 340.15, 345.6,
334.55, 326.35, 325.35, 332.1, 311.4, 296.8, 290.2, 304.85, 297.4,
301, 310.7, 293.6, 296.3, 288.85, 273.4, 293.85, 292.3, 294.7,
287.8, 285.4, 287.05, 279.45, 286.6, 268.6, 261, 255.6, 254.8,
299, 299.1, 291.35, 290.25, 283.3, 293.65, 276.75, 275.05, 272.25,
288.15, 276.75, 277, 273.65, 264.5, 269.1, 274.45, 264.5, 266.7,
257.7, 263.15, 267.5, 270.6, 265.9, 273, 293.1, 278.75, 275.5,
276.5, 282.3, 296.85, 301.4, 308.2, 326.6, 318.5, 304.65, 312.8,
323.7, 316.9, 319.05, 347.2, 367.5, 347.45, 334.85, 336.75, 361.4,
346, 354.75, 375.6, 388, 386.25, 398.35, 416.25, 399.75, 395.85,
423.7, 388.5, 393.25, 395.8, 391.4, 407.25, 415.65, 425.55, 453.4,
435.6, 422.15, 435.45, 427.5, 435.7, 414.45, 437.1, 429, 433.25,
473.25, 470.75, 495.65, 513, 568.75, 556, 582, 644, 653, 613.5,
632.5, 623.5, 599.25, 603.75, 646.7, 632, 650.5, 664.2, 661.75,
677, 659.1, 650.5, 665.5, 672, 743, 789.5, 783.5, 833.75, 923.25,
971.5, 933.5, 871, 885.75, 930.25, 918, 833, 884.5, 730.75, 814.5,
869.75, 919.5, 952, 916.5, 883.25, 975.5, 934.5, 939, 955.5,
995.75, 1040, 1175.75, 1087.5, 1078.5, 1108.25, 1115.5, 1179.25,
1207.5, 1244, 1169, 1246, 1307, 1346.75, 1383.5, 1405.5, 1327,
1411, 1439, 1535.5, 1536.5, 1505.5, 1628.5, 1813.5, 1620, 1722,
1746, 1531, 1744, 1770, 1662.5, 1651.25, 1558, 1598.5, 1622,
1648.5, 1776, 1719, 1726, 1657.5, 1664.75, 1588.5, 1598.25, 1469,
1394.5, 1192, 1314.5, 1394.75, 1326.5, 1324, 1253, 1204.5, 1251,
1326.5, 1291.75, 1288.5, 1250.5, 1315, 1285.25, 1285.75, 1216.5,
1164.25, 1182.75, 1206, 1260.25, 1214, 1187, 1180.25, 1191.4,
1171, 1098.4, 1135, 1114, 1142.35, 1061.9, 1060, 1111.8, 1234.9,
1237, 1285.65, 1212.1, 1320.75, 1342, 1309.25, 1322.5, 1272,
1178.1, 1145.9, 1212.8, 1255.6, 1244.85, 1266.45, 1266.2, 1242.25,
1267.55, 1311.75, 1283.1, 1270.15, 1280.2, 1291, 1345.05, 1317.85,
1323.85, 1313.2, 1305.35, 1250.45, 1220.95, 1202.45, 1187.25,
1214.95, 1217.55, 1279, 1323.25, 1319.15, 1295.4, 1282.3, 1295.55,
1409, 1427.55, 1528.4, 1485.3, 1510.95, 1460.15, 1514.75, 1584.2
), EUR = c(22.24, 23.2, 23.26, 23.42, 23.79, 23.54, 23.55, 23.45,
23.58, 23.98, 24.58, 24.71, 24.41, 25.09, 25.58, 25.61, 26.14,
26.9, 26.39, 27.92, 26.1, 27.39, 27.22, 27.93, 27.06, 28.99,
29.51, 29.36, 30.17, 36.01, 39.03, 41.31, 40.23, 38.78, 39.27,
38.71, 39.55, 40.81, 48.14, 51.83, 52.61, 65.04, 67.54, 63.58,
57.35, 54.93, 54.3, 58.89, 65.95, 82.65, 100.08, 100.68, 97.53,
92.29, 85.08, 91.61, 93.61, 90.4, 99.31, 107.71, 106.18, 98.8,
99.58, 98.85, 92.69, 91.82, 92.63, 98.41, 94.56, 85.78, 84.59,
83.49, 84.68, 80.12, 84.37, 85.94, 87.07, 84.52, 83.13, 75.95,
70.12, 78.1, 83.06, 87.92, 90.21, 89.95, 97.08, 102.08, 100.64,
97.73, 97.61, 100.32, 102.04, 107.8, 111.51, 110.18, 110.08,
117.4, 119.82, 118.79, 113.18, 122.76, 120.43, 129.16, 132.48,
135.68, 141.49, 122.4, 137.06, 144.84, 154.64, 148.04, 152.76,
172, 169.03, 179.68, 190.38, 233.23, 231.45, 244.87, 299.12,
385.01, 381.48, 321.56, 317.27, 323.09, 392.72, 372.37, 386.52,
412.83, 404.91, 406.73, 392.41, 363.31, 357.95, 375.1, 369.74,
386.14, 353.4, 346.87, 362.53, 349.87, 347.03, 332.94, 327.48,
327.92, 308.91, 285.71, 318.81, 284.76, 301.04, 315.16, 388.34,
383.37, 416.77, 423.24, 429.9, 486.07, 394.41, 410.93, 430.88,
447.29, 431.65, 456.53, 452.93, 440.9, 416.46, 451.49, 432, 436.19,
428.55, 421.4, 425.18, 437.24, 431.92, 412.65, 419.37, 436.4,
421.37, 423.66, 402.45, 402.82, 400.46, 425.73, 417.93, 403.43,
404.96, 393.64, 399.98, 375.93, 366.57, 353.9, 347.51, 364.1,
328.64, 348.01, 329.63, 350.96, 336.16, 332.15, 349.46, 383.64,
369.82, 345.5, 337.8, 334.76, 338.02, 346.74, 371.84, 375.9,
373.31, 391.91, 374.28, 384.69, 372.16, 371.97, 351.76, 352.89,
330.48, 347.7, 345.58, 360.76, 364.4, 374.62, 369.07, 341.8,
337.87, 336.58, 332.66, 335.74, 321.64, 329.38, 321.84, 324.56,
330.9, 310.91, 318.07, 312.36, 315.19, 332.89, 310.67, 321.26,
316.15, 283.87, 280.65, 280.21, 265.93, 267.8, 278.03, 291.86,
262.61, 264.8, 265.67, 251.05, 256.11, 279.75, 282.52, 288.89,
308.46, 292.89, 280.79, 273.61, 276.67, 277.92, 250.28, 264.7,
268.95, 261.69, 257.99, 251.28, 243.14, 246.81, 224.5, 241.25,
254.97, 261.39, 266.67, 264.28, 270.45, 274.97, 281.13, 300.65,
321.12, 354.79, 318.97, 298.71, 318.85, 327.89, 348.19, 335.18,
332.98, 331.04, 317.52, 325.31, 317.59, 313.37, 313, 314.77,
298.37, 311.1, 308.79, 297.3, 293.58, 291.35, 291.51, 289.94,
287.07, 280.74, 294.95, 288.98, 285.63, 294.55, 290.67, 314.78,
306.5, 304.48, 308.65, 307.01, 298.59, 293.51, 294.9, 296.14,
294.25, 291.75, 290.49, 288.68, 310.07, 297.45, 300.81, 301.56,
296.89, 305.23, 298.45, 298.75, 273.02, 266.62, 266.06, 284.48,
275.71, 284.19, 284.81, 267.29, 272.95, 262.35, 246.34, 251.03,
247.54, 254.8, 245.08, 251.3, 261.48, 258.85, 270.89, 257.55,
253.08, 238.81, 241.22, 280.75, 284.56, 289.35, 289.56, 289.55,
305, 289.22, 301.82, 293.56, 300.59, 298.67, 311.55, 310.08,
312.06, 309.13, 292.31, 284.41, 290.02, 291.52, 296.81, 315.6,
319.63, 303.89, 300.53, 321.84, 309.48, 307.68, 310.53, 327.91,
343.18, 345.48, 342.03, 349.57, 322.5, 310.74, 318.96, 327.53,
320, 320.72, 330.86, 342.34, 322.37, 306.86, 301.75, 307.27,
301.3, 315.18, 342.11, 333.18, 332.26, 332.32, 330, 321.78, 318.59,
344.78, 324.09, 322.03, 325.32, 325.1, 335.1, 334.66, 334.54,
341.15, 320.47, 323.85, 328.06, 328.93, 337.5, 335.65, 361.05,
353.19, 352.28, 392.53, 393.03, 420.42, 434.91, 468.38, 466.35,
480.93, 511.25, 508.39, 479.8, 495.63, 487.09, 473.06, 473.03,
487.87, 479.28, 500.6, 502.82, 497.13, 496.06, 489.8, 481.66,
486.17, 492.94, 522.45, 545.71, 533.77, 570.26, 623.56, 639.94,
589.13, 559.45, 569.96, 590.43, 588.37, 565.8, 629.69, 576.28,
641.89, 625.7, 717.52, 749.58, 690.29, 666.55, 689.18, 666.24,
662.32, 665.83, 681.23, 704.87, 783.13, 757.97, 775.93, 812.08,
824.4, 886.89, 984.07, 1015.59, 897.3, 980.37, 957.37, 968.96,
1062.8, 1047.67, 967.91, 1021.58, 1014.02, 1034.98, 1068.8, 1038.38,
1133.26, 1259.55, 1207.42, 1234.59, 1297.03, 1179.37, 1332.26,
1323.17, 1248.4, 1247.59, 1260.06, 1259.6, 1317.04, 1307.81,
1380.49, 1326.59, 1327.08, 1257.21, 1226.43, 1215.05, 1244.65,
1114.23, 1076, 917.03, 989.95, 1057.75, 979.94, 973.89, 920.28,
874.12, 927.66, 960.43, 937.24, 929.28, 916.42, 960.45, 960.58,
976.12, 962.99, 929.24, 948.74, 996.65, 1116.8, 1082.33, 1105.21,
1053.28, 1086.7, 1050.98, 994.16, 1012.94, 997.99, 1034.13, 1005.44,
975.79, 1027.49, 1136.53, 1085.52, 1122.5, 1088.79, 1188.85,
1200.09, 1175.53, 1176.81, 1160.37, 1110.58, 1086.42, 1122.24,
1181.63, 1163.9, 1163, 1126.01, 1089.17, 1075.11, 1103.33, 1085.35,
1090.3, 1073.68, 1075.12, 1079.71, 1080.52, 1076.43, 1086.91,
1118.26, 1071, 1043.5, 1033.48, 1022.17, 1072.28, 1075.34, 1118.84,
1153.21, 1158.47, 1153.67, 1144.2, 1162.61, 1237.27, 1282.15,
1387.88, 1362.41, 1354.32, 1324.28, 1349.44, 1429.53), GBP = c(14.66,
14.54, 14.55, 14.67, 14.93, 14.8, 14.85, 14.81, 14.99, 15.23,
15.6, 15.69, 15.46, 15.75, 16.06, 16.08, 16.41, 16.89, 16.57,
17.53, 16.53, 17.15, 17, 17.48, 17.06, 18.18, 18.49, 18.5, 19,
22.75, 26.45, 27.88, 27.32, 26.53, 27.55, 27.04, 27.66, 27.73,
34.25, 36.3, 36.45, 44.73, 47.76, 46.23, 42.09, 41.43, 40.18,
43.07, 48.32, 58.06, 70.54, 72.23, 69.56, 65.41, 60.44, 65.55,
67.31, 64.84, 71.54, 79.11, 79.36, 73.88, 74.81, 73.84, 70.91,
72.26, 76.49, 77.62, 75.72, 69.15, 68.78, 68.48, 69.32, 63.13,
65.32, 67.61, 69.76, 71.37, 69.36, 63.01, 58.51, 69.84, 77.65,
78.82, 79.04, 77.14, 83.29, 86.57, 85.64, 83.16, 83.14, 82.96,
83.77, 88.15, 87.75, 88.08, 86.05, 90.13, 93.87, 97.32, 93.39,
100.46, 98.41, 103.68, 107.38, 109.84, 116.92, 99.46, 110.7,
117.44, 124.19, 116.22, 118.82, 132.69, 127.29, 131.87, 139.89,
180.36, 184.01, 188.97, 230.63, 288.11, 280.25, 228.51, 229.36,
228.36, 277.32, 262.16, 263.41, 279.27, 258.16, 262.83, 246.65,
213.98, 221.77, 228.89, 225.53, 231.52, 220.67, 220.47, 229.85,
237.53, 229.57, 212.02, 208.22, 205.74, 199.07, 179.57, 201.37,
181.65, 182.1, 197.3, 239.66, 234.29, 252.39, 268.56, 282.47,
328.62, 269.64, 279.58, 275.07, 272.67, 271.9, 277.5, 277.28,
270.54, 255.43, 276.26, 263.45, 266.68, 264.51, 269.32, 268.72,
277.34, 275.01, 261.84, 266.14, 278.34, 273.14, 274.62, 266.01,
271.13, 266.44, 266.06, 258.63, 244.07, 242.56, 232.52, 239.23,
231.19, 225.61, 218.54, 226.08, 248.05, 233.69, 231.81, 222.92,
233.07, 226.26, 239.53, 258.45, 292.47, 285.31, 271.71, 262.23,
264.62, 262.52, 262.31, 272.96, 276.69, 277.22, 290.52, 277.56,
282.68, 272.24, 269.72, 257.71, 258.76, 240.29, 242.03, 238.89,
247.76, 255.59, 255.36, 254.01, 234.59, 233.19, 228.37, 226.78,
225.08, 221.84, 227.01, 223.53, 230.08, 240.8, 221, 228.66, 226.94,
237.83, 260.13, 247.19, 247.05, 241.24, 223.67, 224.31, 216.49,
201.89, 200.16, 204.89, 217.99, 195.22, 198.43, 200.1, 186.26,
189.85, 204.51, 207.51, 212.12, 227.52, 215.34, 206.66, 202.45,
205.14, 207.54, 188.75, 197.82, 200.97, 196.89, 189.55, 184.53,
180.36, 186.38, 171.54, 195.9, 217.12, 220.81, 219.88, 222.23,
230.22, 224.38, 225.81, 241.8, 253.4, 270.54, 249.78, 237.71,
248.64, 249.85, 264.79, 252.13, 256.75, 262.17, 248.27, 256.39,
251.55, 249.91, 251.06, 250.38, 235.41, 244.77, 244.97, 236.16,
238.19, 240.67, 242.19, 241.92, 243.29, 239.58, 246.83, 242.62,
242.51, 253.55, 249.27, 268.4, 261.71, 259.65, 260.77, 252.07,
245.86, 247.45, 247.25, 242.42, 233.05, 220.97, 215.77, 215.6,
219.8, 212.03, 209.63, 211.25, 201.02, 199.3, 200.61, 205.58,
185.7, 176.16, 176.38, 186.45, 180.63, 179.75, 185.83, 180.05,
177.58, 176.56, 163.27, 172.91, 174.54, 178.54, 172.98, 173.73,
179.18, 173.11, 178.01, 167.53, 165.58, 157.78, 158.46, 181.56,
182.28, 182.93, 180.09, 174.78, 186.01, 173.48, 175.78, 182.14,
190.34, 184.79, 190.38, 185.09, 182.16, 189.83, 183.73, 181.03,
184.92, 181.27, 183.92, 188.3, 192.41, 186.58, 188.22, 199.43,
191.67, 193.19, 189.98, 199.75, 209.88, 211.66, 211.49, 223.22,
208.96, 195.02, 202.21, 205.84, 202.56, 205.04, 215.67, 223.58,
220.59, 211.84, 210.7, 220.59, 209.68, 220.69, 237.46, 233.54,
227.61, 231.62, 232.52, 219.61, 213.3, 230.54, 219.08, 214.49,
218.25, 215.24, 226.38, 229.7, 232.25, 237.2, 226.89, 223.82,
226.13, 226.24, 228.13, 227.4, 243.86, 243.67, 240.89, 267.51,
265.92, 286.44, 298.82, 320, 317.49, 335.53, 354.3, 348.96, 331.7,
338.77, 327.79, 320.81, 316.56, 328.77, 322.92, 332.33, 338.92,
337.4, 338.53, 333.2, 324.22, 327.5, 333.16, 364.69, 380.05,
381.05, 418.84, 464.41, 488.39, 469.69, 439.78, 448.21, 467.43,
463.43, 456.7, 496.23, 452.27, 530.81, 604.94, 637.79, 667.91,
639.41, 596.07, 604.94, 567.45, 566.4, 586.27, 622.6, 630.93,
716.44, 673.44, 673.05, 727.96, 735.38, 770.42, 831.07, 831.5,
746.44, 810.72, 829.42, 842.35, 888.37, 897.71, 828.47, 867.51,
897.72, 920.59, 933.5, 937.74, 992.08, 1113.77, 1039.93, 1066.85,
1110.12, 985.14, 1105.16, 1108.02, 1040.53, 1016.87, 1012.25,
1019.16, 1035.23, 1037.87, 1099.83, 1067.01, 1076.97, 1019.69,
1050.02, 1046.48, 1052.55, 943.85, 919.79, 785.92, 867.06, 901.64,
819.13, 824.1, 764.93, 727.25, 761.2, 791.54, 774.83, 763.08,
745.52, 769.07, 761.27, 774.2, 750.39, 727.72, 755.24, 773.45,
839.1, 785.56, 799.6, 768.02, 780.76, 744.58, 703.88, 737.97,
735.44, 739.67, 705.44, 719.18, 783.79, 886.12, 860.64, 877.64,
832.77, 987.99, 1010.77, 999.66, 1018.09, 1041.85, 942.93, 927.37,
963.99, 1009, 995.52, 978.9, 980.83, 956.35, 961.47, 1018.01,
956.36, 956.47, 945.74, 954.35, 945.85, 956.45, 943.72, 953.43,
980.99, 947.13, 930.78, 925.14, 910.43, 950.85, 954.27, 1004.24,
1005.93, 991.77, 994.13, 983.62, 1027.89, 1107.1, 1165.87, 1255,
1205.31, 1167.66, 1128.84, 1143.42, 1201.79), JPY = c(12592.44,
12496.35, 12529.95, 12627.65, 12858.36, 12757.88, 12764.72, 12728.16,
12799.92, 13018.66, 13341.25, 13403.93, 13239.03, 13613.21, 13867.1,
13892.95, 14188.38, 14594.98, 14328.74, 15153.74, 13538.98, 14059.41,
13957.5, 14250.08, 13691.48, 14624.17, 14619.8, 14695.64, 15092.19,
18096.15, 19085.52, 20559.69, 20090.92, 19326.34, 19410.68, 19127.31,
19568.2, 19897.07, 22585.63, 23834.22, 24145.47, 30392.78, 32263.28,
30535.41, 27419.59, 26534.94, 26098.24, 28232.64, 31384.58, 39453.89,
46802.46, 47602.1, 47234.18, 44183.03, 41041.61, 46668.94, 47254.41,
45131.84, 50150.83, 55215.86, 56029.4, 52492.53, 52067.51, 51834.86,
48788.12, 48777.59, 49565.26, 49794.25, 47625.81, 42800.94, 43162.37,
41975.69, 42788.35, 38950.05, 40006.82, 38873.24, 38474.65, 37717.84,
36897.27, 33015.97, 30015.22, 33347.41, 36300.55, 38700.65, 39440.3,
38185.75, 40377.01, 41380.39, 40936.12, 39666.76, 38285.79, 38493.04,
39036.11, 40680.98, 40276.31, 39109.66, 39538.1, 42495.47, 43462.15,
40096.07, 38241.67, 40838.54, 37298.9, 37789.89, 39731.94, 41080.4,
43492.65, 38590.52, 43893.74, 47327.21, 50918.25, 50379.21, 54567.22,
60506.23, 60400.85, 64222.08, 69384.93, 89144.59, 90807.88, 103747.21,
122753.24, 156299.39, 160301.01, 123625.03, 123852.21, 119317.25,
143581.44, 139733.44, 138287.63, 140611.01, 132821.9, 134174.05,
119873.91, 104745.2, 102568.08, 108495.29, 104139.22, 107310.34,
96652.77, 97559.14, 97458.09, 99645.86, 99518.62, 88783.57, 87350.12,
88366.05, 85797.72, 79371.51, 85177.74, 79200.8, 80809.09, 88289.82,
107608.45, 106483.75, 117233.06, 109033.58, 107339.81, 119945.76,
97069.41, 99109.49, 102120.63, 104637.36, 99037.93, 102048.79,
102037.58, 95501.08, 89401.56, 94067.22, 88651.45, 87803.93,
92049.07, 87261.11, 85317.53, 89025.08, 88552.94, 83983.73, 84168.28,
84476.24, 81941.05, 81426.13, 77540.56, 78085.95, 74735.07, 82478.9,
80887.16, 78835.6, 78952.48, 77544.39, 79544.62, 70455.94, 68753.77,
65726.53, 65450.46, 67594.74, 60993.19, 61718.4, 57680.74, 59899.76,
56735.83, 54972.42, 59443.07, 65322.55, 65478.19, 63105.32, 61492.07,
61523.85, 62150.7, 61445.03, 63736.17, 64952.37, 65632.63, 69287.93,
64394.79, 67278.39, 64725.48, 65203.72, 58756.86, 58543.8, 54725.53,
56695.21, 56080.25, 56984.01, 58274.86, 58095.28, 58358.48, 53135.74,
51768.65, 51497.63, 51252.96, 51430.46, 49082.16, 50737.68, 50127.67,
51480.31, 53698.52, 50443.76, 52020.64, 51173.85, 53571.85, 58335.06,
57287.2, 59972.28, 60672.52, 57987.09, 58375.7, 55420.9, 53602.28,
54393.81, 55780.68, 56458.81, 49292.09, 51243.87, 52377.16, 48101.57,
48220.78, 50208.24, 48765.23, 49902.39, 50793.19, 49851.52, 47532.41,
47120.95, 46668.54, 47629.39, 44120.18, 44460.3, 45619.62, 45481.28,
44875.08, 43087.08, 43240.65, 45569.99, 41899.63, 41874.13, 41795.6,
41568.03, 41557.52, 41222.92, 38676.62, 38817.25, 39404.32, 40441.07,
40416.99, 42136.42, 38903.5, 37736.97, 40092.83, 40475.44, 43721.26,
41192.97, 39738.41, 39990.29, 38204.02, 40564.26, 38298.92, 38467.2,
38642.5, 39078.28, 37214.24, 37907.73, 38236.85, 37096.34, 36397.87,
33868.79, 32746.79, 32529.05, 32860.52, 33750.11, 37451.17, 37881.59,
39124.04, 39373.31, 39920.98, 43359.35, 42136.35, 42330.2, 40990.61,
42218.43, 41899.67, 41121.12, 41968.46, 42211.13, 43164.33, 42253.91,
42858.84, 41943.7, 43279.41, 43104.44, 43168.42, 40272.75, 38282.53,
38639.82, 39136.33, 40094.41, 37452.06, 37879.08, 37733.25, 38654.96,
37570.53, 40139.85, 41052.78, 40682.66, 41121.96, 41704.14, 38601.33,
39998.84, 34061.7, 36224.52, 32463.81, 33186.3, 34058.46, 33095.26,
34209.99, 32714.12, 31591.44, 29333.93, 27937.54, 31834.53, 31214.06,
29771.58, 29708.54, 30325.83, 32258.91, 28384.86, 29726.01, 29314.52,
30487.7, 30326.26, 29539.28, 29570.61, 28880.75, 29809.53, 31342.18,
30757.38, 31282.57, 32294.96, 32508.22, 31784.35, 33749.21, 33206.91,
32488.36, 34916.98, 34120.39, 33919.56, 36238.09, 37764.65, 39725.93,
39946.03, 39572.87, 40532.67, 38175.41, 36484.87, 37087.1, 39407.23,
38823.41, 39088.39, 41202.22, 44077.95, 41077.27, 39706.49, 40160.8,
43225.23, 41545.95, 42763.34, 43824.99, 43345.42, 42462.39, 43627.27,
44609.51, 42313.53, 43250.55, 44079.62, 42872.92, 43463.95, 43187.69,
43623.47, 44693.65, 45810.85, 45189.13, 46607.24, 44635.91, 43673.51,
45410.89, 45725.4, 45711.46, 44667.34, 48435.03, 48088.75, 48149.24,
53638.15, 54797.65, 59306.99, 60549.39, 66580.71, 64398.7, 68670.17,
73544.8, 73207.82, 70132.24, 72380.14, 73127.19, 70750.44, 70732.33,
74794.08, 75305.96, 78684.47, 78697.72, 78136.13, 80904.88, 80288.24,
80333.49, 79237.75, 77894.88, 85456.14, 91017.5, 86909.73, 93142.38,
98164.55, 101138, 92915.91, 91028.21, 93504.19, 98611.14, 99222.02,
90401.32, 93902.94, 71869.26, 77577.05, 78842.83, 82580.29, 93148.43,
90522.69, 86898.55, 93116.34, 90165.22, 89388.1, 88636.96, 89154.46,
94140.79, 101284.97, 101240.8, 97771.42, 98484.63, 104232.31,
110861.29, 109924.75, 110081.55, 101305.53, 104626.6, 109186.77,
108480.7, 115895.78, 113993.07, 108721.1, 115603.22, 119264.31,
124590.47, 124825.25, 121584.17, 125703.89, 138678.32, 124869.58,
134272.93, 135541.96, 117795.12, 132980, 143263.78, 136815.43,
131844.03, 122170.56, 127544.3, 126678.19, 129077.53, 138172.78,
137399.66, 142343.2, 143315.73, 151925.08, 146555, 150267.45,
143095.27, 140809.62, 118407.31, 129287.64, 136845.88, 130162.81,
129897.62, 128263.33, 126598.97, 127583.22, 135389.21, 133030.85,
131620.27, 127232.12, 133216.07, 132168.67, 133570.13, 133443.96,
130518.24, 140374.68, 144593.36, 148035.26, 145127.63, 142350.96,
141240.52, 147846.75, 143289.4, 136086.24, 137544.97, 133418.21,
137853.05, 130911.02, 127512.7, 134600.03, 139389.33, 139032.6,
137558.11, 134427.93, 135495.74, 137534.85, 135422.27, 133922.96,
133668.11, 134238.58, 133652.03, 136530.93, 140482.78, 138713.61,
141171.16, 140022.71, 139579.19, 140057.91, 144371.2, 144432.12,
144327.12, 143260.77, 145431.14, 146818.91, 140621.15, 140791.42,
143696.89, 141819.72, 138506.09, 136673.12, 133333.66, 134853.78,
137113.17, 138252.77, 140325.48, 144009.28, 146847.76, 143381.34,
142816.14, 140664.32, 151805.65, 154996.22, 162231.99, 160523.77,
163341.24, 159901.01, 164615.44, 171703.5)), row.names = c(NA,
-602L), class = "data.frame")

R - How to perform cross-year date operations?

I am working with daily measurements of temperature. In total I have about 40 years of observations. How can I perform date operations covering a time interval that crosses years?
For example, I want to sum the values from every october-to-february period. However, the sum should be taken only on the contiguous period of oct-nov-dec-jan-feb.
"Isolated" months should not be taken into account, like for example jan and feb of the first year, and oct-nov-dec of the last year. The sum has to run over the contiguous period only (from oct-nov-dec-jan-fev).
For example, this is what I am looking for:
1st year 2nd year 3rd year
J-F-M-A-M-J-J-A-S-**O-N-D J-F**-M-A-M-J-J-A-S-**O-N-D J-F**-M-A-M-J-J-A-S-O-N-D
But this is not OK:
1st year 2nd year 3rd year
**J-F**-M-A-M-J-J-A-S-**O-N-D J-F**-M-A-M-J-J-A-S-**O-N-D J-F**-M-A-M-J-J-A-S-**O-N-D**
This is a sample data frame to work on:
df <- structure(list(date = structure(c(-3653, -3622, -3593, -3562,
-3532, -3501, -3471, -3440, -3409, -3379, -3348, -3318, -3287,
-3256, -3228, -3197, -3167, -3136, -3106, -3075, -3044, -3014,
-2983, -2953, -2922, -2891, -2863, -2832, -2802, -2771, -2741,
-2710, -2679, -2649, -2618, -2588, -2557, -2526, -2498, -2467,
-2437, -2406, -2376, -2345, -2314, -2284, -2253, -2223, -2192,
-2161, -2132, -2101, -2071, -2040, -2010, -1979, -1948, -1918,
-1887, -1857, -1826, -1795, -1767, -1736, -1706, -1675, -1645,
-1614, -1583, -1553, -1522, -1492, -1461, -1430, -1402, -1371,
-1341, -1310, -1280, -1249, -1218, -1188, -1157, -1127, -1096,
-1065, -1037, -1006, -976, -945, -915, -884, -853, -823, -792,
-762, -731, -700, -671, -640, -610, -579, -549, -518, -487, -457,
-426, -396, -365, -334, -306, -275, -245, -214, -184, -153, -122,
-92, -61, -31, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304,
334, 365, 396, 424, 455, 485, 516, 546, 577, 608, 638, 669, 699,
730, 761, 790, 821, 851, 882, 912, 943, 974, 1004, 1035, 1065,
1096, 1127, 1155, 1186, 1216, 1247, 1277, 1308, 1339, 1369, 1400,
1430, 1461, 1492, 1520, 1551, 1581, 1612, 1642, 1673, 1704, 1734,
1765, 1795, 1826, 1857, 1885, 1916, 1946, 1977, 2007, 2038, 2069,
2099, 2130, 2160, 2191, 2222, 2251, 2282, 2312, 2343, 2373, 2404,
2435, 2465, 2496, 2526, 2557, 2588, 2616, 2647, 2677, 2708, 2738,
2769, 2800, 2830, 2861, 2891, 2922, 2953, 2981, 3012, 3042, 3073,
3103, 3134, 3165, 3195, 3226, 3256, 3287, 3318, 3346, 3377, 3407,
3438, 3468, 3499, 3530, 3560, 3591, 3621, 3652, 3683, 3712, 3743,
3773, 3804, 3834, 3865, 3896, 3926, 3957, 3987, 4018, 4049, 4077,
4108, 4138, 4169, 4199, 4230, 4261, 4291, 4322, 4352, 4383, 4414,
4442, 4473, 4503, 4534, 4564, 4595, 4626, 4656, 4687, 4717, 4748,
4779, 4807, 4838, 4868, 4899, 4929, 4960, 4991, 5021, 5052, 5082,
5113, 5144, 5173, 5204, 5234, 5265, 5295, 5326, 5357, 5387, 5418,
5448, 5479, 5510, 5538, 5569, 5599, 5630, 5660, 5691, 5722, 5752,
5783, 5813, 5844, 5875, 5903, 5934, 5964, 5995, 6025, 6056, 6087,
6117, 6148, 6178, 6209, 6240, 6268, 6299, 6329, 6360, 6390, 6421,
6452, 6482, 6513, 6543, 6574, 6605, 6634, 6665, 6695, 6726, 6756,
6787, 6818, 6848, 6879, 6909, 6940, 6971, 6999, 7030, 7060, 7091,
7121, 7152, 7183, 7213, 7244, 7274), class = "Date"), temp = c(22.9223529411765,
23.0705882352941, 23.1094117647059, 20.7835294117647, 17.4517647058824,
17.3176470588235, 18.0494117647059, 19.6188235294118, 21.3023529411765,
23.1105882352941, 22.2364705882353, 22.7482352941176, 23.5870588235294,
24.0023529411765, 23.0094117647059, 22.0176470588235, 19.4917647058824,
18.1011764705882, 18.3164705882353, 20.0623529411765, 22.8717647058824,
23.2576470588235, 23.68, 22.3694117647059, 22.9517647058824,
23.6976470588235, 23.3294117647059, 20.8564705882353, 18.16,
15.8988235294118, 15.7988235294118, 18.4176470588235, 20.8423529411765,
20.3247058823529, 22.3070588235294, 22.2035294117647, 24.2235294117647,
23.6976470588235, 24.4082352941176, 21.1752941176471, 18.1023529411765,
16.1211764705882, 18.3164705882353, 19.7635294117647, 23.1294117647059,
22.9964705882353, 23.6552941176471, 22.6964705882353, 23.6011764705882,
23.6517647058824, 23.7035294117647, 22.4352941176471, 18.5835294117647,
16.5976470588235, 15.7741176470588, 19.2541176470588, 20.8776470588235,
20.5729411764706, 21.1729411764706, 21.5870588235294, 22.4576470588235,
23.6058823529412, 21.84, 21.6694117647059, 19.2458823529412,
18.7517647058824, 17.7811764705882, 19.4764705882353, 21.9270588235294,
21.5470588235294, 22.88, 23.2458823529412, 24.2776470588235,
25.2470588235294, 23.4694117647059, 21.4435294117647, 19.3941176470588,
18.5447058823529, 17.6, 18.3764705882353, 19.8529411764706, 22.0823529411765,
22.7294117647059, 23.4011764705882, 23.3611764705882, 24.2505882352941,
23.2870588235294, 21.9482352941176, 20.5552941176471, 18.0788235294118,
18.5929411764706, 20.8752941176471, 21.9023529411765, 23.6105882352941,
22.4070588235294, 21.5635294117647, 23.3129411764706, 22.9741176470588,
23.3670588235294, 19.6105882352941, 16.9941176470588, 17.7670588235294,
17.4858823529412, 17.8517647058824, 20.26, 22.1576470588235,
23.8364705882353, 23.4447058823529, 24.8129411764706, 25.1764705882353,
24.2694117647059, 21.5035294117647, 20.0458823529412, 18.4694117647059,
18.4541176470588, 19.5388235294118, 22.02, 20.5364705882353,
22.9858823529412, 21.9752941176471, 23.7729411764706, 24.0576470588235,
24.0941176470588, 22.1552941176471, 21.2329411764706, 19.5611764705882,
17.8788235294118, 18.6823529411765, 20.1541176470588, 21.6258823529412,
21.5211764705882, 23.9811764705882, 24.8352941176471, 24.5882352941176,
24.1729411764706, 21.1035294117647, 19.0435294117647, 17.08,
17.4529411764706, 19.1458823529412, 20.4447058823529, 20.7129411764706,
21.5047058823529, 22.6952941176471, 23.4364705882353, 23.1, 24.1847058823529,
19.8105882352941, 19.9847058823529, 20.5188235294118, 17.7658823529412,
19.4435294117647, 20.7588235294118, 21.7835294117647, 22.7788235294118,
23.2388235294118, 24.9129411764706, 25.6, 23.5647058823529, 24.0058823529412,
19.7823529411765, 19.3152941176471, 18.7741176470588, 19.0305882352941,
20.5576470588235, 21.3611764705882, 21.4247058823529, 23.4811764705882,
23.6505882352941, 25.1870588235294, 23.3541176470588, 21.4823529411765,
18.7364705882353, 17.7235294117647, 18.3976470588235, 19.7235294117647,
21.0741176470588, 21.6094117647059, 22.9635294117647, 22.4011764705882,
23.4152941176471, 24.7741176470588, 24.3270588235294, 20.7976470588235,
18.8764705882353, 17.7788235294118, 16.4129411764706, 21.4117647058824,
22.3317647058824, 21.66, 22.3694117647059, 23.0917647058824,
24.4541176470588, 23.2847058823529, 23.3164705882353, 21.2529411764706,
19.1258823529412, 17.3882352941176, 17.3823529411765, 19.0529411764706,
19.6576470588235, 20.2976470588235, 21.9023529411765, 23.3094117647059,
24.0117647058824, 25.5611764705882, 24.9129411764706, 21.3964705882353,
19.9870588235294, 18.3929411764706, 20.9917647058824, 20.3058823529412,
21.4435294117647, 23.1941176470588, 22.8388235294118, 22.5176470588235,
24.6317647058824, 24.6541176470588, 24.2, 20.84, 18.4576470588235,
17.5011764705882, 19.16, 20.54, 20.1517647058824, 22.6776470588235,
22.7470588235294, 22.7882352941176, 22.0811764705882, 24.2152941176471,
22.9235294117647, 20.8411764705882, 19.6188235294118, 17.16,
16.0529411764706, 20.3223529411765, 19.9752941176471, 22.5152941176471,
22.2705882352941, 23.1541176470588, 23.1047058823529, 23.9517647058824,
24.8176470588235, 22.18, 20.5023529411765, 17.3505882352941,
19.1917647058824, 19.9894117647059, 19.0235294117647, 22.8235294117647,
22.7094117647059, 23.8741176470588, 24.0517647058824, 25.1764705882353,
23.9235294117647, 21.2929411764706, 20.6117647058824, 17.1305882352941,
16.3470588235294, 19.6470588235294, 21.3341176470588, 20.2176470588235,
23.7435294117647, 22.6741176470588, 22.9070588235294, 24.7152941176471,
23.2905882352941, 20.5776470588235, 18.9635294117647, 19.0658823529412,
18.8423529411765, 20.0729411764706, 21.3047058823529, 22.1588235294118,
24.0388235294118, 22.1917647058824, 24.0517647058824, 24.8729411764706,
23.0117647058824, 23, 21.3094117647059, 19.4105882352941, 20.3470588235294,
19.4482352941176, 20.0670588235294, 21.6364705882353, 23.4211764705882,
23.16, 25.4788235294118, 26.4741176470588, 24.0482352941176,
21.4176470588235, 21.7164705882353, 19.0905882352941, 19.6752941176471,
18.1611764705882, 20.0482352941176, 23.4917647058824, 23.4894117647059,
22.5482352941176, 23.1376470588235, 24.9811764705882, 24.1552941176471,
22.8423529411765, 19.7435294117647, 16.4, 17.3105882352941, 20.5235294117647,
21.0494117647059, 23.1352941176471, 23.9435294117647, 23.9058823529412,
24.9835294117647, 24.6952941176471, 24.0047058823529, 23.3164705882353,
21.5823529411765, 18.3447058823529, 18.1964705882353, 20.0035294117647,
20.7152941176471, 22.5705882352941, 24.6541176470588, 23.2329411764706,
25.0517647058824, 24.3329411764706, 23.5811764705882, 22.9988235294118,
19.4976470588235, 17.3188235294118, 19.5635294117647, 19.0211764705882,
19.7223529411765, 22.6858823529412, 23.9423529411765, 23.6905882352941,
25.7129411764706, 23.9505882352941, 24.4376470588235, 22.6070588235294,
19.8882352941176, 17.2058823529412, 16.4211764705882, 20.02,
21.9458823529412, 21.9341176470588, 22.74, 23.8, 23.9611764705882,
24.4564705882353, 24, 23.2129411764706, 19.4729411764706, 17.7105882352941,
16.9682352941176, 19.0341176470588, 20.2917647058824, 20.7776470588235,
22.9364705882353, 22.7894117647059)), .Names = c("date", "temp"
), row.names = c(NA, -360L), class = "data.frame")
Any input appreciated.
Hopefully this helps:
df$date = as.POSIXct(df$date,format="%Y-%m-%d")
df$year = as.numeric(format(df$date,format="%Y"))
df$month = as.numeric(format(df$date,format="%m"))
years = unique(df$year)
# initialize a new data frame to store in your summed values
newdf=NULL
# run through a loop starting at your second year and ending at second last
for(i in 2:(length(years)-1)){
#data from year1
start = df[df$year==years[i] & df$month %in% c(10,11,12),]
end = df[df$year==years[i+1] & df$month %in% c(1,2),]
data1 = rbind(start,end)
# in case you have NAs in your data you can add ra.rm = T
sum.data = sum(data1$temp,na.rm = T)
df1 = as.data.frame(list(Year = years[i],
sum.data = sum.data))
# or paste year 1 and year 2 together
#df1 = as.data.frame(list(Year = paste(years[i],years[i+1],sep="-"),
# sum.data = sum.data))
newdf = rbind(newdf,df1)
}
head(newdf)

Resources