R - subtract df elements based on column and colnames - r

I have two dfs - benchmark return and allreturns (samples below), and I'd like to create a third df that returns a subtracted value allreturns-benchmarkreturns, matched by a "startmonth" value for each row of allreturns, as well as the colname (V1:V12) that is consistent in both dfs.
This problem is akin to an index match with two criteria in Excel, where I'd match by startmonth on the row side and "V month" in the column side (colnames here).
So for instance, if in allreturns I had a January 2005 startmonth and the value for that row in V3 was 5, and the benchmarkreturn for row January 2005 and column V3 was 3, the value for that row of allreturns in the new df would be 3. And so on.
The actual allreturns dataframe is quite large, so I am concerned about merging before subtracting, and was wondering if that is the most viable method or if I can subtract the elements in one step.
Thank you for your help!
All returns:
structure(list(startmonth = structure(c(2012, 2012.08333333333,
2012.16666666667, 2012.25, 2012.33333333333, 2012.41666666667,
2012.5, 2012.58333333333, 2012.66666666667, 2012.75), class = "yearmon"),
V1 = c(0.0284247303, 0.0010087300099999, 0.0022169700300001,
-0.01468004942, 0.00102045998000011, 0.00591265976000011,
0.0199267303899999, 0.0417483615900001, 0.00335606009, 0.00334482998000007
), V2 = c(0.0294621331884797, 0.00322793636420049, -0.012495624619603,
-0.0136745698429375, 0.0069391533726606, 0.0259572101269254,
0.0625070003256281, 0.0452445316901551, 0.00671211552040374,
0.0314807661806089), V3 = c(0.0317444198847785, -0.0114994993211506,
-0.0114879159244523, -0.00784276316178301, 0.0270041584010525,
0.0687892427111718, 0.0660728376647666, 0.0487406969361834,
0.0349424776135903, 0.0430893809414579), V4 = c(0.0165983608120608,
-0.0104907741199978, -0.00564318030266497, 0.0119276866011795,
0.0698798993604133, 0.0723761636332563, 0.0696386700530516,
0.0781496303938529, 0.0465900515831383, 0.0514300069951692
), V5 = c(0.0176357587550031, -0.00464014273798818, 0.0141710999549016,
0.0541740095643377, 0.0734704805917499, 0.0759630795752142,
0.0996336273987892, 0.090283471493128, 0.054958669279074,
0.0744177901533509), V6 = c(0.0236526927561311, 0.0151941247787009,
0.056511081749947, 0.057711880885752, 0.0770610568378784,
0.106135386898253, 0.112009256278245, 0.099001465266642,
0.0780236008188706, 0.0925232124743112), V7 = c(0.0440507439776798,
0.0575768161840056, 0.0600567964260508, 0.0612497472951408,
0.107264153793045, 0.118584188660589, 0.120900971163225,
0.123029319908411, 0.0961897860250946, 0.0862168299189658
), V8 = c(0.0876381519555687, 0.06112610752891, 0.0636025061792393,
0.0910094612943682, 0.119725659059378, 0.127528477230367,
0.145407622386291, 0.141953913583335, 0.0898622388709431,
0.0799104502736558), V9 = c(0.0912883309497081, 0.0646753939459335,
0.0934281965725043, 0.103288031047544, 0.128679074917691,
0.152180027943971, 0.164709321337116, 0.135362201702614,
0.0835346946365931, 0.076045245502748), V10 = c(0.0949385048758928,
0.0945311704081673, 0.105733987546834, 0.11211001031858,
0.153355781552244, 0.171595851273483, 0.157986258190939,
0.128770492863592, 0.0796565180062141, 0.069524282329458),
V11 = c(0.125642923731963, 0.10684937460315, 0.114575524881519,
0.136424461680214, 0.172791417952441, 0.164833036942378,
0.151263198147072, 0.124730408712938, 0.0731136700886696,
0.0680519003357034), V12 = c(0.138311269558968, 0.115699830661878,
0.138943880653118, 0.155574781977547, 0.166021702439959,
0.158070225731925, 0.147142608328795, 0.117914407614703,
0.0716363466924688, 0.086773098425986)), .Names = c("startmonth",
"V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10",
"V11", "V12"), class = "data.frame", row.names = c(NA, 10L))
Benchmark returns:
structure(list(startmonth = structure(c(2005, 2005.08333333333,
2005.16666666667, 2005.25, 2005.33333333333, 2005.41666666667,
2005.5, 2005.58333333333, 2005.66666666667, 2005.75, 2005.83333333333,
2005.91666666667, 2006, 2006.08333333333, 2006.16666666667, 2006.25,
2006.33333333333, 2006.41666666667, 2006.5, 2006.58333333333,
2006.66666666667, 2006.75, 2006.83333333333, 2006.91666666667,
2007, 2007.08333333333, 2007.16666666667, 2007.25, 2007.33333333333,
2007.41666666667, 2007.5, 2007.58333333333, 2007.66666666667,
2007.75, 2007.83333333333, 2007.91666666667, 2008, 2008.08333333333,
2008.16666666667, 2008.25, 2008.33333333333, 2008.41666666667,
2008.5, 2008.58333333333, 2008.66666666667, 2008.75, 2008.83333333333,
2008.91666666667, 2009, 2009.08333333333, 2009.16666666667, 2009.25,
2009.33333333333, 2009.41666666667, 2009.5, 2009.58333333333,
2009.66666666667, 2009.75, 2009.83333333333, 2009.91666666667,
2010, 2010.08333333333, 2010.16666666667, 2010.25, 2010.33333333333,
2010.41666666667, 2010.5, 2010.58333333333, 2010.66666666667,
2010.75, 2010.83333333333, 2010.91666666667, 2011, 2011.08333333333,
2011.16666666667, 2011.25, 2011.33333333333, 2011.41666666667,
2011.5, 2011.58333333333, 2011.66666666667, 2011.75, 2011.83333333333,
2011.91666666667, 2012, 2012.08333333333, 2012.16666666667, 2012.25,
2012.33333333333, 2012.41666666667, 2012.5, 2012.58333333333,
2012.66666666667, 2012.75, 2012.83333333333, 2012.91666666667,
2013, 2013.08333333333, 2013.16666666667, 2013.25, 2013.33333333333,
2013.41666666667, 2013.5, 2013.58333333333, 2013.66666666667,
2013.75, 2013.83333333333, 2013.91666666667, 2014, 2014.08333333333,
2014.16666666667, 2014.25, 2014.33333333333, 2014.41666666667,
2014.5, 2014.58333333333, 2014.66666666667, 2014.75, 2014.83333333333
), class = "yearmon"), V1 = c(0.83740290789865, 0.175849379040301,
0.115745179820806, 0.588703967630863, 0.663440374424681, 0.122854199260473,
0.329111734172329, 0.386456901207566, 0.328031520592049, 0.91476626554504,
0.838957223575562, 0.482753940392286, 0.203118846053258, 0.832898694789037,
0.569874424720183, 0.85476457583718, 0.873173910891637, 0.599100088467821,
0.990275882417336, 0.360726963961497, 0.366468257969245, 0.213221889222041,
0.736634878907353, 0.770827260101214, 0.78438491653651, 0.443004491971806,
0.0431681957561523, 0.362984816078097, 0.933778994018212, 0.890268561197445,
0.0288882539607584, 0.704298130935058, 0.0533608365803957, 0.965311451349407,
0.797033908311278, 0.702818523859605, 0.615030416054651, 0.783928481163457,
0.0931849081534892, 0.693762929877266, 0.709550492931157, 0.527024435112253,
0.463098735082895, 0.975416449131444, 0.418448116164654, 0.716492718551308,
0.900618054671213, 0.937315494287759, 0.371745373355225, 0.876447483897209,
0.638362225377932, 0.230460224673152, 0.84510417538695, 0.709547182312235,
0.135460949270055, 0.86736886552535, 0.0408105747774243, 0.59634031355381,
0.378888477338478, 0.400274467887357, 0.73448179056868, 0.0201477019581944,
0.786700234282762, 0.145882265642285, 0.722347423899919, 0.495645943330601,
0.8204965388868, 0.944398637628183, 0.920839294558391, 0.244003586936742,
0.770501866471022, 0.608119423966855, 0.599260926479474, 0.316019677789882,
0.594015010166913, 0.862004647962749, 0.248981321696192, 0.251351157668978,
0.86709617683664, 0.477806584443897, 0.308428246993572, 0.930116105359048,
0.820579398889095, 0.407806032337248, 0.00576947652734816, 0.205415812321007,
0.503058285918087, 0.421452875249088, 0.171408428112045, 0.927357386332005,
0.736080843256786, 0.0634949475061148, 0.628076422959566, 0.825837341602892,
0.860992948058993, 0.637153514893726, 0.496678973315284, 0.532001776387915,
0.742080342955887, 0.279888727236539, 0.702802252722904, 0.20291490922682,
0.761654698755592, 0.413414426380768, 0.0555722850840539, 0.125917585333809,
0.545490033458918, 0.19256273820065, 0.242046363884583, 0.0174444266594946,
0.380734569625929, 0.735217718640342, 0.346396475099027, 0.997578310314566,
0.544119880301878, 0.610004446236417, 0.643721028929576, 0.0819557739887387,
0.666767429094762), V2 = c(0.325795059790835, 0.0280552506446838,
0.563847373938188, 0.968374367104843, 0.0397681496106088, 0.409115779446438,
0.568448322825134, 0.498488057404757, 0.725102471420541, 0.745882571907714,
0.956671225838363, 0.732607456855476, 0.723114193649963, 0.715586860431358,
0.740428047254682, 0.447626604698598, 0.136078626848757, 0.343137193005532,
0.994878244586289, 0.569102957844734, 0.264083825284615, 0.0172849236987531,
0.902412769151852, 0.237230452476069, 0.342157111503184, 0.940635218983516,
0.355561858741567, 0.0913543966598809, 0.730372012127191, 0.424820594489574,
0.862009244738147, 0.673514985479414, 0.0558020728640258, 0.769041493767872,
0.429111589910462, 0.814037810778245, 0.485316230216995, 0.407698386581615,
0.0245183343067765, 0.38149883877486, 0.939248957671225, 0.206260631326586,
0.773903579683974, 0.955869939411059, 0.930366629268974, 0.497170565184206,
0.232823575846851, 0.119068749016151, 0.188956700265408, 0.769491335609928,
0.487886165967211, 0.249603169038892, 0.61157290590927, 0.967520525446162,
0.669223197968677, 0.0509379308205098, 0.630447809584439, 0.93567297863774,
0.296445045853034, 0.231306056724861, 0.664361900882795, 0.570044811815023,
0.988849612651393, 0.96225642086938, 0.567738547222689, 0.562166472664103,
0.382391084451228, 0.87047664122656, 0.553916300181299, 0.348980325739831,
0.861658359179273, 0.824328954331577, 0.602889677742496, 0.108061951119453,
0.510826006531715, 0.787226195679978, 0.209260584786534, 0.467020642245188,
0.916463258676231, 0.810527657391503, 0.259206298971549, 0.383183149620891,
0.399636579910293, 0.517378240358084, 0.277383098145947, 0.0540329110808671,
0.334274391178042, 0.762725146021694, 0.624413190409541, 0.664978531654924,
0.877504705451429, 0.133810454979539, 0.303127287654206, 0.401904000202194,
0.333468962227926, 0.880315525457263, 0.693340804660693, 0.104616170749068,
0.726646555587649, 0.787352371029556, 0.805738570401445, 0.842183391330764,
0.203002500347793, 0.4008568006102, 0.764609909849241, 0.68458701716736,
0.826388106215745, 0.458343503531069, 0.681572974193841, 0.816049969289452,
0.99422895279713, 0.586194961098954, 0.106009811395779, 0.564460329711437,
0.763485615840182, 0.641962635098025, 0.556110577424988, 0.624630474718288,
0.0865604616701603), V3 = c(0.103272917214781, 0.145110326353461,
0.139097063103691, 0.704820184968412, 0.499401112552732, 0.183894460555166,
0.142426387872547, 0.654179619625211, 0.231400778284296, 0.07779724849388,
0.639861593721434, 0.329660887364298, 0.207785484148189, 0.349164958344772,
0.918049042113125, 0.561889169970527, 0.801518984371796, 0.374053293140605,
0.279966498259455, 0.211821462959051, 0.988277795957401, 0.208132355008274,
0.358667319407687, 0.489217024296522, 0.235113901318982, 0.0665953925345093,
0.682081600185484, 0.923956993501633, 0.951206840341911, 0.153028624830768,
0.457171397982165, 0.497927478514612, 0.464426794555038, 0.700641778064892,
0.350401868345216, 0.239867100957781, 0.495585883036256, 0.659481293288991,
0.121178397443146, 0.966434765374288, 0.781230272026733, 0.191542416810989,
0.144150232896209, 0.649679786758497, 0.990280882688239, 0.182715938193724,
0.990455492865294, 0.0560185567010194, 0.258451806847006, 0.745103346649557,
0.389899141388014, 0.0482800761237741, 0.0293514989316463, 0.393075271975249,
0.53582366858609, 0.522070667007938, 0.802251521963626, 0.292497304268181,
0.523711683694273, 0.606568831950426, 0.198962268885225, 0.391696797916666,
0.059201990487054, 0.0645288161467761, 0.808837213553488, 0.0917617143131793,
0.698996715713292, 0.330968038411811, 0.843029006384313, 0.9479373190552,
0.514315341366455, 0.325804576743394, 0.3519700136967, 0.537765094777569,
0.871437344467267, 0.667971923248842, 0.0599088978487998, 0.560071154031903,
0.902071484364569, 0.193852530093864, 0.629741749959067, 0.0646166354417801,
0.238403898198158, 0.737684201914817, 0.0984734564553946, 0.314926577033475,
0.667433348251507, 0.200710391392931, 0.510746134677902, 0.0941306182648987,
0.408070910954848, 0.349233734188601, 0.0384121341630816, 0.469132867408916,
0.968564240029082, 0.250593126285821, 0.0388190811499953, 0.301829227479175,
0.62604419933632, 0.981997888535261, 0.699610964162275, 0.319871086394414,
0.0948076455388218, 0.0173410377465189, 0.733540053712204, 0.304378144675866,
0.593588132876903, 0.85377266141586, 0.272565411170945, 0.151829193811864,
0.951305341674015, 0.21787527250126, 0.728856807108968, 0.932554795406759,
0.530237913830206, 0.146221859846264, 0.220554714323953, 0.133143884129822,
0.845931079704314), V4 = c(0.828090658877045, 0.244675925467163,
0.617208154639229, 0.675795117858797, 0.921914914855734, 0.777150499867275,
0.841221024747938, 0.492060288786888, 0.326705329120159, 0.686669388087466,
0.121503615053371, 0.367948720930144, 0.106932319700718, 0.363004767335951,
0.350841429783031, 0.204824617831036, 0.233977065887302, 0.7365177560132,
0.0454314323142171, 0.865328938700259, 0.328468273859471, 0.187198075000197,
0.0820242392364889, 0.516697632148862, 0.481362534221262, 0.620970563730225,
0.817455855663866, 0.98652182915248, 0.980461026541889, 0.0709870441351086,
0.536431754939258, 0.976550402119756, 0.40865416219458, 0.890750366030261,
0.662430537166074, 0.388162778690457, 0.959246737649664, 0.931342099327594,
0.770709854317829, 0.279344801558182, 0.305849055293947, 0.370182184502482,
0.562077804701403, 0.240711282240227, 0.630282753147185, 0.170173112303019,
0.313635889207944, 0.694733579177409, 0.319224840262905, 0.34643409284763,
0.695703814737499, 0.39429243397899, 0.189475272549316, 0.516221221769229,
0.777665859088302, 0.558720602886751, 0.229818729683757, 0.484828221844509,
0.481090292567387, 0.0106754100415856, 0.684031964046881, 0.815850237151608,
0.484693359583616, 0.362359361024573, 0.116397734964266, 0.350968763930723,
0.150200203061104, 0.91834011208266, 0.52454294054769, 0.148975162534043,
0.510950653115287, 0.803136837668717, 0.918003916973248, 0.100692971842363,
0.0494276131503284, 0.526557822246104, 0.0758368712849915, 0.269769576610997,
0.101892914390191, 0.424805645598099, 0.110284168738872, 0.709118485450745,
0.784806538838893, 0.0519805552903563, 0.398219058057293, 0.34969964553602,
0.695627773646265, 0.549499853979796, 0.273849666584283, 0.151610748609528,
0.874867760110646, 0.198461376130581, 0.569974714424461, 0.725238451501355,
0.594061649870127, 0.287728592054918, 0.167462798533961, 0.126261120429263,
0.557413443457335, 0.464988547144458, 0.552185143576935, 0.0831529255956411,
0.333197562023997, 0.574352362193167, 0.4974719658494, 0.190328923054039,
0.82773098279722, 0.356611662777141, 0.803166383411735, 0.320444498909637,
0.366018867818639, 0.809054122306406, 0.553693874971941, 0.293376821791753,
0.97094266442582, 0.879881211323664, 0.308937143767253, 0.579827836947516,
0.496185180498287), V5 = c(0.923095905454829, 0.762332424288616,
0.972339176572859, 0.422411200823262, 0.940925979521126, 0.540574067272246,
0.985336700454354, 0.8358859855216, 0.452674505067989, 0.919172912370414,
0.177077513653785, 0.484894159948453, 0.228302743285894, 0.588133673649281,
0.120827580103651, 0.320581880165264, 0.132633685600013, 0.244240331463516,
0.0881568337790668, 0.0182364885695279, 0.619532398879528, 0.763255036668852,
0.360307943308726, 0.291531737195328, 0.637159892357886, 0.423571469495073,
0.144552590325475, 0.232900225091726, 0.431558183860034, 0.624517237534747,
0.660041945520788, 0.0605304769705981, 0.894808513810858, 0.766446133842692,
0.708146936027333, 0.980518834665418, 0.831859190016985, 0.63390138419345,
0.0906804192345589, 0.863758355844766, 0.81235816469416, 0.754735963186249,
0.96086330129765, 0.379362520063296, 0.86472422350198, 0.434156836709008,
0.921033434569836, 0.930532508296892, 0.919923858484253, 0.0185073432512581,
0.190673178527504, 0.389489938504994, 0.251335079083219, 0.398261951981112,
0.323421294335276, 0.0406879091169685, 0.0622786141466349, 0.511600321624428,
0.761615104507655, 0.267784109571949, 0.593046235619113, 0.923055295599625,
0.481184788281098, 0.539936339249834, 0.71660251962021, 0.525291009107605,
0.878714059945196, 0.127967000938952, 0.616779315285385, 0.174148306716233,
0.737354298587888, 0.931568023283035, 0.51300498447381, 0.860497320536524,
0.776048321742564, 0.0440801647491753, 0.0390385524369776, 0.765144015429541,
0.334515287308022, 0.312304191756994, 0.874887532787398, 0.495062640635297,
0.772741753375158, 0.847717159660533, 0.465354095678777, 0.988151066936553,
0.933549190638587, 0.493211443535984, 0.604139760835096, 0.11511691333726,
0.955369158880785, 0.00772590702399611, 0.396924669155851, 0.360593063989654,
0.44654577318579, 0.775453717214987, 0.103126123780385, 0.0639552725479007,
0.382581982528791, 0.221267726272345, 0.569829758256674, 0.189883300568908,
0.343288340838626, 0.372111623873934, 0.235726190032437, 0.540518779074773,
0.706296919612214, 0.939652142813429, 0.0611454977188259, 0.428340997546911,
0.625002122484148, 0.828457030234858, 0.038334577344358, 0.986632968764752,
0.838095314567909, 0.635932896286249, 0.375527458963916, 0.63829215336591,
0.54482639557682), V6 = c(0.370218734024093, 0.419946043053642,
0.545280206250027, 0.277894535567611, 0.00736652850173414, 0.790516802109778,
0.313465759390965, 0.809552269754931, 0.842788263224065, 0.499014808563516,
0.623795239022002, 0.548406617250293, 0.05658471561037, 0.965675859013572,
0.472378655336797, 0.524981135968119, 0.46187190245837, 0.387807991588488,
0.938760220305994, 0.50433289213106, 0.233549498720095, 0.00805983133614063,
0.127320852363482, 0.672702707583085, 0.415367575129494, 0.250937715405598,
0.0297437864355743, 0.406187633285299, 0.675050344318151, 0.292069549206644,
0.61900219717063, 0.616054858779535, 0.250656338408589, 0.518912112340331,
0.183584470301867, 0.0910171526484191, 0.546375629259273, 0.621274302946404,
0.0846014332491904, 0.621578456601128, 0.576011084485799, 0.504746064078063,
0.221732606412843, 0.69746913574636, 0.164020692463964, 0.158075062790886,
0.847464352613315, 0.605109791038558, 0.596407983452082, 0.0114535829052329,
0.063888885313645, 0.353724629618227, 0.612920761108398, 0.957113454118371,
0.741108618909493, 0.0766351209022105, 0.641778253018856, 0.988549496047199,
0.554500735830516, 0.490220351610333, 0.814872513525188, 0.473424580646679,
0.210200395900756, 0.70044357329607, 0.208990931045264, 0.680869682924822,
0.932328240247443, 0.718588735442609, 0.658000691328198, 0.185951039893553,
0.753725055372342, 0.123452978907153, 0.212786188349128, 0.228114538360387,
0.00365104852244258, 0.814572377130389, 0.124759285245091, 0.11509175482206,
0.3558327362407, 0.487612128257751, 0.344766818918288, 0.25334310438484,
0.712012055562809, 0.453843667870387, 0.938410549657419, 0.718660024693236,
0.325095203239471, 0.156200079247355, 0.175217281095684, 0.551201074384153,
0.830405764747411, 0.281535709509626, 0.403883191291243, 0.545596988638863,
0.282760329078883, 0.755758260609582, 0.310922948876396, 0.819937098305672,
0.0865648619364947, 0.936576428823173, 0.880421161418781, 0.454629295505583,
0.14402421656996, 0.840076276101172, 0.655401981435716, 0.569122347747907,
0.438042796216905, 0.0409106772858649, 0.944646059069782, 0.825017294846475,
0.0786613943055272, 0.0726817650720477, 0.588666438357905, 0.588839216856286,
0.444881158880889, 0.721039973665029, 0.534136849688366, 0.852300873491913,
0.281278134556487), V7 = c(0.141886763740331, 0.532325896434486,
0.588067500153556, 0.640411838656291, 0.946903814794496, 0.0120973803568631,
0.952789547387511, 0.107461815234274, 0.236731971381232, 0.79851196357049,
0.312241489300504, 0.458273995434865, 0.275089886970818, 0.966928232926875,
0.153488726355135, 0.250537436921149, 0.262579692527652, 0.622048743302003,
0.497019267408177, 0.081929708365351, 0.995605900418013, 0.385106679983437,
0.176982956239954, 0.696908152429387, 0.732609438244253, 0.187662536511198,
0.936794911511242, 0.168030542787164, 0.569703943328932, 0.604032611008734,
0.115415274398401, 0.807121691294014, 0.139127567177638, 0.530566544737667,
0.27088794275187, 0.702946513192728, 0.755043589742854, 0.811690449714661,
0.114138029748574, 0.233510547783226, 0.654041203204542, 0.441771358018741,
0.969852354610339, 0.964909290196374, 0.541466456372291, 0.0193931085523218,
0.201263489434496, 0.33053227327764, 0.687692743493244, 0.18348641670309,
0.418929822510108, 0.383909322321415, 0.137825107900426, 0.217210126807913,
0.453193115768954, 0.0340990910772234, 0.61968918191269, 0.780179363442585,
0.80423841974698, 0.491154700284824, 0.351554640568793, 0.587180010043085,
0.239602132700384, 0.644263347610831, 0.969654035987332, 0.547199918190017,
0.397612665314227, 0.722763631725684, 0.72788112401031, 0.795322017977014,
0.686627405928448, 0.762283272109926, 0.926369941327721, 0.894270734628662,
0.257203514222056, 0.0720642674714327, 0.985171274049208, 0.660412719240412,
0.648350277217105, 0.917640027124435, 0.694397774524987, 0.917451658984646,
0.524428785312921, 0.0992080934811383, 0.100496702594683, 0.975102295167744,
0.492288384120911, 0.0227630862500519, 0.813120178645477, 0.71656278334558,
0.913717438234016, 0.636773794423789, 0.929305002791807, 0.892870238050818,
0.780625773593783, 0.850657029775903, 0.204829484922811, 0.640264326473698,
0.663275289349258, 0.789437669329345, 0.485021750209853, 0.0749609414488077,
0.655508011812344, 0.74328700476326, 0.31854355102405, 0.0728525989688933,
0.382023223210126, 0.215404908405617, 0.656497932272032, 0.92760404618457,
0.781216817675158, 0.833905356237665, 0.361858946271241, 0.300276404013857,
0.373293370939791, 0.853469310561195, 0.133900812361389, 0.986530470428988,
0.411433706991374), V8 = c(0.849042785586789, 0.539557616924867,
0.564340554876253, 0.755421123001724, 0.984947801567614, 0.112984226550907,
0.403615512186661, 0.0420174030587077, 0.611705861985683, 0.659787082113326,
0.876130681717768, 0.948364004027098, 0.203974137781188, 0.309610587079078,
0.984034060733393, 0.534302587388083, 0.971052328590304, 0.812090678606182,
0.12758506112732, 0.4455144808162, 0.415974463103339, 0.518267513252795,
0.0146076162345707, 0.24067388032563, 0.0458081206306815, 0.366667992202565,
0.0854597128927708, 0.0380873722024262, 0.663318049395457, 0.873134173220024,
0.715449047507718, 0.83272837777622, 0.269359193509445, 0.936196858529001,
0.00233305594883859, 0.890992778819054, 0.980090971337631, 0.483554403064772,
0.535345176234841, 0.51444540428929, 0.802370720077306, 0.772680272115394,
0.102163553936407, 0.00971000618301332, 0.902426401618868, 0.880951991537586,
0.284606313100085, 0.0439106288831681, 0.937477112514898, 0.912608936196193,
0.998304838780314, 0.745025476673618, 0.751772192539647, 0.278778153471649,
0.162150844698772, 0.837488571181893, 0.388283311389387, 0.335549421375617,
0.650775108020753, 0.990717119770125, 0.0961552720982581, 0.302420736989006,
0.276211500866339, 0.733167931437492, 0.164790137903765, 0.249713592929766,
0.836560999508947, 0.692190924426541, 0.570303184678778, 0.984758368227631,
0.328286996111274, 0.917134278221056, 0.612915431382135, 0.242480249842629,
0.52148644789122, 0.660938010783866, 0.448608730221167, 0.691765318857506,
0.341251585632563, 0.304311832180247, 0.0227175708860159, 0.149960547685623,
0.17484575859271, 0.518439768115059, 0.639089794829488, 0.860548116965219,
0.705641856649891, 0.673352806130424, 0.100467606680468, 0.570258957333863,
0.706326137995347, 0.70817924500443, 0.453099914360791, 0.70276203728281,
0.252404118655249, 0.598284177947789, 0.741140816593543, 0.806638022884727,
0.720691111404449, 0.68312481045723, 0.973559587728232, 0.917065860237926,
0.874346838332713, 0.143847105791792, 0.453837911831215, 0.569988392293453,
0.692350469296798, 0.806599335046485, 0.456668916391209, 0.386287276633084,
0.908880397910252, 0.793711767531931, 0.754064193693921, 0.533496839459985,
0.615311589790508, 0.331158632645383, 0.11861584126018, 0.408010076498613,
0.222291024168953), V9 = c(0.125224817777053, 0.914628089172766,
0.377369399182498, 0.751476752338931, 0.0853602013085037, 0.846715328749269,
0.561333758756518, 0.0429282214026898, 0.284403225872666, 0.285037216497585,
0.604412623681128, 0.319880489027128, 0.91246378608048, 0.455940068000928,
0.867939765099436, 0.425015810178593, 0.470896222162992, 0.224302776856348,
0.36109847901389, 0.594786533853039, 0.642710687592626, 0.207564177224413,
0.284501956542954, 0.732748613227159, 0.774612213019282, 0.11284792679362,
0.956883376231417, 0.821359134977683, 0.846147159812972, 0.411850540433079,
0.770936917047948, 0.496579384431243, 0.619339638156816, 0.177370799472556,
0.239690004847944, 0.224350028671324, 0.0038589695468545, 0.179824534803629,
0.719484498025849, 0.798693082761019, 0.842053556581959, 0.294362392276525,
0.221064954530448, 0.898532920051366, 0.723221117863432, 0.863187251612544,
0.0994312795810401, 0.361342326970771, 0.783163121435791, 0.502090321853757,
0.13186371861957, 0.659234342398122, 0.390342378057539, 0.328385862288997,
0.446345729287714, 0.507106031524017, 0.152638432569802, 0.445379047188908,
0.304815052077174, 0.0760775990784168, 0.924889291869476, 0.0672700984869152,
0.199834800790995, 0.0825146653223783, 0.0361869866028428, 0.543002652470022,
0.972956772428006, 0.190790882101282, 0.376062947325408, 0.351586842909455,
0.689982761396095, 0.241374674253166, 0.838397472165525, 0.934896029997617,
0.468875553458929, 0.628862793324515, 0.431529769208282, 0.935133612947538,
0.352268195245415, 0.830344556132331, 0.666667894460261, 0.905744371935725,
0.7915226041805, 0.895240528741851, 0.584306629607454, 0.864065658533946,
0.0452204917091876, 0.55241105845198, 0.101811402710155, 0.749172917334363,
0.27551553142257, 0.83245463995263, 0.274472723482177, 0.257237296551466,
0.959463604493067, 0.109898063354194, 0.971408878453076, 0.0386630205903202,
0.975892154034227, 0.358955943025649, 0.399594583781436, 0.98816026840359,
0.964564403751865, 0.385808907682076, 0.42681196751073, 0.346436691703275,
0.723757655825466, 0.839694014983252, 0.847320703556761, 0.127265167189762,
0.192201391793787, 0.113802545471117, 0.373555006459355, 0.84189727040939,
0.515876488294452, 0.0691332728601992, 0.834895022911951, 0.19373700208962,
0.921317730797455)), .Names = c("startmonth", "V1", "V2", "V3",
"V4", "V5", "V6", "V7", "V8", "V9"), class = "data.frame", row.names = c(NA,
119L))

Here's what I get with a merge that is then restricted to only the results of the first two columns of each df:
> merge(Benret, Allret, by=1)[c(1,2,3,11,12)]
startmonth V1.x V2.x V1.y V2.y
1 Jan 2012 0.005769477 0.27738310 0.02842473 0.029462133
2 Feb 2012 0.205415812 0.05403291 0.00100873 0.003227936
3 Mar 2012 0.503058286 0.33427439 0.00221697 -0.012495625
4 Apr 2012 0.421452875 0.76272515 -0.01468005 -0.013674570
5 May 2012 0.171408428 0.62441319 0.00102046 0.006939153
6 Jun 2012 0.927357386 0.66497853 0.00591266 0.025957210
7 Jul 2012 0.736080843 0.87750471 0.01992673 0.062507000
8 Aug 2012 0.063494948 0.13381045 0.04174836 0.045244532
9 Sep 2012 0.628076423 0.30312729 0.00335606 0.006712116
10 Oct 2012 0.825837342 0.40190400 0.00334483 0.031480766
Calculate the differences and omit the inputs:
res <- merge(Benret, Allret, by=1)[c(1,2,3,11,12)]
> res$diffV1 <- with(res, V1.x - V1.y)
> res$diffV2 <- with(res, V2.x - V2.y)
> res[ -(2:5) ]
startmonth diffV1 diffV2
1 Jan 2012 -0.02265525 0.24792096
2 Feb 2012 0.20440708 0.05080497
3 Mar 2012 0.50084132 0.34677002
4 Apr 2012 0.43613292 0.77639972
5 May 2012 0.17038797 0.61747404
6 Jun 2012 0.92144473 0.63902132
7 Jul 2012 0.71615411 0.81499771
8 Aug 2012 0.02174659 0.08856592
9 Sep 2012 0.62472036 0.29641517
10 Oct 2012 0.82249251 0.37042323

Related

Reshaping Data Frame (Spread and Make XTS)

I am trying to reshape a data frame from a long vector to a table with the date being first column (ideally its the index as I want to make this an xts format ultimately) and the place_id's being new columns extending outward from left to right. The values contained for the place_id and corresponding date would be populating the table "index_nsa".
place_id index_nsa New_Date
1 DV_ENC 100.00 1991-1
2 DV_ENC 100.99 1991-2
3 DV_ENC 101.36 1991-3
4 DV_ENC 101.75 1991-4
5 DV_ENC 102.39 1991-5
6 DV_ENC 102.81 1991-6
dput:
structure(list(place_id = structure(c(414L, 414L, 414L, 414L,
414L, 414L), .Label = c("10180", "10420", "10500", "10540", "10580",
"10740", "10780", "10900", "11020", "11100", "11180", "11244",
"11260", "11460", "11500", "11540", "11700", "12020", "12060",
"12100", "12220", "12260", "12420", "12540", "12580", "12620",
"12700", "12940", "12980", "13020", "13140", "13220", "13380",
"13460", "13740", "13780", "13820", "13900", "13980", "14010",
"14020", "14100", "14260", "14454", "14500", "14540", "14740",
"14860", "15180", "15260", "15380", "15500", "15540", "15680",
"15764", "15804", "15940", "15980", "16020", "16060", "16180",
"16220", "16300", "16540", "16580", "16620", "16700", "16740",
"16820", "16860", "16940", "16984", "17020", "17140", "17300",
"17420", "17460", "17660", "17780", "17820", "17860", "17900",
"17980", "18020", "18140", "18580", "18700", "18880", "19060",
"19124", "19140", "19180", "19300", "19340", "19430", "19460",
"19500", "19660", "19740", "19780", "19804", "20020", "20100",
"20220", "20260", "20500", "20700", "20740", "20940", "20994",
"21060", "21140", "21300", "21340", "21420", "21500", "21660",
"21780", "21820", "22020", "22140", "22180", "22220", "22380",
"22420", "22500", "22520", "22540", "22660", "22744", "22900",
"23060", "23104", "23224", "23420", "23460", "23540", "23580",
"23844", "23900", "24020", "24140", "24220", "24260", "24300",
"24340", "24420", "24500", "24540", "24580", "24660", "24780",
"24860", "25060", "25180", "25220", "25260", "25420", "25500",
"25540", "25620", "25860", "25940", "25980", "26140", "26300",
"26380", "26420", "26580", "26620", "26820", "26900", "26980",
"27060", "27100", "27140", "27180", "27260", "27340", "27500",
"27620", "27740", "27780", "27860", "27900", "27980", "28020",
"28100", "28140", "28420", "28660", "28700", "28740", "28940",
"29020", "29100", "29180", "29200", "29340", "29404", "29420",
"29460", "29540", "29620", "29700", "29740", "29820", "29940",
"30020", "30140", "30300", "30340", "30460", "30620", "30700",
"30780", "30860", "30980", "31020", "31084", "31140", "31180",
"31340", "31420", "31460", "31540", "31700", "31740", "31860",
"31900", "32580", "32780", "32820", "32900", "33124", "33140",
"33220", "33260", "33340", "33460", "33540", "33660", "33700",
"33740", "33780", "33860", "33874", "34060", "34100", "34580",
"34620", "34740", "34820", "34900", "34940", "34980", "35004",
"35084", "35100", "35154", "35300", "35380", "35614", "35660",
"35840", "35980", "36084", "36100", "36140", "36220", "36260",
"36420", "36500", "36540", "36740", "36780", "36980", "37100",
"37340", "37460", "37620", "37860", "37900", "37964", "38060",
"38220", "38300", "38340", "38540", "38860", "38900", "38940",
"39100", "39150", "39300", "39340", "39380", "39460", "39540",
"39580", "39660", "39740", "39820", "39900", "40060", "40140",
"40220", "40340", "40380", "40420", "40484", "40580", "40660",
"40900", "40980", "41060", "41100", "41140", "41180", "41420",
"41500", "41540", "41620", "41660", "41700", "41740", "41884",
"41940", "42020", "42034", "42100", "42140", "42200", "42220",
"42340", "42540", "42644", "42680", "42700", "43100", "43300",
"43340", "43420", "43580", "43620", "43780", "43900", "44060",
"44100", "44140", "44180", "44220", "44300", "44420", "44700",
"44940", "45060", "45104", "45220", "45300", "45460", "45500",
"45540", "45780", "45820", "45940", "46060", "46140", "46220",
"46300", "46340", "46520", "46540", "46660", "46700", "47020",
"47220", "47260", "47300", "47380", "47460", "47580", "47664",
"47894", "47940", "48060", "48140", "48260", "48300", "48424",
"48540", "48620", "48660", "48700", "48864", "48900", "49020",
"49180", "49340", "49420", "49620", "49660", "49700", "49740",
"AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "DV_ENC",
"DV_ESC", "DV_MA", "DV_MT", "DV_NE", "DV_PAC", "DV_SA", "DV_WNC",
"DV_WSC", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY",
"LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND",
"NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "PR",
"RI", "SC", "SD", "TN", "TX", "USA", "UT", "VA", "VT", "WA",
"WI", "WV", "WY"), class = "factor"), index_nsa = c(100, 100.99,
101.36, 101.75, 102.39, 102.81), New_Date = structure(c(65L,
69L, 70L, 71L, 72L, 73L), .Label = c("1975-1", "1975-2", "1975-3",
"1975-4", "1976-1", "1976-2", "1976-3", "1976-4", "1977-1", "1977-2",
"1977-3", "1977-4", "1978-1", "1978-2", "1978-3", "1978-4", "1979-1",
"1979-2", "1979-3", "1979-4", "1980-1", "1980-2", "1980-3", "1980-4",
"1981-1", "1981-2", "1981-3", "1981-4", "1982-1", "1982-2", "1982-3",
"1982-4", "1983-1", "1983-2", "1983-3", "1983-4", "1984-1", "1984-2",
"1984-3", "1984-4", "1985-1", "1985-2", "1985-3", "1985-4", "1986-1",
"1986-2", "1986-3", "1986-4", "1987-1", "1987-2", "1987-3", "1987-4",
"1988-1", "1988-2", "1988-3", "1988-4", "1989-1", "1989-2", "1989-3",
"1989-4", "1990-1", "1990-2", "1990-3", "1990-4", "1991-1", "1991-10",
"1991-11", "1991-12", "1991-2", "1991-3", "1991-4", "1991-5",
"1991-6", "1991-7", "1991-8", "1991-9", "1992-1", "1992-10",
"1992-11", "1992-12", "1992-2", "1992-3", "1992-4", "1992-5",
"1992-6", "1992-7", "1992-8", "1992-9", "1993-1", "1993-10",
"1993-11", "1993-12", "1993-2", "1993-3", "1993-4", "1993-5",
"1993-6", "1993-7", "1993-8", "1993-9", "1994-1", "1994-10",
"1994-11", "1994-12", "1994-2", "1994-3", "1994-4", "1994-5",
"1994-6", "1994-7", "1994-8", "1994-9", "1995-1", "1995-10",
"1995-11", "1995-12", "1995-2", "1995-3", "1995-4", "1995-5",
"1995-6", "1995-7", "1995-8", "1995-9", "1996-1", "1996-10",
"1996-11", "1996-12", "1996-2", "1996-3", "1996-4", "1996-5",
"1996-6", "1996-7", "1996-8", "1996-9", "1997-1", "1997-10",
"1997-11", "1997-12", "1997-2", "1997-3", "1997-4", "1997-5",
"1997-6", "1997-7", "1997-8", "1997-9", "1998-1", "1998-10",
"1998-11", "1998-12", "1998-2", "1998-3", "1998-4", "1998-5",
"1998-6", "1998-7", "1998-8", "1998-9", "1999-1", "1999-10",
"1999-11", "1999-12", "1999-2", "1999-3", "1999-4", "1999-5",
"1999-6", "1999-7", "1999-8", "1999-9", "2000-1", "2000-10",
"2000-11", "2000-12", "2000-2", "2000-3", "2000-4", "2000-5",
"2000-6", "2000-7", "2000-8", "2000-9", "2001-1", "2001-10",
"2001-11", "2001-12", "2001-2", "2001-3", "2001-4", "2001-5",
"2001-6", "2001-7", "2001-8", "2001-9", "2002-1", "2002-10",
"2002-11", "2002-12", "2002-2", "2002-3", "2002-4", "2002-5",
"2002-6", "2002-7", "2002-8", "2002-9", "2003-1", "2003-10",
"2003-11", "2003-12", "2003-2", "2003-3", "2003-4", "2003-5",
"2003-6", "2003-7", "2003-8", "2003-9", "2004-1", "2004-10",
"2004-11", "2004-12", "2004-2", "2004-3", "2004-4", "2004-5",
"2004-6", "2004-7", "2004-8", "2004-9", "2005-1", "2005-10",
"2005-11", "2005-12", "2005-2", "2005-3", "2005-4", "2005-5",
"2005-6", "2005-7", "2005-8", "2005-9", "2006-1", "2006-10",
"2006-11", "2006-12", "2006-2", "2006-3", "2006-4", "2006-5",
"2006-6", "2006-7", "2006-8", "2006-9", "2007-1", "2007-10",
"2007-11", "2007-12", "2007-2", "2007-3", "2007-4", "2007-5",
"2007-6", "2007-7", "2007-8", "2007-9", "2008-1", "2008-10",
"2008-11", "2008-12", "2008-2", "2008-3", "2008-4", "2008-5",
"2008-6", "2008-7", "2008-8", "2008-9", "2009-1", "2009-10",
"2009-11", "2009-12", "2009-2", "2009-3", "2009-4", "2009-5",
"2009-6", "2009-7", "2009-8", "2009-9", "2010-1", "2010-10",
"2010-11", "2010-12", "2010-2", "2010-3", "2010-4", "2010-5",
"2010-6", "2010-7", "2010-8", "2010-9", "2011-1", "2011-10",
"2011-11", "2011-12", "2011-2", "2011-3", "2011-4", "2011-5",
"2011-6", "2011-7", "2011-8", "2011-9", "2012-1", "2012-10",
"2012-11", "2012-12", "2012-2", "2012-3", "2012-4", "2012-5",
"2012-6", "2012-7", "2012-8", "2012-9", "2013-1", "2013-10",
"2013-11", "2013-12", "2013-2", "2013-3", "2013-4", "2013-5",
"2013-6", "2013-7", "2013-8", "2013-9", "2014-1", "2014-10",
"2014-11", "2014-12", "2014-2", "2014-3", "2014-4", "2014-5",
"2014-6", "2014-7", "2014-8", "2014-9", "2015-1", "2015-10",
"2015-11", "2015-12", "2015-2", "2015-3", "2015-4", "2015-5",
"2015-6", "2015-7", "2015-8", "2015-9", "2016-1", "2016-10",
"2016-11", "2016-12", "2016-2", "2016-3", "2016-4", "2016-5",
"2016-6", "2016-7", "2016-8", "2016-9", "2017-1", "2017-10",
"2017-11", "2017-12", "2017-2", "2017-3", "2017-4", "2017-5",
"2017-6", "2017-7", "2017-8", "2017-9", "2018-1", "2018-10",
"2018-11", "2018-12", "2018-2", "2018-3", "2018-4", "2018-5",
"2018-6", "2018-7", "2018-8", "2018-9", "2019-1", "2019-2", "2019-3",
"2019-4", "2019-5", "2019-6", "2019-7", "2019-8"), class = "factor")), row.names = c(NA,
6L), class = "data.frame")
I've tried tidyr such as
spread(data7, key= "New_Date", value= "index_nsa")
spread(data7, key= "place_id", value= "index_nsa")
However, neither instance produces the desired result but it does give me a number of errors. There are a few instances where the index_nsa has blanks for certain dates and id's but that's fine, I'd just want them to be NA or 0 in those cases.
Any help to solve this would be appreciated!
First we improve the minimal example giving data8 which has 2 id's (we call the second one X) since there can be special issues with one id. We drop the unused levels in place_id. Now use read.zoo . Its split argument will cause the data frame to be split by that column and its entries will be shown as the column names.
library(xts)
# create an example with two id's
data8 <- rbind(data7, transform(data7, place_id = "X"))
data8 <- transform(data8, place_id = droplevels(place_id))
z <- read.zoo(data8, split = 1, index = 3, FUN = as.yearmon)
x <- as.xts(z)
x
giving:
DV_ENC X
Jan 1991 100.00 100.00
Feb 1991 100.99 100.99
Mar 1991 101.36 101.36
Apr 1991 101.75 101.75
May 1991 102.39 102.39
Jun 1991 102.81 102.81

Conversion from xts to zooreg time series removes date values

I hope you are having a nice day!
First, here's the dput of my data:
StreamsTempAveragextsMonthly <- structure(c(16.44, 15.7230769230769, 16.4484358974359, 16.2202307692308,
15.6025, 16.4114423076923, 16.7097115384615, 16.32125, 16.7625,
16.8855769230769, 17.864358974359, 18.4282692307692, 17.5625,
16.9068269230769, 17.1636730769231, 16.8279230769231, 17.09125,
17.8747916666667, 17.2025, 16.9225, 17, 17.75, 17.85, 17.3663461538462,
17.4355769230769, 16.8797115384615, 17.0658717948718, 17.2979230769231,
17.2128205128205, 17.78225, 17.48, 16.9457051282051, 18.535,
18.5871634615385, 17.7346153846154, 18.214188034188, 16.7875,
16.6706196581197, 17.103125, 17.3691346153846, 17.8264423076923,
16.4920192307692, 16.8905128205128, 16.9484615384615, 17.350641025641,
17.9035096153846, 18.1136363636364, 18.0958, 18.125, 17.3089871794872,
17.3978205128205, 17.8078985507246, 17.6580384615385, 17.7281643356643,
17.3222222222222, 17.6125555555556, 18.4708333333333, 18.9261363636364,
18.3610714285714, 18.2842857142857, 17.5, 18.9776818181818, 18.2313068181818,
18.5352272727273, 18.2314393939394, 16.8462121212121, 17.5330452568202,
16.5851136363636, 18.3415151515152, 19.0620454545455, 17.5744047619048,
16.7176308539945, 16.6407407407407, 16.7227272727273, 16.4184848484848,
17.5290598290598, 17.1817965367965, 16.4547619047619, 15.9484848484848,
15.6835902503294, 16.0388865398168, 17.0166666666667, 17.5905555555556,
16.4290088383838, 16.2997452016069, 16.1557023172906, 16.7221212121212,
16.857196969697, 17.3215277777778, 16.5125, 16.0752525252525,
15.5828743589744, 16.6117845117845, 17.9668930686172, 17.5651666666667,
16.3064442224442, 16.2763888888889, 17.650505050505, 16.8803872053872,
17.3298611111111, 17.3772443181818, 17.0242424242424, 16.8111111111111,
16.8055555555556, 17.232601010101, 17.8261363636364, 17.3753787878788,
16.5777272727273, 15.8893939393939, 16.9522435897436, 16.5363636363636,
16.6034090909091, 17.2667929292929, 18.0511363636364, 17.5534090909091,
16.4551136363636, 17.23125, 17.5136363636364, 17.8482954545455,
16.9869318181818, 16.4244318181818, 17.125, 16.8022727272727,
17.4343006993007, 17.2990909090909, 17.1961538461538, 17.009375,
16.9174747474747, 17.2710227272727, 17.6715909090909, 17.4818181818182,
15.9962121212121, 16.5631818181818, 16.6078349282297, 17.1787878787879,
17.0653409090909, 17.4431818181818, 17.4880681818182, 15.0918803418803,
16.7741339869281, 17.4332792207792, 17.7990981240981, 18.0886363636364,
16.8297619047619, 16.6851973684211, 17.2556818181818, 17.0295454545455,
16.8240909090909, 16.7538770053476, 16.2579545454545, 15.9655757575758,
17.1280753968254, 17.7519191919192, 17.4414335664336, 17.4557954545455,
16.1622115384615, 16.8327651515152, 16.9667613636364, 16.672798573975,
16.9659090909091, 17.0818181818182, 17.1822027972028, 17.8164884135472,
17.4545454545455, 16.7771212121212, 17.8238636363636, 17.7005244755245,
16.7160984848485, 16.341754756871, 16.6440025252525, 16.9829545454545,
16.6630555555556, 19.1123467230444, 19.3261363636364, 17.7603021978022,
18.8059107142857, 18.2065559440559, 18.25, 16.965, 18.0582115800866,
17.2105637254902, 18.6762012987013, 18.4136363636364, 18.2600378787879,
18.0003496503497, 17.9288825757576, 17.959375, 18.5164772727273,
18.4772727272727, 18.2170454545455, 18.90625, 16.7813725490196,
17.2660256410256, 17.0635542929293, 17.2422902097902, 17.1887820512821,
16.1727941176471, 16.0860294117647, 18.3868181818182, 18.0298076923077,
18.7031536293164, 18.3659090909091, 18.625, 16.7256818181818,
17.2232600732601, 18.3319639192886, 17.9238636363636, 17.9799422799423,
16.5837121212121, 16.100952540107, 16.9387019230769, 17.3568181818182,
17.1753146853147, 17.1159090909091, 18.1145833333333, 16.8415170940171,
16.9133597285068, 17.6888034188034, 17.1259793447293, 17.384655448718,
16.7841346153846, 16.2973468660969, 16.1201495726496, 17.5849893162393,
18.2456018518519, 18.0254807692308, 17.4643233618234, 17.1731532356532,
17.5413283475783, 17.7156695156695, 17.5869255189255, 17.6197802197802,
17.1997996794872, 17.6756588319088, 18.048433048433, 18.5746082621083,
19.1991987179487, 18.6432081807082, 16.8430288461538, 17.4325367647059,
17.314707977208, 17.5010576923077, 17.9123219373219, 17.4, 17.5033333333333,
17.8433333333333, 18.9353846153846, 18.6049679487179, 17.1923076923077,
17.4415196078431, 17.6505057932264, 17.2357352941176, 17.9955326484661,
17.5959523809524, 17.5952941176471, 16.9622171945701, 18.2641666666667,
17.3842124183007, 18.8371301247772, 18.2442091503268, 17.8693572984749,
17.4261783559578, 17.8408721670486, 18.0430294117647, 17.5234615384615,
18.7757894736842, 18.0545833333333, 18.3861029411765, 17.2286764705882,
19.1830882352941, 18.9825490196078, 19.399375, 18.8928921568627,
17.379375, 17.8381315789474, 18.9048611111111, 18.32625, 18.5,
17.1804924242424, 17.165, 19.0856617647059, 18.6882066993464,
18.949358974359, 18.2374264705882, 17.9036554621849, 17.7655882352941,
18.0464795008913, 19.0757010582011, 18.0125, 17.0503365384615,
17.4858707264957, 18.0303062678063, 19.0284455128205, 18.0894444444444,
18.4038461538462), .Dim = c(295L, 1L), .Dimnames = list(NULL,
"AverageTemp"), index = structure(c(741398400, 742003200,
746668800, 749347200, 751766400, 754185600, 757209600, 759628800,
762048000, 765072000, 767491200, 770256000, 772243200, 775353600,
778118400, 780710400, 783216000, 785635200, 788054400, 791424000,
793238400, 796521600, 798940800, 801360000, 803865600, 807148800,
809740800, 812246400, 814406400, 817603200, 819244800, 823046400,
824860800, 827884800, 830131200, 833328000, 835315200, 838771200,
841190400, 843609600, 846633600, 849052800, 851904000, 854496000,
857088000, 859420800, 861321600, 864864000, 867110400, 870220800,
872640000, 875491200, 878083200, 880502400, 883353600, 885945600,
888364800, 890784000, 893808000, 895190400, 900115200, 904521600,
907027200, 909532800, 911952000, 913161600, 916790400, 919555200,
921715200, 925257600, 928108800, 930700800, 933292800, 935712000,
938131200, 941155200, 942969600, 945734400, 949017600, 951436800,
953856000, 956016000, 958694400, 962064000, 964483200, 966988800,
970185600, 972604800, 974937600, 977443200, 980208000, 982800000,
985910400, 987984000, 991267200, 993772800, 994723200, 999216000,
1001548800, 1004054400, 1006473600, 1008892800, 1012435200, 1014854400,
1017446400, 1019260800, 1022803200, 1025308800, 1027728000, 1030147200,
1032566400, 1034985600, 1038614400, 1039824000, 1043452800, 1045872000,
1048291200, 1050624000, 1053129600, 1056758400, 1059177600, 1060387200,
1064016000, 1067558400, 1070064000, 1071273600, 1074902400, 1077580800,
1079740800, 1083283200, 1084579200, 1087948800, 1091232000, 1093651200,
1096070400, 1098489600, 1100908800, 1103328000, 1106352000, 1108771200,
1111190400, 1114819200, 1116028800, 1117843200, 1122076800, 1124496000,
1128038400, 1130544000, 1133222400, 1135209600, 1138665600, 1140825600,
1143244800, 1145664000, 1148083200, 1151107200, 1154131200, 1156550400,
1158969600, 1161388800, 1163808000, 1166227200, 1169251200, 1171670400,
1175299200, 1177718400, 1180051200, 1183161600, 1185580800, 1.188e+09,
1190419200, 1191628800, 1196294400, 1197676800, 1200614400, 1203120000,
1206748800, 1209168000, 1210377600, 1214006400, 1216339200, 1220054400,
1222473600, 1224892800, 1227312000, 1229731200, 1232755200, 1235174400,
1237507200, 1240012800, 1242432000, 1248480000, 1250553600, 1253318400,
1256947200, 1259280000, 1260576000, 1262995200, 1266710400, 1269043200,
1272585600, 1275091200, 1275350400, 1280534400, 1282953600, 1285372800,
1287792000, 1290211200, 1292630400, 1295568000, 1298073600, 1299283200,
1304035200, 1306454400, 1307664000, 1311984000, 1314316800, 1316822400,
1319241600, 1322611200, 1324080000, 1327104000, 1330473600, 1333152000,
1335571200, 1338249600, 1340323200, 1342828800, 1346371200, 1348790400,
1351209600, 1353628800, 1356134400, 1359158400, 1360368000, 1363910400,
1366416000, 1369699200, 1375228800, 1377648000, 1379635200, 1382486400,
1384992000, 1386892800, 1391126400, 1393545600, 1395360000, 1398297600,
1401235200, 1406246400, 1408579200, 1412035200, 1414713600, 1417046400,
1418428800, 1421712000, 1424217600, 1426723200, 1430352000, 1432944000,
1435276800, 1438214400, 1440979200, 1443398400, 1444867200, 1447372800,
1449619200, 1453334400, 1456704000, 1459382400, 1461715200, 1463011200,
1466726400, 1469059200, 1472601600, 1475020800, 1477440000, 1479168000,
1481155200, 1484179200, 1487721600, 1489968000, 1491436800, 1495065600,
1498780800, 1501027200, 1503446400, 1508544000, 1510963200, 1513296000,
1516233600, 1519171200, 1520812800, 1523318400, 1525219200), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", class = c("xts",
"zoo"))
It has this structure:
An ‘xts’ object on 1993-06-30/2018-05-02 containing:
Data: num [1:295, 1] 16.4 15.7 16.4 16.2 15.6 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "AverageTemp"
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
NULL
Notice how the last date is 2018-05-02? Now, the problem is when I convert my xts object into a zooreg object. I used this code:
StreamsTempzooreg <- zooreg(StreamsTempAveragextsMonthly, start = c(1993,6), end = c(2018,5), frequency = 12)
It has this structure:
‘zooreg’ series from Jun 1993 to Dec 2017
Data: num [1:295, 1] 16.4 15.7 16.4 16.2 15.6 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:295] "1993-06-30" "1993-07-07" "1993-08-30" "1993-09-30" ...
..$ : chr "AverageTemp"
Index: 'yearmon' num [1:295] Jun 1993 Jul 1993 Aug 1993 Sep 1993 ...
Frequency: 12
It changes the last date to December 2017. Why is this happening? How can I fix it so that the original end date is May 2018?
Any help would be greatly appreciated. Thank you.
You want as.zooreg, not zooreg. zooreg is used to construct a zooreg object from its data and index whereas as.zooreg is used to convert other objects to zooreg class. The first argument of zooreg specifies the data portion and the other arguments specify the index portion. The first argument of zooreg should be a numeric vector or matrix but since an xts object was given instead it took the data portion of it ignoring the time index.
as.zooreg can be used to convert from xts class to zooreg class. The first line below does the converstion keeping the Date class index and then the next line converts the index to year/month using the yearmon class.
zr <- as.zooreg(StreamsTempAveragextsMonthly)
zr <- aggregate(zr, as.yearmon, c) ##
range(index(zr))
## [1] "Jun 1993" "May 2018"
The line marked ## could alternatley be written:
index(zr) <- as.yearmon(index(zr))

Irregular gsub in R

I can't get my head around the following odd thing:
I have a data.frame that I need to tidy up, and in this step I want to delete all entries that start with [ANONYMOUS] or a year in ().
Consider this example, a subset with two entries to illustrate my problem.
> print(allstacked2) #mysample- subset
values ind
711 [ANONYMOUS], 2010, COMMUNICATION. V1
1588 [ANONYMOUS], 2008, END UNLAWFUL ATTACKS. V2
1297 (1986) ACTIVE MEASURES, , U.S. DEPARTMENT OF STATE, AUGUST 1986 V1
3994 (1900) NEW YORK TIMES, P. 17. , 22 APRIL V3
> pat <- "(?:^)\\(.*"
> repl <- NA
> allstacked2$V3 <- gsub(pat, repl, allstacked2$values, perl=TRUE)
>
> pat <- "(^)\\[ANONYMOUS]"
> repl <- NA
> allstacked2$V3 <- gsub(pat, repl, allstacked2$V3, perl=TRUE)
>
> pat <- "[\\s]+"
> repl <- " "
> allstacked2$V3 <- gsub(pat, repl, allstacked2$V3, perl=TRUE)
>
> allstacked2[is.na(allstacked2)] = 'notavailable'
> allstacked2$V4 <- ifelse(allstacked2$V3==allstacked2$values,1,0) #to compare what has changed
> print(allstacked2)
values ind V3 V4
711 [ANONYMOUS], 2010, COMMUNICATION. V1 notavailable 0
1588 [ANONYMOUS], 2008, END UNLAWFUL ATTACKS. V2 [ANONYMOUS], 2008, END UNLAWFUL ATTACKS. 1
1297 (1986) ACTIVE MEASURES, , U.S. DEPARTMENT OF STATE, AUGUST 1986 V1 notavailable 0
3994 (1900) NEW YORK TIMES, P. 17. , 22 APRIL V3 (1900) NEW YORK TIMES, P. 17. , 22 APRIL 1
>
It seems to me that the problem has to do with the index colum, which I need to keep in order to unstack my data later.
I just don't get why. Thanks for any help here!!
EDIT:
Wiktor to the rescue - this is what happens when I use his pattern. It takes the stuff I want to replace, but won't replace the whole pattern. And I still wonder why it would return different results in my own regex.
> print(allstacked2)
values ind V3 V4
711 [ANONYMOUS], 2010, COMMUNICATION. V1 notavailable 0
1297 (1986) ACTIVE MEASURES, , U.S. DEPARTMENT OF STATE, AUGUST 1986 V1 notavailable 0
1588 [ANONYMOUS], 2008, END UNLAWFUL ATTACKS. V2 [ANONYMOUS], 2008, END UNLAWFUL ATTACKS. 1
3994 (1900) NEW YORK TIMES, P. 17. , 22 APRIL V3 (1900) NEW YORK TIMES, P. 17. , 22 APRIL 1
> allstacked2$V3 <- gsub("^[[:space:]]*(\\(\\d{4}\\)|\\[ANONYMOUS])[[:space:]]*", "", allstacked2$V3)
> allstacked2$V3 <- gsub("[[:space:]]+", " ", allstacked2$V3)
>
>
> print(allstacked2)
values ind V3 V4
711 [ANONYMOUS], 2010, COMMUNICATION. V1 notavailable 0
1297 (1986) ACTIVE MEASURES, , U.S. DEPARTMENT OF STATE, AUGUST 1986 V1 notavailable 0
1588 [ANONYMOUS], 2008, END UNLAWFUL ATTACKS. V2 , 2008, END UNLAWFUL ATTACKS. 1
3994 (1900) NEW YORK TIMES, P. 17. , 22 APRIL V3 NEW YORK TIMES, P. 17. , 22 APRIL 1
>
Edit: Here the dput!
> dput(allstacked2)
structure(list(values = c("[ANONYMOUS], 2010, COMMUNICATION.",
"(1986) ACTIVE MEASURES, , U.S. DEPARTMENT OF STATE, AUGUST 1986",
" [ANONYMOUS], 2008, END UNLAWFUL ATTACKS.", " (1900) NEW YORK TIMES, P. 17. , 22 APRIL"
), ind = structure(c(1L, 1L, 2L, 3L), .Label = c("V1", "V2",
"V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12",
"V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20", "V21",
"V22", "V23", "V24", "V25", "V26", "V27", "V28", "V29", "V30",
"V31", "V32", "V33", "V34", "V35", "V36", "V37", "V38", "V39",
"V40", "V41", "V42", "V43", "V44", "V45", "V46", "V47", "V48",
"V49", "V50", "V51", "V52", "V53", "V54", "V55", "V56", "V57",
"V58", "V59", "V60", "V61", "V62", "V63", "V64", "V65", "V66",
"V67", "V68", "V69", "V70", "V71", "V72", "V73", "V74", "V75",
"V76", "V77", "V78", "V79", "V80", "V81", "V82", "V83", "V84",
"V85", "V86", "V87", "V88", "V89", "V90", "V91", "V92", "V93",
"V94", "V95", "V96", "V97", "V98", "V99", "V100", "V101", "V102",
"V103", "V104", "V105", "V106", "V107", "V108", "V109", "V110",
"V111", "V112", "V113", "V114", "V115", "V116", "V117", "V118",
"V119", "V120", "V121", "V122", "V123", "V124", "V125", "V126",
"V127", "V128", "V129", "V130", "V131", "V132", "V133", "V134",
"V135", "V136", "V137", "V138", "V139", "V140", "V141", "V142",
"V143", "V144", "V145", "V146", "V147", "V148", "V149", "V150",
"V151", "V152", "V153", "V154", "V155", "V156", "V157", "V158",
"V159", "V160", "V161", "V162", "V163", "V164", "V165", "V166",
"V167", "V168", "V169", "V170", "V171", "V172", "V173", "V174",
"V175", "V176", "V177", "V178", "V179", "V180", "V181", "V182",
"V183", "V184", "V185", "V186", "V187", "V188", "V189", "V190",
"V191", "V192", "V193", "V194", "V195", "V196", "V197", "V198",
"V199", "V200", "V201", "V202", "V203", "V204", "V205", "V206",
"V207", "V208", "V209", "V210", "V211", "V212", "V213", "V214",
"V215", "V216", "V217", "V218", "V219", "V220", "V221", "V222",
"V223", "V224", "V225", "V226", "V227", "V228", "V229", "V230",
"V231", "V232", "V233", "V234", "V235", "V236", "V237", "V238",
"V239", "V240", "V241", "V242", "V243", "V244", "V245", "V246",
"V247", "V248", "V249", "V250", "V251", "V252", "V253", "V254",
"V255", "V256", "V257", "V258", "V259", "V260", "V261", "V262",
"V263", "V264", "V265", "V266", "V267", "V268", "V269", "V270",
"V271", "V272", "V273", "V274", "V275", "V276", "V277", "V278",
"V279", "V280", "V281", "V282", "V283", "V284", "V285", "V286",
"V287", "V288", "V289", "V290", "V291", "V292", "V293", "V294",
"V295", "V296", "V297", "V298", "V299", "V300", "V301", "V302",
"V303", "V304", "V305", "V306", "V307", "V308", "V309", "V310",
"V311", "V312", "V313", "V314", "V315", "V316", "V317", "V318",
"V319", "V320", "V321", "V322", "V323", "V324", "V325", "V326",
"V327", "V328", "V329", "V330", "V331", "V332", "V333", "V334",
"V335", "V336", "V337", "V338", "V339", "V340", "V341", "V342",
"V343", "V344", "V345", "V346", "V347", "V348", "V349", "V350",
"V351", "V352", "V353", "V354", "V355", "V356", "V357", "V358",
"V359", "V360", "V361", "V362", "V363", "V364", "V365", "V366",
"V367", "V368", "V369", "V370", "V371", "V372", "V373", "V374",
"V375", "V376", "V377", "V378", "V379", "V380", "V381", "V382",
"V383", "V384", "V385", "V386", "V387", "V388", "V389", "V390",
"V391", "V392", "V393", "V394", "V395", "V396", "V397", "V398",
"V399", "V400", "V401", "V402", "V403", "V404", "V405", "V406",
"V407", "V408", "V409", "V410", "V411", "V412", "V413", "V414",
"V415", "V416", "V417", "V418", "V419", "V420", "V421", "V422",
"V423", "V424", "V425", "V426", "V427", "V428", "V429", "V430",
"V431", "V432", "V433", "V434", "V435", "V436", "V437", "V438",
"V439", "V440", "V441", "V442", "V443", "V444", "V445", "V446",
"V447", "V448", "V449", "V450", "V451", "V452", "V453", "V454",
"V455", "V456", "V457", "V458", "V459", "V460", "V461", "V462",
"V463", "V464", "V465", "V466", "V467", "V468", "V469", "V470",
"V471", "V472", "V473", "V474", "V475", "V476", "V477", "V478",
"V479", "V480", "V481", "V482", "V483", "V484", "V485", "V486",
"V487", "V488", "V489", "V490", "V491", "V492", "V493", "V494",
"V495", "V496", "V497", "V498", "V499", "V500", "V501", "V502",
"V503", "V504", "V505", "V506", "V507", "V508", "V509", "V510",
"V511", "V512", "V513", "V514", "V515", "V516", "V517", "V518",
"V519", "V520", "V521", "V522", "V523", "V524", "V525", "V526",
"V527", "V528", "V529", "V530", "V531", "V532", "V533", "V534",
"V535", "V536", "V537", "V538", "V539", "V540", "V541", "V542",
"V543", "V544", "V545", "V546", "V547", "V548", "V549", "V550",
"V551", "V552", "V553", "V554", "V555", "V556", "V557", "V558",
"V559", "V560", "V561", "V562", "V563", "V564", "V565", "V566",
"V567", "V568", "V569", "V570", "V571", "V572", "V573", "V574",
"V575", "V576", "V577", "V578", "V579", "V580", "V581", "V582",
"V583", "V584", "V585", "V586", "V587", "V588", "V589", "V590",
"V591", "V592", "V593", "V594", "V595", "V596", "V597", "V598",
"V599", "V600", "V601", "V602", "V603", "V604", "V605", "V606",
"V607", "V608", "V609", "V610", "V611", "V612", "V613", "V614",
"V615", "V616", "V617", "V618", "V619", "V620", "V621", "V622",
"V623", "V624", "V625", "V626", "V627", "V628", "V629", "V630",
"V631", "V632", "V633", "V634", "V635", "V636", "V637", "V638",
"V639", "V640", "V641", "V642", "V643", "V644", "V645", "V646",
"V647", "V648", "V649", "V650", "V651", "V652", "V653", "V654",
"V655", "V656", "V657", "V658", "V659", "V660", "V661", "V662",
"V663", "V664", "V665", "V666", "V667", "V668", "V669", "V670",
"V671", "V672", "V673", "V674", "V675", "V676", "V677", "V678",
"V679", "V680", "V681", "V682", "V683", "V684", "V685", "V686",
"V687", "V688", "V689", "V690", "V691", "V692", "V693", "V694",
"V695", "V696", "V697", "V698", "V699", "V700", "V701", "V702",
"V703", "V704", "V705", "V706", "V707", "V708", "V709", "V710",
"V711", "V712", "V713", "V714", "V715", "V716", "V717", "V718",
"V719", "V720", "V721", "V722", "V723", "V724", "V725", "V726",
"V727", "V728", "V729", "V730", "V731", "V732", "V733", "V734",
"V735", "V736", "V737", "V738", "V739", "V740", "V741", "V742",
"V743", "V744", "V745", "V746", "V747", "V748", "V749", "V750",
"V751", "V752", "V753", "V754", "V755", "V756", "V757", "V758",
"V759", "V760", "V761", "V762", "V763", "V764", "V765", "V766",
"V767", "V768", "V769", "V770", "V771", "V772", "V773", "V774",
"V775", "V776", "V777", "V778", "V779", "V780", "V781", "V782",
"V783", "V784", "V785", "V786", "V787", "V788", "V789", "V790",
"V791", "V792", "V793", "V794", "V795", "V796", "V797", "V798",
"V799", "V800", "V801", "V802", "V803", "V804", "V805", "V806",
"V807", "V808", "V809", "V810", "V811", "V812", "V813", "V814",
"V815", "V816", "V817", "V818", "V819", "V820", "V821", "V822",
"V823", "V824", "V825", "V826", "V827", "V828", "V829", "V830",
"V831", "V832", "V833", "V834", "V835", "V836", "V837", "V838",
"V839", "V840", "V841", "V842", "V843", "V844", "V845", "V846",
"V847", "V848", "V849", "V850", "V851", "V852", "V853", "V854",
"V855", "V856", "V857", "V858", "V859", "V860", "V861", "V862",
"V863", "V864", "V865", "V866", "V867", "V868", "V869", "V870",
"V871", "V872", "V873", "V874", "V875", "V876", "V877", "V878",
"V879", "V880", "V881", "V882", "V883", "V884", "V885", "V886",
"V887", "V888", "V889", "V890", "V891", "V892", "V893", "V894",
"V895", "V896", "V897", "V898", "V899", "V900", "V901", "V902",
"V903", "V904", "V905", "V906", "V907", "V908", "V909", "V910",
"V911", "V912", "V913", "V914", "V915", "V916", "V917", "V918",
"V919", "V920", "V921", "V922", "V923", "V924", "V925", "V926",
"V927", "V928", "V929", "V930", "V931", "V932", "V933", "V934",
"V935", "V936", "V937", "V938", "V939", "V940", "V941", "V942",
"V943", "V944", "V945", "V946", "V947", "V948", "V949", "V950",
"V951", "V952", "V953", "V954", "V955", "V956", "V957", "V958",
"V959", "V960", "V961", "V962", "V963", "V964", "V965", "V966",
"V967", "V968", "V969", "V970", "V971", "V972", "V973", "V974",
"V975", "V976", "V977", "V978", "V979", "V980", "V981", "V982",
"V983", "V984", "V985", "V986", "V987", "V988", "V989", "V990",
"V991", "V992", "V993", "V994", "V995", "V996", "V997", "V998",
"V999", "V1000", "V1001", "V1002", "V1003", "V1004", "V1005",
"V1006", "V1007", "V1008", "V1009", "V1010", "V1011", "V1012",
"V1013", "V1014", "V1015", "V1016", "V1017", "V1018", "V1019",
"V1020", "V1021", "V1022", "V1023", "V1024", "V1025", "V1026",
"V1027", "V1028", "V1029", "V1030", "V1031", "V1032", "V1033",
"V1034", "V1035", "V1036", "V1037", "V1038", "V1039", "V1040",
"V1041", "V1042", "V1043", "V1044", "V1045", "V1046", "V1047",
"V1048", "V1049", "V1050", "V1051", "V1052", "V1053", "V1054",
"V1055", "V1056", "V1057", "V1058", "V1059", "V1060", "V1061",
"V1062", "V1063", "V1064", "V1065", "V1066", "V1067", "V1068",
"V1069", "V1070", "V1071", "V1072", "V1073", "V1074", "V1075",
"V1076", "V1077", "V1078", "V1079", "V1080", "V1081", "V1082",
"V1083", "V1084", "V1085", "V1086", "V1087", "V1088", "V1089",
"V1090", "V1091", "V1092", "V1093", "V1094", "V1095", "V1096",
"V1097", "V1098", "V1099", "V1100", "V1101", "V1102", "V1103",
"V1104", "V1105", "V1106", "V1107", "V1108", "V1109", "V1110",
"V1111", "V1112", "V1113", "V1114", "V1115", "V1116", "V1117",
"V1118", "V1119", "V1120", "V1121", "V1122", "V1123", "V1124",
"V1125", "V1126", "V1127", "V1128", "V1129", "V1130", "V1131",
"V1132", "V1133", "V1134", "V1135", "V1136", "V1137", "V1138",
"V1139", "V1140", "V1141", "V1142", "V1143", "V1144", "V1145",
"V1146", "V1147", "V1148", "V1149", "V1150", "V1151", "V1152",
"V1153", "V1154", "V1155", "V1156", "V1157", "V1158", "V1159",
"V1160", "V1161", "V1162", "V1163", "V1164", "V1165", "V1166",
"V1167", "V1168", "V1169", "V1170", "V1171", "V1172", "V1173",
"V1174", "V1175", "V1176", "V1177", "V1178", "V1179", "V1180",
"V1181", "V1182", "V1183", "V1184", "V1185", "V1186", "V1187",
"V1188", "V1189", "V1190", "V1191", "V1192", "V1193", "V1194",
"V1195", "V1196", "V1197", "V1198", "V1199", "V1200", "V1201",
"V1202", "V1203", "V1204", "V1205", "V1206", "V1207", "V1208",
"V1209", "V1210", "V1211", "V1212", "V1213", "V1214", "V1215",
"V1216", "V1217", "V1218", "V1219", "V1220", "V1221", "V1222",
"V1223", "V1224", "V1225", "V1226", "V1227", "V1228", "V1229",
"V1230", "V1231", "V1232", "V1233", "V1234", "V1235", "V1236",
"V1237", "V1238", "V1239", "V1240", "V1241", "V1242", "V1243",
"V1244", "V1245", "V1246", "V1247", "V1248", "V1249", "V1250",
"V1251", "V1252", "V1253", "V1254", "V1255", "V1256", "V1257",
"V1258", "V1259", "V1260", "V1261", "V1262", "V1263", "V1264",
"V1265", "V1266", "V1267", "V1268", "V1269", "V1270", "V1271",
"V1272", "V1273", "V1274", "V1275", "V1276", "V1277", "V1278",
"V1279", "V1280", "V1281", "V1282", "V1283", "V1284", "V1285",
"V1286", "V1287", "V1288", "V1289", "V1290", "V1291", "V1292",
"V1293", "V1294", "V1295", "V1296", "V1297", "V1298", "V1299",
"V1300", "index", "index2"), class = "factor")), .Names = c("values",
"ind"), row.names = c(711L, 1297L, 1588L, 3994L), class = "data.frame")
>
You have some whitespaces at the start of the input. You need to make sure you match those with [[:space:]]*:
allstacked2$V3 <- gsub("^[[:space:]]*(\\(\\d{4}\\)|\\[ANONYMOUS],)[[:space:]]*", "", allstacked2$V3)
allstacked2$V3 <- gsub("[[:space:]]+", " ", allstacked2$V3)
See the R demo online.
Details
^ - start of string
[[:space:]]* - 0+ whitespaces
(\\(\\d{4}\\)|\\[ANONYMOUS],) - one of the two alternatives:
\\(\\d{4}\\) - a (, 4 digits, )
| - or
\\[ANONYMOUS], - [ANONYMOUS], substring
[[:space:]]* - 0+ whitespaces
The [[:space:]] in a TRE regex will match Unicode whitespaces, so it is preferable than \s.

Daylight savings and the date_histogram in kibana 4

I am trying to create a graph containing a date histogram. Everything is working except when crossing the daylight savings period. Check the table below for an example, notice the problem with the line of October 31st. Does anyone know a way to overcome this problem and get buckets beginning on each first of the month. The inputs are dates without a time, so for every record the time is midnight.
April 1st 2013, 00:00:00.000 11499
May 1st 2013, 00:00:00.000 11850
June 1st 2013, 00:00:00.000 15992
July 1st 2013, 00:00:00.000 14215
August 1st 2013, 00:00:00.000 14178
September 1st 2013, 00:00:00.000 12739
October 1st 2013, 00:00:00.000 14133
October 31st 2013, 23:00:00.000 13653
November 30th 2013, 23:00:00.000 12537
December 31st 2013, 23:00:00.000 14040
Just to make things better to debug, this is the executed query:
{
"size": 0,
"query": {
"query_string": {
"query": "*",
"analyze_wildcard": true
}
},
"aggs": {
"2": {
"date_histogram": {
"field": "orderCreationDate",
"interval": "1M",
"pre_zone": "+02:00",
"pre_zone_adjust_large_interval": true,
"min_doc_count": 1
}
}
}
}

Selecting dates and time interval from observations in R

Having an object of class zoo we can select observations for a range of dates of interest using the function:
window(z, start = as.Date("2006-01-05"), end = as.Date("2006-01-08"))
In running this function the following warning message occurs:
Warning messages:
1: In which(in.index & all.indexes >= start & all.indexes <= end) :
Metodi incompatibili ("Ops.POSIXt", "Ops.Date") per ">="
2: In which(in.index & all.indexes >= start & all.indexes <= end) :
Metodi incompatibili ("Ops.POSIXt", "Ops.Date") per "<="
I have checked that the object is of class zoo and that Dates are included in the time series.
How is that possible?
Below the str(z) as requested:
‘zoo’ series from 2006-01-03 to 2013-01-24
Data: num [1:1795, 1:40] 3.65 3.68 3.69 3.72 3.7 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:1795] "1" "2" "3" "4" ...
..$ : chr [1:40] "EURARS" "EURAUD" "EURBRO" "EURCAD" ...
Index: POSIXct[1:1795], format: "2006-01-03" "2006-01-04" "2006-01-05" "2006-01-06" ...
Below the dput(head(z)) as requested:
structure(c(3.6511, 3.6833, 3.6931, 3.7152, 3.7027, 3.6897, 1.62349,
1.62257, 1.62011, 1.6115, 1.60243, 1.61108, 2.802, 2.7692, 2.7727,
2.7741, 2.7238, 2.729, 1.38937, 1.39109, 1.40716, 1.41627, 1.41196,
1.40666, 1.55055, 1.5472, 1.5448, 1.54335, 1.54215, 1.545, 623.73,
624.16, 628.43, 638.11, 632.27, 630.7, 9.6988, 9.7803, 9.7689,
9.802, 9.7492, 9.7354, 2742.03, 2765.68, 2758.65, 2769.27, 2753.3,
2747.31, 29.047, 28.972, 28.9, 28.88, 28.764, 28.792, 7.4616,
7.4601, 7.4612, 7.458, 7.46, 7.4589, 6.8983, 6.9551, 6.9594,
6.9838, 6.9374, 6.9253, 0.6882, 0.68905, 0.68961, 0.6863, 0.68473,
0.68358, 9.3178, 9.3963, 9.3889, 9.4207, 9.3702, 9.3516, 251.72,
250.28, 250.66, 250.39, 249.89, 250.86, 11657.46, 11677.26, 11612.05,
11603.59, 11433.35, 11403.84, 5.5244, 5.5808, 5.5799, 5.6134,
5.5858, 5.5957, 53.5288, 54.0151, 54.0323, 54.0105, 53.5591,
53.6189, 74.96, 74.88, 74.41, 73.94, 73.79, 73.84, 139.6, 140.71,
140.39, 139.09, 138.39, 137.93, 1208.3493, 1210.0214, 1195.7746,
1200.3966, 1181.3457, 1184.9635, 160.65, 162.15, 162.02, 162.53,
161.7, 161.43, 10.9802, 10.997, 10.9947, 10.9635, 10.9909, 10.9874,
12.7724, 12.8255, 12.8746, 12.8338, 12.7859, 12.8273, 4.5416,
4.5702, 4.5623, 4.5597, 4.5308, 4.5229, 7.9654, 7.9248, 7.9254,
7.914, 7.9574, 8.0011, 1.7571, 1.7626, 1.7622, 1.7574, 1.7411,
1.7391, 4.1276, 4.1608, 4.1665, 4.1818, 4.1606, 4.1534, 63.2627,
63.4733, 63.6725, 63.7198, 63.3608, 63.3412, 3.8295, 3.8116,
3.807, 3.805, 3.7609, 3.7799, 3.6732, 3.6815, 3.6834, 3.6842,
3.6644, 3.6492, 34.5475, 34.8363, 34.7254, 34.8369, 34.68, 34.39,
9.3648, 9.3279, 9.3321, 9.3152, 9.3389, 9.3603, 1.9844, 1.9932,
1.9938, 1.9902, 1.9766, 1.9716, 48.9853, 48.9426, 48.6762, 48.3184,
47.9995, 48.0187, 1.6149, 1.6195, 1.6193, 1.6204, 1.6155, 1.6148,
1.6129, 1.6175, 1.6184, 1.6201, 1.6182, 1.6221, 39.2261, 39.1868,
38.7569, 39.1189, 38.6148, 38.6309, 6.0673, 6.114, 6.1208, 6.1484,
6.1095, 6.1082, 1.2019, 1.2119, 1.211, 1.2151, 1.2088, 1.2065,
7.4834, 7.4559, 7.4658, 7.3872, 7.3206, 7.3497), .Dim = c(6L,
40L), .Dimnames = list(c("1", "2", "3", "4", "5", "6"), c("EURARS",
"EURAUD", "EURBRO", "EURCAD", "EURCHF", "EURCLP", "EURCNO", "EURCOP",
"EURCZK", "EURDKK", "EUREGP", "EURGBP", "EURHKD", "EURHUF", "EURIDO",
"EURILS", "EURINO", "EURISK", "EURJPY", "EURKRO", "EURKZT", "EURMAD",
"EURMXN", "EURMYO", "EURNOK", "EURNZD", "EURPEN", "EURPHO", "EURPLN",
"EURRON", "EURRUB", "EURSEK", "EURSGO", "EURTHO", "EURTND", "EURTRY",
"EURTWO", "EURUAH", "EURUSD", "EURZAR")), index = structure(c(1136242800,
1136329200, 1136415600, 1136502000, 1136761200, 1136847600), class = c("POSIXct",
"POSIXt"), tzone = ""), class = "zoo")
You shouldn't be comparing Date with POSIXct
Try this:
window(z, start = as.POSIXct("2006-01-05"), end = as.POSIXct("2006-01-08"))
Alternatively, as #JoshuaUlrich points out in a comment, your data is daily frequency, so you'd be better off using a Date index class for your data to avoid timezone weirdness.
index(z) <- as.Date(index(z))
window(z, start = as.Date("2006-01-05"), end = as.Date("2006-01-08"))
It looks like window doesn't convert between Date and POSIXct automatically. You have to specify the times in the same class as in the data:
window(z, start = as.POSIXct("2006-01-05"), end = as.POSIXct("2006-01-08"))
EURARS EURAUD EURBRO EURCAD EURCHF EURCLP EURCNO EURCOP
2006-01-05 23:00:00 3.7152 1.6115 2.7741 1.41627 1.54335 638.11 9.802 2769.27
EURCZK EURDKK EUREGP EURGBP EURHKD EURHUF EURIDO EURILS
2006-01-05 23:00:00 28.88 7.458 6.9838 0.6863 9.4207 250.39 11603.59 5.6134
EURINO EURISK EURJPY EURKRO EURKZT EURMAD EURMXN
2006-01-05 23:00:00 54.0105 73.94 139.09 1200.397 162.53 10.9635 12.8338
EURMYO EURNOK EURNZD EURPEN EURPHO EURPLN EURRON EURRUB
2006-01-05 23:00:00 4.5597 7.914 1.7574 4.1818 63.7198 3.805 3.6842 34.8369
EURSEK EURSGO EURTHO EURTND EURTRY EURTWO EURUAH EURUSD
2006-01-05 23:00:00 9.3152 1.9902 48.3184 1.6204 1.6201 39.1189 6.1484 1.2151
EURZAR
2006-01-05 23:00:00 7.3872

Resources