Krug
Krug

Reputation: 1013

Subsetting xts objects - index functions not working

In an effort to subset an xts object, I am using the .index* family of functions (http://www.inside-r.org/packages/cran/xts/docs/indexClass) as shown in one of the answers to this question: Return data subset time frames within another timeframes?.

The sample xts object used in this example has 1000 hourly data points from 2015-12-29 to 2016-02-29. The code below attempts to create subsets for various time selection criteria. Some of the functions are not working: indexyear, indexweek, indexday and indexmin fail to produce any output (please note the rest of the functions work as intended). Would anyone know why? Thanks very much!

library(xts)        

DATSxts <- structure(c(1069.4, 1070.7, 1070.4, 1070.3, 1068.7, 1068.7, 1069.4, 1069.7, 1069.5, 1068.1, 1069.3, 1069.2, 1069.3, 1070.7, 1070.9, 1070.7, 1070.8, 1070.5, 1070.5, 1069.7, 1068.9, 1070.2, 1067.3, 1067.8, 1067.9, 1061.7, 1060.8, 1061.7, 1060.9, 1060.2, 1060.9, 1061, 1060.3, 1061.2, 1061.9, 1062, 1061.9, 1062.5, 1062.3, 1062.3, 1062, 1062.7, 1063, 1062, 1062.6, 1062.4, 1061.8, 1058.9, 1061.5, 1062.4, 1061.4, 1060.9, 1062, 1060.6, 1059.4, 1060.5, 1061.3, 1063.1, 1064.6, 1063.5, 1064.3, 1063.7, 1065.3, 1068.7, 1069.8, 1071.3, 1072.8, 1073.8, 1072.9, 1072.7, 1072.6, 1078.8, 1080.6, 1078.2, 1073.7, 1076.4, 1075.2, 1075.4, 1075.4, 1074.7, 1073.2, 1076, 1076.5, 1076.2, 1077.1, 1077.6, 1078.5, 1079.1, 1077, 1077, 1078.1, 1077.7, 1080, 1079.7, 1075.6, 1077.2, 1077.2, 1080, 1080, 1078.8, 1079, 1078.9, 1077.8, 1078, 1077.7, 1076.1, 1077, 1078.4, 1078.6, 1079.5, 1080.5, 1082.1, 1083.3, 1083.5, 1085.6, 1085.2, 1088.8, 1085.1, 1090.5, 1088, 1091.1, 1091.4, 1094.3, 1094.4, 1094.2, 1094.1, 1093.1, 1093.1, 1097.6, 1098.7, 1098.2, 1100.1, 
   1099.4, 1100.1, 1097.6, 1096.1, 1097, 1096.4, 1098.5, 1100.8, 1101.8, 1106.4, 1103.5, 1106.4, 1109.5, 1108.7, 1110.4, 1109.8, 1109.5, 1109.9, 1109.1, 1104.3, 1105.2, 1104.1, 1103.5, 1104, 1102.9, 1101.7, 1099.8, 1096.8, 1098.6, 1100, 1100.1, 1100, 1101.9, 1103.7, 1103.8, 1101.2, 1098.9, 1103.1, 1104.6, 1104.9, 1107.2, 1107.1, 1106.1, 1105.2, 1104.5, 1104.4, 1105.6, 1107.4, 1106.7, 1100.8, 1100, 1104.4, 1103.4, 1104, 1101.4, 1102, 1100, 1098.9, 1099.6, 1097.3, 1096.8, 1095.4, 1094.5, 1096.3, 1095.4, 1095.8, 1096, 1097.4, 1098.4, 1096.3, 1095, 1095.4, 1096.9, 1095.4, 1092.3, 1091.6, 1087.8, 1089, 1085.8, 1088.8, 1086.8, 1086.8, 1087.5, 1090.3, 1090, 1086.8, 1087.1, 1086.6, 1086.2, 1085, 1084.4, 1083.9, 1085.7, 1085.9, 1082.9, 1082.7, 1081.7, 1082.7, 1083.3, 1083.2, 1082.2, 1088.7, 1089.5, 1092.4, 1091.6, 1093, 1094.4, 1095.2, 1094.1, 1095.8, 1094.5, 1093.1, 1093.7, 1093.1, 1095.4, 1092.3, 1091.5, 1089.1, 1092.5, 1092.4, 1091.4, 1092.3, 1086.1, 1084.8, 1088.7, 1082.9, 1082.6, 1078.4, 1073.6, 1076.9, 1077.6, 1079.1, 1077.7, 1078.6, 1078.8, 1080.4, 1081.2, 1081.5, 1081.1, 1083.6, 1084.9, 1084.2, 1083.9, 1081.3, 1082.4, 1084.6, 1094.6, 1094.1, 1091.1, 1090.7, 1090.8, 1090.3, 1089.3, 1088.8, 1089.4, 1091, 1091.5, 1091.9, 1093.3, 1092.4, 1093.1, 1090.9, 1090.6, 1091, 1089.3, 1091.8, 1091.2, 1090.5, 1091, 1090.6, 1089.8, 1089.7, 1090.1, 1089.7, 1089.3, 1089.1, 1086.4, 1090.6, 1090.3, 1089, 1089.3, 1089.5, 1090.8, 1093.3, 1089.5, 1087.8, 1084.7, 1085.3, 1086.7, 1086.8, 1086.9, 1087.7, 1090.1, 1090.8, 1088.3, 1087.9, 1088.1, 1088.6, 1090.1, 1092.2, 1091.6, 1093.3, 1093.3, 1091.9, 1092.7, 1094.8, 1096.3, 1094.1, 1095.5, 1095.9, 1102.5, 1100.5, 1101.9, 1102.8, 1104.3, 1106.8, 1105.6, 1104.5, 1102.7, 1101.8, 1100.4, 1099.7, 1100.7, 1100.3, 1100.9, 1103, 1104.3, 1103.8, 1104.1, 1101.4, 1099.7, 1098.3, 1100.2, 1100.6, 1098.6, 1097.2, 1095.7, 1095.4, 1096, 1101.8, 1101.6, 1103.5, 1102.2, 1101.7, 1100.3, 1100.4, 1100.1, 1102.1, 1100.6, 1099.8, 1099.5, 1097.8, 1096.4, 1098.1, 1098.7, 1099.1, 1098.9, 1096.2, 1097.3, 1101.3, 1100, 1099, 1097, 1097.8, 1098.8, 1099, 1099.1, 1099.3, 1101.3, 1102.1, 1102.9, 1103.1, 1101.9, 1102.3, 1105.2, 1104.3, 1105.7, 1105.1, 1107.5, 1105.4, 1107.9, 1107.4, 1107.5, 1108.8, 1107, 1106.4, 1106.5, 1109.6, 1108.9, 1109.7, 1109.4, 1110.9, 1112.2, 1114.1, 1113.5, 1113.5, 1115.9, 1117.3, 1115.2, 1114.5, 1114.9, 1112.7, 1113.2, 1114.2, 1114.9, 1119.6, 1118.8, 1119.6, 1122.1, 1123.1, 1122.6, 1120.7, 1120.5, 1121.4, 1121.3, 1122, 1122.3, 1121, 1121.5, 1120.4, 1119, 1118.4, 1119.7, 1117.9, 1118.9, 1119.8, 1119.4, 1117.6, 1117.2, 1117.5, 1118.2, 1117.3, 1128.3, 1126.4, 1125.7, 1125.3, 1122.8, 1120.6, 1121, 1122.3, 1121.1, 1120.3, 1118, 1118.5, 1119.3, 1118.2, 1118.9, 1122.1, 1120.3, 1120.8, 1114, 1115.9, 1116.5, 1116.1, 1115.8, 1115.9, 1114.3, 1115.6, 1114.6, 1114.8, 1116.3, 1114.9, 1112.7, 1115.7, 1114.6, 1115.5, 1113.4, 1111.7, 1113.4, 1112.8, 1112.1, 1115.4, 1113.5, 1112.4, 1118, 1117, 1117.1, 1116.6, 1116.5, 1117.9, 1118.4, 1117.8, 1118.4, 1120.4, 1121.4, 1122.5, 1122.3, 1122.3, 1121.8, 1122.9, 1122, 1122, 1123, 1121.8, 1123.9, 1122.9, 1126.4, 1126.3, 1126.7, 1127.2, 1127.6, 1128.8, 1129.7, 1128.6, 1128.6, 1129.5, 1127.6, 1127.4, 1126.1, 1125.8, 1125.8, 1125.9, 1126.1, 1126.4, 1125.8, 1125.2, 1124.8, 1125.4, 1127.4, 1128.9, 1128.3, 1125, 1126.3, 1128, 1128.3, 1130.1, 1129.6, 1127.9, 1127.6, 1128.4, 1128.7, 1127.8, 1127.1, 1128.5, 1128.5, 1124.9, 1126.8, 1128.2, 1130.4, 1128.6, 1130.2, 1126.7, 1132.5, 1138.2, 1138.6, 1140, 1144.2, 1138.9, 1142.8, 1143, 1142.6, 1143.5, 1141.8, 1141.3, 1141.2, 1141.8, 1143.5, 1143.9, 1142.4, 1145.4, 1146.4, 1147.6, 1145.8, 1148.2, 1153.5, 1155.3, 1152.1, 1154.6, 1155.9, 1155.5, 1156.4, 1156.4, 1156, 1156, 1156.1, 1154.8, 1156.4, 1156.1, 1155.7, 1155.3, 1154.8, 1154.8, 1156.3, 1157.9, 1159.4, 1159.4, 1159, 1151.5, 1149.9, 1154, 1155.3, 1157.6, 1157.9, 1162.8, 1174.5, 1174.1, 1174.1, 1165.3, 1168.5, 1165.4, 1165.5, 1166.2, 1166.7, 1166.2, 1166.4, 1165.8, 1168.5, 1173.8, 1176.4, 1175.9, 1182.5, 1182.1, 1192.1, 1193.6, 1194.4, 1195.6, 1201, 1195.5, 1190.7, 1189.6, 1191.5, 
   1193.2, 1193.9, 1194, 1193.2, 1193.1, 1191.8, 1193.4, 1186.7, 1188.7, 1189.5, 1189.3, 1191.7, 1196.6, 1198.5, 1189.7, 1195.5, 1195.6, 1197.3, 1195.2, 1190.8, 1187.9, 1189.4, 1189.6, 1192.1, 1193.4, 1191.5, 1191.3, 1191.8, 1191.8, 1191.2, 1188, 1187, 1182.3, 1182.6, 1182.8, 1183.7, 1187.9, 1191, 1192.6, 1193.3, 1192.1, 1192.5, 1194, 1196.9, 1197.4, 1197.4, 1199, 1208.7, 1208.4, 1207, 1206.1, 1209.3, 1206.7, 1208.2, 1207.6, 1217.6, 1217.6, 1224.8, 1237.3, 1235.9, 1236, 1233, 1247.1, 1254.5, 1248.4, 1249.4, 1245, 1245.1, 1247, 1240.7, 1238.6, 1236.3, 1238.2, 1233.9, 1233.5, 1239.3, 1245, 1242.6, 1239.5, 1238.4, 1242.6, 1238, 1238.7, 1236, 1239.5, 1238.4, 1237.8, 1236.9, 1239.3, 1238.7, 1238.5, 1238.5, 1233.8, 1228.1, 1223.7, 1224.7, 1221, 1220.6, 1219.8, 1219.5, 1212.4, 1210.1, 1209.5, 1210.2, 1209.9, 1209.6, 1211.7, 1210, 1205.7, 1207.4, 1209.4, 1208.4, 1208.4, 1204.7, 1198.4, 1193.4, 1194.2, 1197.9, 1201.3, 1196.2, 1200, 1211, 1212, 1215.4, 1213.9, 1215.3, 1211.2, 1216.4, 1213, 1209.1, 1204.2, 1204.2, 1200.4, 1200.9, 1203.2, 1198.2, 1202.6, 1203.7, 1209.3, 1209.1, 1206.8, 1207.3, 1211.5, 1204.6, 1202.9, 1203, 1202.3, 1207, 1209.4, 1210.9, 1210.6, 1212.8, 1210.2, 1210.3, 1208.9, 1208.3, 1209, 1207.4, 1209.3, 1209.1, 1210.5, 1210.1, 1209.5, 1209.9, 1208.1, 1207.5, 1207.6, 1206.8, 1203.4, 1204.3, 1205.8, 1206.9, 1210.8, 1215.8, 1218.8, 1227.8, 1232.9, 1234.7, 1237.5, 1231.3, 1231.6, 1232.8, 1227.2, 1227.5, 1228.4, 1228, 1226.7, 1226.5, 1224.4, 1224.2, 1221.5, 1222.9, 1230.8, 1231.7, 1230.7, 1229.3, 1231.6, 1233, 1231.3, 1231.9, 1232.7, 1229.6, 1226.6, 1226.1, 1225.9, 1223.5, 1223.3, 1217.1, 1216.3, 1217, 1217.7, 1211, 1209.5, 1206.6, 1205.2, 1204, 1207.6, 1209.1, 1212.6, 1212.2, 1210.3, 1211.1, 1209.2, 1208.2, 1207.9, 1208.9, 1209.3, 1211.2, 1215.8, 1215.7, 1221.1, 1220.5, 1216.6, 1216.7, 1217.8, 1216.9, 1219.2, 1218.8, 1217.4, 1219.3, 1218.6, 1221.6, 1225.6, 1224.6, 1223.8, 1222.8, 1223.6, 1226.1, 1226, 1231.1, 1230, 1225.9, 1228, 1228.6, 1228.7, 1228.1, 1226, 1224.2, 1226.2, 1230.6, 1237.1, 1238, 1236.1, 1244.4, 1250.2, 1248.6, 1245.6, 1238.4, 1239.7, 1230.5, 1230.3, 1229.4, 1225, 1228.5, 1236.3, 1233.3, 1234.4, 1233.4, 1234.5, 1237.2, 1241, 1239.5, 1237.4, 1236, 1233.3, 1236.5, 1234.8, 1236.4, 1239.2, 1239.8, 1242.1, 1235.1, 1239.1, 1233.6, 1233.7, 1234.5, 1237.5, 1239, 1237.6, 1236.5, 1238.1, 1240, 1239.6, 1237, 1231.4, 1233.1, 1233.4, 1236.6, 1237.1, 1229.3, 1227.6, 1217.5, 1219.2, 1220.4, 1222.3, 1226.2, 1224.9, 1222.8, 1221.9, 1221.8, 1226.4, 1226.4, 1225.2, 1226.8, 1229, 1227.3, 1230.8, 1234.1, 1235.1, 1234.9, 1229.9, 1231.3, 1229.2, 1234.6, 1232.4, 1233.8, 1232.6, 1234.6, 1239.6, 1240.9, 1239.3, 1241.1, 1241.4, 1247.2, 1244.8, 1244.4, 1245.7), 
   .Dim = c(1000L, 1L), index = structure(c(1451386800, 1451390400, 1451394000, 1451397600, 1451401200, 1451404800, 1451408400, 1451412000, 1451415600, 1451419200, 1451422800, 1451430000, 1451433600, 1451437200, 1451440800, 1451444400, 1451448000, 1451451600, 1451455200, 1451458800, 1451462400, 1451466000, 1451469600, 1451473200, 1451476800, 1451480400, 1451484000, 1451487600, 1451491200, 1451494800, 1451498400, 1451502000, 1451505600, 1451509200, 1451516400, 1451520000, 1451523600, 1451527200, 1451530800, 1451534400, 1451538000, 1451541600, 1451545200, 1451548800, 1451552400, 1451556000, 1451559600, 1451563200, 1451566800, 1451570400, 1451574000, 1451577600, 1451581200, 1451584800, 1451588400, 1451592000, 1451595600, 1451862000, 1451865600, 1451869200, 1451872800, 1451876400, 1451880000, 1451883600, 1451887200, 1451890800, 1451894400, 1451898000, 1451901600, 1451905200, 1451908800, 1451912400, 1451916000, 1451919600, 1451923200, 1451926800, 1451930400, 1451934000, 1451937600, 1451941200, 1451948400, 1451952000, 1451955600, 1451959200, 1451962800, 1451966400, 1451970000, 1451973600, 1451977200, 1451980800, 1451984400, 1451988000, 1451991600, 1451995200, 1451998800, 1452002400, 1452006000, 1452009600, 1452013200, 1452016800, 1452020400, 1452024000, 1452027600, 1452034800, 1452038400, 1452042000, 1452045600, 1452049200, 1452052800, 1452056400, 1452060000, 1452063600, 1452067200, 1452070800, 1452074400, 1452078000, 1452081600, 1452085200, 1452088800, 1452092400, 1452096000, 1452099600, 1452103200, 1452106800, 1452110400, 1452114000, 1452121200, 1452124800, 1452128400, 1452132000, 1452135600, 1452139200, 1452142800, 1452146400, 1452150000, 1452153600, 1452157200, 1452160800, 1452164400, 1452168000, 1452171600, 1452175200, 1452178800, 1452182400, 1452186000, 1452189600, 1452193200, 1452196800, 1452200400, 1452207600, 1452211200, 1452214800, 1452218400, 1452222000, 1452225600, 1452229200, 1452232800, 1452236400, 1452240000, 1452243600, 1452247200, 1452250800, 1452254400, 1452258000, 1452261600, 1452265200, 1452268800, 1452272400, 1452276000, 1452279600, 1452283200, 1452286800, 1452466800, 1452470400, 1452474000, 1452477600, 1452481200, 1452484800, 1452488400, 1452492000, 1452495600, 1452499200, 1452502800, 1452506400, 1452510000, 1452513600, 1452517200, 1452520800, 1452524400, 1452528000, 1452531600, 1452535200, 1452538800, 1452542400, 1452546000, 1452553200, 1452556800, 1452560400, 1452564000, 1452567600, 1452571200, 1452574800, 1452578400, 1452582000, 1452585600, 1452589200, 1452592800, 1452596400, 1452600000, 1452603600, 1452607200, 1452610800, 1452614400, 1452618000, 1452621600, 1452625200, 1452628800, 1452632400, 1452639600, 1452643200, 1452646800, 1452650400, 1452654000, 1452657600, 1452661200, 1452664800, 1452668400, 1452672000, 1452675600, 1452679200, 1452682800, 1452686400, 1452690000, 1452693600, 1452697200, 1452700800, 1452704400, 1452708000, 1452711600, 1452715200, 1452718800, 1452726000, 1452729600, 1452733200, 1452736800, 1452740400, 1452744000, 1452747600, 1452751200, 1452754800, 1452758400, 1452762000, 1452765600, 1452769200, 1452772800, 1452776400, 1452780000, 1452783600, 1452787200, 1452790800, 1452794400, 1452798000, 1452801600, 1452805200, 1452812400, 1452816000, 1452819600, 1452823200, 1452826800, 1452830400, 1452834000, 1452837600, 1452841200, 1452844800, 1452848400, 1452852000, 1452855600, 1452859200, 1452862800, 1452866400, 1452870000, 1452873600, 1452877200, 1452880800, 1452884400, 1452888000, 1452891600, 1453071600, 1453075200, 1453078800, 1453082400, 1453086000, 1453089600, 1453093200, 1453096800, 1453100400, 1453104000, 1453107600, 1453111200, 1453114800, 1453118400, 1453122000, 1453125600, 1453129200, 1453132800, 1453136400, 1453158000, 1453161600, 1453165200, 1453168800, 1453172400, 1453176000, 1453179600, 1453183200, 1453186800, 1453190400, 1453194000, 1453197600, 1453201200, 1453204800, 1453208400, 1453212000, 1453215600, 1453219200, 1453222800, 
   1453226400, 1453230000, 1453233600, 1453237200, 1453244400, 1453248000, 1453251600, 1453255200, 1453258800, 1453262400, 1453266000, 1453269600, 1453273200, 1453276800, 1453280400, 1453284000, 1453287600, 1453291200, 1453294800, 1453298400, 1453302000, 1453305600, 1453309200, 1453312800, 1453316400, 1453320000, 1453323600, 1453330800, 1453334400, 1453338000, 1453341600, 1453345200, 1453348800, 1453352400, 1453356000, 1453359600, 1453363200, 1453366800, 1453370400, 1453374000, 1453377600, 1453381200, 1453384800, 1453388400, 1453392000, 1453395600, 1453399200, 1453402800, 1453406400, 1453410000, 1453417200, 1453420800, 1453424400, 1453428000, 1453431600, 1453435200, 1453438800, 1453442400, 1453446000, 1453449600, 1453453200, 1453456800, 1453460400, 1453464000, 1453467600, 1453471200, 1453474800, 1453478400, 1453482000, 1453485600, 1453489200, 1453492800, 1453496400, 1453676400, 1453680000, 1453683600, 1453687200, 1453690800, 1453694400, 1453698000, 1453701600, 1453705200, 1453708800, 1453712400, 1453716000, 1453719600, 1453723200, 1453726800, 1453730400, 1453734000, 1453737600, 1453741200, 1453744800, 1453748400, 1453752000, 1453755600, 1453762800, 1453766400, 1453770000, 1453773600, 1453777200, 1453780800, 1453784400, 1453788000, 1453791600, 1453795200, 1453798800, 1453802400, 1453806000, 1453809600, 1453813200, 1453816800, 1453820400, 1453824000, 1453827600, 1453831200, 1453834800, 1453838400, 1453842000, 1453849200, 1453852800, 1453856400, 1453860000, 1453863600, 1453867200, 1453870800, 1453874400, 1453878000, 1453881600, 1453885200, 1453888800, 1453892400, 1453896000, 1453899600, 1453903200, 1453906800, 1453910400, 1453914000, 1453917600, 1453921200, 1453924800, 1453928400, 1453935600, 1453939200, 1453942800, 1453946400, 1453950000, 1453953600, 1453957200, 1453960800, 1453964400, 1453968000, 1453971600, 1453975200, 1453978800, 1453982400, 1453986000, 1453989600, 1453993200, 1453996800, 1454000400, 1454004000, 1454007600, 1454011200, 1454014800, 1454022000, 1454025600, 1454029200, 1454032800, 1454036400, 1454040000, 1454043600, 1454047200, 1454050800, 1454054400, 1454058000, 1454061600, 1454065200, 1454068800, 1454072400, 1454076000, 1454079600, 1454083200, 1454086800, 1454090400, 1454094000, 1454097600, 1454101200, 1454281200, 1454284800, 1454288400, 1454292000, 1454295600, 1454299200, 1454302800, 1454306400, 1454310000, 1454313600, 1454317200, 1454320800, 1454324400, 1454328000, 1454331600, 1454335200, 1454338800, 1454342400, 1454346000, 1454349600, 1454353200, 1454356800, 1454360400, 1454367600, 1454371200, 1454374800, 1454378400, 1454382000, 1454385600, 1454389200, 1454392800, 1454396400, 1454400000, 1454403600, 1454407200, 1454410800, 1454414400, 1454418000, 1454421600, 1454425200, 1454428800, 1454432400, 1454436000, 1454439600, 1454443200, 1454446800, 1454454000, 1454457600, 1454461200, 1454464800, 1454468400, 1454472000, 1454475600, 1454479200, 1454482800, 1454486400, 1454490000, 1454493600, 1454497200, 1454500800, 1454504400, 1454508000, 1454511600, 1454515200, 1454518800, 1454522400, 1454526000, 1454529600, 1454533200, 1454540400, 1454544000, 1454547600, 1454551200, 1454554800, 1454558400, 1454562000, 1454565600, 1454569200, 1454572800, 1454576400, 1454580000, 1454583600, 1454587200, 1454590800, 1454594400, 1454598000, 1454601600, 1454605200, 1454608800, 1454612400, 1454616000, 1454619600, 1454626800, 1454630400, 1454634000, 1454637600, 1454641200, 1454644800, 1454648400, 1454652000, 
   1454655600, 1454659200, 1454662800, 1454666400, 1454670000, 1454673600, 1454677200, 1454680800, 1454684400, 1454688000, 1454691600, 1454695200, 1454698800, 1454702400, 1454706000, 1454709600, 1454886000, 1454889600, 1454893200, 1454896800, 1454900400, 1454904000, 1454907600, 1454911200, 1454914800, 1454918400, 1454922000, 1454925600, 1454929200, 1454932800, 1454936400, 1454940000, 1454943600, 1454947200, 1454950800, 1454954400, 1454958000, 1454961600, 1454965200, 1454972400, 1454976000, 1454979600, 1454983200, 1454986800, 1454990400, 1454994000, 1454997600, 1455001200, 1455004800, 1455008400, 1455012000, 1455015600, 1455019200, 1455022800, 1455026400, 1455030000, 1455033600, 1455037200, 1455040800, 1455044400, 1455048000, 1455051600, 1455058800, 1455062400, 1455066000, 1455069600, 1455073200, 1455076800, 1455080400, 1455084000, 1455087600, 1455091200, 1455094800, 1455098400, 1455102000, 1455105600, 1455109200, 1455112800, 1455116400, 1455120000, 1455123600, 1455127200, 1455130800, 1455134400, 1455138000, 1455141600, 1455145200, 1455148800, 1455152400, 1455156000, 
   1455159600, 1455163200, 1455166800, 1455170400, 1455174000, 1455177600, 1455181200, 1455184800, 1455188400, 1455192000, 1455195600, 1455199200, 1455202800, 1455206400, 1455210000, 1455213600, 1455217200, 1455220800, 1455224400, 1455231600, 1455235200, 1455238800, 1455242400, 1455246000, 1455249600, 1455253200, 1455256800, 1455260400, 1455264000, 1455267600, 1455271200, 1455274800, 1455278400, 1455282000, 1455285600, 1455289200, 1455292800, 1455296400, 1455300000, 1455303600, 1455307200, 1455310800, 1455490800, 1455494400, 1455498000, 1455501600, 1455505200, 1455508800, 1455512400, 1455516000, 1455519600, 1455523200, 1455526800, 1455530400, 1455534000, 1455537600, 1455541200, 1455544800, 1455548400, 1455552000, 1455555600, 1455577200, 1455580800, 1455584400, 1455588000, 1455591600, 1455595200, 1455598800, 1455602400, 1455606000, 1455609600, 1455613200, 1455616800, 1455620400, 1455624000, 1455627600, 1455631200, 1455634800, 1455638400, 1455642000, 1455645600, 1455649200, 1455652800, 1455656400, 1455663600, 1455667200, 1455670800, 1455674400, 1455678000, 1455681600, 1455685200, 1455688800, 1455692400, 1455696000, 1455699600, 1455703200, 1455706800, 1455710400, 1455714000, 1455717600, 1455721200, 1455724800, 1455728400, 1455732000, 1455735600, 1455739200, 1455742800, 1455750000, 1455753600, 1455757200, 1455760800, 1455764400, 1455768000, 1455771600, 1455775200, 1455778800, 1455782400, 1455786000, 1455789600, 1455793200, 1455796800, 1455800400, 1455804000, 1455807600, 1455811200, 1455814800, 1455818400, 1455822000, 1455825600, 1455829200, 1455836400, 1455840000, 1455843600, 1455847200, 1455850800, 1455854400, 1455858000, 1455861600, 1455865200, 1455868800, 1455872400, 1455876000, 1455879600, 1455883200, 1455886800, 1455890400, 1455894000, 1455897600, 1455901200, 1455904800, 1455908400, 1455912000, 1455915600, 1456095600, 1456099200, 1456102800, 1456106400, 1456110000, 1456113600, 1456117200, 1456120800, 1456124400, 1456128000, 1456131600, 1456135200, 1456138800, 1456142400, 1456146000, 1456149600, 1456153200, 1456156800, 1456160400, 1456164000, 1456167600, 1456171200, 1456174800, 1456182000, 1456185600, 1456189200, 1456192800, 1456196400, 1456200000, 1456203600, 1456207200, 1456210800, 1456214400, 1456218000, 1456221600, 1456225200, 1456228800, 1456232400, 1456236000, 1456239600, 1456243200, 1456246800, 1456250400, 1456254000, 1456257600, 1456261200, 1456268400, 1456272000, 1456275600, 1456279200, 1456282800, 1456286400, 1456290000, 1456293600, 1456297200, 1456300800, 1456304400, 1456308000, 1456311600, 1456315200, 1456318800, 1456322400, 1456326000, 1456329600, 1456333200, 1456336800, 1456340400, 1456344000, 1456347600, 1456354800, 1456358400, 1456362000, 1456365600, 1456369200, 1456372800, 1456376400, 1456380000, 1456383600, 1456387200, 1456390800, 1456394400, 1456398000, 1456401600, 1456405200, 1456408800, 1456412400, 1456416000, 1456419600, 1456423200, 1456426800, 1456430400, 1456434000, 1456441200, 1456444800, 1456448400, 1456452000, 1456455600, 1456459200, 1456462800, 1456466400, 1456470000, 1456473600, 1456477200, 1456480800, 1456484400, 1456488000, 1456491600, 1456495200, 1456498800, 1456502400, 1456506000, 1456509600, 1456513200, 1456516800, 1456520400, 1456700400, 1456704000, 1456707600, 1456711200, 1456714800, 1456718400, 1456722000, 1456725600, 1456729200, 1456732800, 1456736400, 1456740000, 1456743600, 1456747200, 1456750800, 1456754400, 1456758000, 1456761600, 1456765200, 1456768800, 1456772400, 1456776000, 1456779600, 1456786800, 1456790400, 1456794000, 1456797600, 1456801200, 1456804800), 
   tzone = "", tclass = c("POSIXlt","POSIXt")), class = c("xts", "zoo"), .indexCLASS = c("POSIXlt", "POSIXt"), tclass = c("POSIXlt", "POSIXt"), .indexTZ = "", tzone = "", .CLASS = "double")

#THESE WORK AS INTENDED
SelectedDates <- DATSxts['2015-12-30::2015-12-31']    #get data between those dates
SelectedMonth <- DATSxts[.indexmon(DATSxts)==1]       #get data for months February
SelectedDayYr <- DATSxts[.indexyday(DATSxts)==5]      #get data for 5th trading days of the year 
SelectedDayMon <- DATSxts[.indexmday(DATSxts)==10]    #get data for 10th day of every month
SelectedDayWk <- DATSxts[.indexwday(DATSxts)==1]      #get data for mondays
SelectedHour <- DATSxts[.indexhour(DATSxts)==08]      #get data for 8AM

#THESE DO NOT WORK
SelectedYear <- DATSxts[.indexyear(DATSxts)==2016]    #get data for year 2016
SelectedWeek <- DATSxts[.indexweek(DATSxts)==1]       #get data for weeks 1
SelectedDay <- DATSxts[.indexday(DATSxts)==10]        #get data for days 10
SelectedMin <- DATSxts[.indexmin(DATSxts)==30]        #get data for every minute 30

****(Note: for some odd reason if I put all data/xts code in less than 8 lines loading the data gives an error message)****

Upvotes: 1

Views: 227

Answers (2)

Joshua Ulrich
Joshua Ulrich

Reputation: 176648

Granted, these aren't well-documented, but there are .index* functions that do what you want. A quick look at the source of a couple of the functions could have pointed you in the right direction. Take .indexyear for example, and note that ?POSIXlt says year is years since 1900.

R> .indexyear
function (x) 
{
    as.POSIXlt(.POSIXct(.index(x)))$year
}
<environment: namespace:xts>
SelectedYear <- DATSxts[(1900+.indexyear(DATSxts))==2016]  #data for year 2016

Week of the year is not simple, and looking at the source would not have likely helped... There are several different ways to count the weeks in a year. See the "%U", "%V", and "%W" formats in ?strftime.

SelectedWeek <- DATSxts[strftime(index(DATSxts),"%W")=="00"]  #get data for weeks 1

.indexday returns the day/date of the underlying index. Use .indexmday if you want the day of the month.

SelectedDay <- DATSxts[.indexmday(DATSxts)==10]  #get data for days 10

I'm not sure what you expect .indexmin to do, since you do not have any data that occur on the 30th minute of any hour. The function below seems to be working as expected.

SelectedMin <- DATSxts[.indexmin(DATSxts)==30]  #get data for every minute 30

Upvotes: 1

IRTFM
IRTFM

Reputation: 263342

You need to look at what .indexyear actually returns:

> .indexyear(DATSxts[1,])
[1] 115
> length( DATSxts[.indexyear(DATSxts)==116] )
[1] 943
> head( DATSxts[.indexyear(DATSxts)==116] )
                      [,1]
2016-01-03 15:00:00 1063.1
2016-01-03 16:00:00 1064.6
2016-01-03 17:00:00 1063.5
2016-01-03 18:00:00 1064.3
2016-01-03 19:00:00 1063.7
2016-01-03 20:00:00 1065.3

The 1900 basis is probably a holdover from an effort to emulate Excel on Windows. The weeks and days values are also different than you apparently expected:

> table( .indexweek(DATSxts) )

2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 
  58  115  115  111  115  116  116  111  115   28 

> table( .indexday(DATSxts) )

16798 16799 16800 16803 16804 16805 16806 16807 16808 16810 16811 16812 16813 16814 
   12    23    22     1    23    23    23    23    22     1    23    23    23    23 
16815 16817 16818 16819 16820 16821 16822 16824 16825 16826 16827 16828 16829 16831 
   22     1    19    23    23    23    22     1    23    23    23    23    22     1 
16832 16833 16834 16835 16836 16838 16839 16840 16841 16842 16843 16845 16846 16847 
   23    23    23    23    23     1    23    23    24    23    22     1    19    23 
16848 16849 16850 16852 16853 16854 16855 16856 16857 16859 16860 16861 
   23    23    22     1    23    23    23    23    22     1    23     5 

Upvotes: 2

Related Questions