Re: [R] How to sum and group data by DATE in data frame
Well, it's not quite the dput(head(X, 25)) I asked for, but it worksthank you :-) Do take a look at the examples for aggregate -- then the right syntax is pretty clear z <- aggregate(x, timeSequence(from = start(x), to = end(x), by = "day"), sum) but there seems to be some little issue that makes you miss the endpoints of each day -- I think that's because your first and last points aren't quite on the midnight boundary. This might work better aggregate(x, timeSequence(from = as.Date(start(x)), to = as.Date(end(x)), by = "day"), FUN = sum) Personally, I find the timeSeries framework inordinately painful to work with and I'd suggest you use zoo/xts objects instead Best, Michael On Thu, May 17, 2012 at 2:48 AM, Cren wrote: > Thank you, Michael :) > > Michael Weylandt wrote >> >> If that doesn't nail it down, I'll need you to answer the questions I >> asked in my previous email. > Previously I made a mistake with *dput()*, this is the correct output: > >> dput(X) > new("timeSeries" > , .Data = structure(c(124.3, 124.38, 124.67, 125.19, 124.9, 125.27, > 125.5, > 125.58, 125.91, 125.8, 125.83, 126.215, 126.25, 126.25, 124.901, > 124.43, 124.4654, 124.46, 124.68, 124.86, 124.73, 125.22, 125.48, > 125.5601, 125.4091, 125.15, 125.43, 125.481, 125.91, 125.29, > 124.79, 124.77, 124.7, 124.37, 124.56, 124.86, 125.3, 125.59, > 125.95, 125.73, 126.27, 126.26, 127.33, 126.37, 126.46, 126, > 126.06, 126.2662, 126.23, 126.4499, 127.12, 127.48, 127.49, 127.69, > 127.88, 127.88, 124.51, 124.42, 124.92, 125.18, 125.23, 124.81, > 125.07, 124.61, 123.8869, 123.24, 123.3329, 123.6, 123.19, 123.161, > 123.96, 123.58, 123.42, 123.68, 124.19, 123.985, 124.24, 124.61, > 124.6566, 124.18, 123.795, 124.36, 124.32, 124.32, 126.1101, > 126.42, 126.8181, 126.79, 126.675, 126.68, 126.685, 126.725, > 126.65, 126.77, 126.55, 126.63, 126.67, 126.66, 125.8829, 126.05, > 125.97, 125.99, 125.59, 125.21, 125.31, 125.46, 125.39, 125.23, > 124.93, 125.32, 125.46, 125.46, 125.62, 125.34, 125.163, 125.01, > 125.115, 125.67, 126.08, 126.15, 126.33, 126.19, 126.3955, 126.71, > 126.083, 126.083, 125.23, 124.96, 125.595, 125.77, 125.4, 125.38, > 125.54, 126.01, 126.05, 126.01, 125.72, 124.79, 124.05, 124.08, > 123.35, 123.85, 123.75, 123.5, 123.58, 122.9285, 122.14, 122.02, > 121.86, 121.58, 121.79, 121.68, 122.18, 122.105, 122.37, 122.02, > 122.4765, 122.31, 121.99, 121.8401, 121.85, 122.17, 122.27, 122.26, > 122.08, 122.02, 121.979, 121.979, 119.74, 119.55, 118.95, 119.12, > 119.14, 118.91, 119.0201, 119.235, 118.93, 119.51, 119.51, 120.04, > 119.63, 119.661, 119.77, 119.78, 118.89, 118.85, 118.9, 119.2671, > 119.73, 119.8, 119.52, 119.43, 119.16, 119.28, 119.19, 119.19, > 117.43, 117.18, 117.3, 116.87, 117.07, 117.0565, 117.4497, 117.47, > 117.07, 116.856, 116.91, 117.075, 116.6, 116.56, 117.09, 117.361, > 117.35, 117.2787, 116.72, 116.76, 116.34, 116.34, 116.34, 119.88, > 119.98, 119.8095, 119.995, 119.88, 119.685, 119.48, 119.845, > 119.505, 119.65, 119.36, 119.02, 119.73, 119.71, 120.44, 120.65, > 120.45, 120.53, 120.41, 120.67, 120.399, 120.39, 119.91, 120.069, > 120.18, 120.37, 120.05, 120.05, 123.9711, 124.01, 124.47, 124.14, > 124.14, 124.12, 124.14, 123.94, 123.86, 124, 124.07, 124.33, > 125.04, 124.99, 125.47, 125.39, 124.91, 124.5802, 124.88, 124.89, > 124.48, 124.88, 125.335, 125.12, 125.07, 125.25, 124.96, 124.97, > 126.02, 126.41, 126.19, 125.93, 125.5199, 125.575, 125.6702, > 125.471, 125.506, 125.41, 125.14, 125.18, 124.86, 124.86, 126.63, > 127.01, 126.881, 126.86, 126.81, 126.86, 127.05, 126.945, 126.2946, > 126.1511, 126.28, 125.54, 126.24, 126.22, 125.99, 126.41, 126.12, > 126.05, 126.17, 126.13, 126.14, 126.43, 126.26, 126.35, 126.89, > 126.645, 126.26, 126.26, 125.5, 125.12, 125.49, 125.86, 125.96, > 126.28, 126.37, 126.35, 126.115, 126.1, 125.83, 126.42, 126.73, > 126.73, 125.79, 125.32, 125.4, 124.98, 125.0601, 125, 124.84, > 124.5, 124.6246, 124.56, 124.29, 124.78, 123.98, 123.95, 125.13, > 125.24, 125.48, 125.84, 125.73, 125.76, 125.87, 126.02, 125.9, > 126.09, 126.19, 126.21, 126.05, 126.05, 124.208, 123.96, 124.04, > 123.9, 123.4, 123.48, 123.5046, 123.55, 123.5911, 123.59, 123.71, > 123.825, 124.19, 124.21, 125.38, 125.0199, 124.64, 124.64, 124.85, > 124.675, 124.79, 124.67, 124.79, 124.53, 123.86, 123.1, 123.05, > 123.05, 122.42, 122.25, 121.81, 121.77, 121.66, 121.94, 122.1484, > 122.5, 122.0654, 122.07, 121.67, 121.96, 121.71, 121.74, 123.06, > 122.57, 122.21, 122.55, 122.31, 122.71, 122.47, 122.4773, 122.405, > 122.31, 122.18, 122.37, 122.18, 122.185, 122.44, 122.68, 122.69, > 122.51, 121.9, 121.74, 121.6, 121.78, 121.5999, 121.46, 121.67, > 121.57, 121.59, 121.59, 122.05, 121.892, 121.47, 121.28, 120.85, > 121.09, 121.15, 121.02, 121.24, 121.02, 121.07, 120.21, 120.32, > 120.29, 122.99, 123.3, 123.35, 123.47, 123.4254, 123.469, 123.54, > 123.72, 123.7, 123.72, 123.9, 123.97, 123.92, 123.93, 123.26, > 123.49, 123.31, 123.13, 123.2, 122.85, 123.34, 123.34, 123.59, > 123.57, 123.73, 124.03, 124.17, 124.1
Re: [R] How to sum and group data by DATE in data frame
Thank you, Michael :) Michael Weylandt wrote > > If that doesn't nail it down, I'll need you to answer the questions I > asked in my previous email. Previously I made a mistake with *dput()*, this is the correct output: > dput(X) new("timeSeries" , .Data = structure(c(124.3, 124.38, 124.67, 125.19, 124.9, 125.27, 125.5, 125.58, 125.91, 125.8, 125.83, 126.215, 126.25, 126.25, 124.901, 124.43, 124.4654, 124.46, 124.68, 124.86, 124.73, 125.22, 125.48, 125.5601, 125.4091, 125.15, 125.43, 125.481, 125.91, 125.29, 124.79, 124.77, 124.7, 124.37, 124.56, 124.86, 125.3, 125.59, 125.95, 125.73, 126.27, 126.26, 127.33, 126.37, 126.46, 126, 126.06, 126.2662, 126.23, 126.4499, 127.12, 127.48, 127.49, 127.69, 127.88, 127.88, 124.51, 124.42, 124.92, 125.18, 125.23, 124.81, 125.07, 124.61, 123.8869, 123.24, 123.3329, 123.6, 123.19, 123.161, 123.96, 123.58, 123.42, 123.68, 124.19, 123.985, 124.24, 124.61, 124.6566, 124.18, 123.795, 124.36, 124.32, 124.32, 126.1101, 126.42, 126.8181, 126.79, 126.675, 126.68, 126.685, 126.725, 126.65, 126.77, 126.55, 126.63, 126.67, 126.66, 125.8829, 126.05, 125.97, 125.99, 125.59, 125.21, 125.31, 125.46, 125.39, 125.23, 124.93, 125.32, 125.46, 125.46, 125.62, 125.34, 125.163, 125.01, 125.115, 125.67, 126.08, 126.15, 126.33, 126.19, 126.3955, 126.71, 126.083, 126.083, 125.23, 124.96, 125.595, 125.77, 125.4, 125.38, 125.54, 126.01, 126.05, 126.01, 125.72, 124.79, 124.05, 124.08, 123.35, 123.85, 123.75, 123.5, 123.58, 122.9285, 122.14, 122.02, 121.86, 121.58, 121.79, 121.68, 122.18, 122.105, 122.37, 122.02, 122.4765, 122.31, 121.99, 121.8401, 121.85, 122.17, 122.27, 122.26, 122.08, 122.02, 121.979, 121.979, 119.74, 119.55, 118.95, 119.12, 119.14, 118.91, 119.0201, 119.235, 118.93, 119.51, 119.51, 120.04, 119.63, 119.661, 119.77, 119.78, 118.89, 118.85, 118.9, 119.2671, 119.73, 119.8, 119.52, 119.43, 119.16, 119.28, 119.19, 119.19, 117.43, 117.18, 117.3, 116.87, 117.07, 117.0565, 117.4497, 117.47, 117.07, 116.856, 116.91, 117.075, 116.6, 116.56, 117.09, 117.361, 117.35, 117.2787, 116.72, 116.76, 116.34, 116.34, 116.34, 119.88, 119.98, 119.8095, 119.995, 119.88, 119.685, 119.48, 119.845, 119.505, 119.65, 119.36, 119.02, 119.73, 119.71, 120.44, 120.65, 120.45, 120.53, 120.41, 120.67, 120.399, 120.39, 119.91, 120.069, 120.18, 120.37, 120.05, 120.05, 123.9711, 124.01, 124.47, 124.14, 124.14, 124.12, 124.14, 123.94, 123.86, 124, 124.07, 124.33, 125.04, 124.99, 125.47, 125.39, 124.91, 124.5802, 124.88, 124.89, 124.48, 124.88, 125.335, 125.12, 125.07, 125.25, 124.96, 124.97, 126.02, 126.41, 126.19, 125.93, 125.5199, 125.575, 125.6702, 125.471, 125.506, 125.41, 125.14, 125.18, 124.86, 124.86, 126.63, 127.01, 126.881, 126.86, 126.81, 126.86, 127.05, 126.945, 126.2946, 126.1511, 126.28, 125.54, 126.24, 126.22, 125.99, 126.41, 126.12, 126.05, 126.17, 126.13, 126.14, 126.43, 126.26, 126.35, 126.89, 126.645, 126.26, 126.26, 125.5, 125.12, 125.49, 125.86, 125.96, 126.28, 126.37, 126.35, 126.115, 126.1, 125.83, 126.42, 126.73, 126.73, 125.79, 125.32, 125.4, 124.98, 125.0601, 125, 124.84, 124.5, 124.6246, 124.56, 124.29, 124.78, 123.98, 123.95, 125.13, 125.24, 125.48, 125.84, 125.73, 125.76, 125.87, 126.02, 125.9, 126.09, 126.19, 126.21, 126.05, 126.05, 124.208, 123.96, 124.04, 123.9, 123.4, 123.48, 123.5046, 123.55, 123.5911, 123.59, 123.71, 123.825, 124.19, 124.21, 125.38, 125.0199, 124.64, 124.64, 124.85, 124.675, 124.79, 124.67, 124.79, 124.53, 123.86, 123.1, 123.05, 123.05, 122.42, 122.25, 121.81, 121.77, 121.66, 121.94, 122.1484, 122.5, 122.0654, 122.07, 121.67, 121.96, 121.71, 121.74, 123.06, 122.57, 122.21, 122.55, 122.31, 122.71, 122.47, 122.4773, 122.405, 122.31, 122.18, 122.37, 122.18, 122.185, 122.44, 122.68, 122.69, 122.51, 121.9, 121.74, 121.6, 121.78, 121.5999, 121.46, 121.67, 121.57, 121.59, 121.59, 122.05, 121.892, 121.47, 121.28, 120.85, 121.09, 121.15, 121.02, 121.24, 121.02, 121.07, 120.21, 120.32, 120.29, 122.99, 123.3, 123.35, 123.47, 123.4254, 123.469, 123.54, 123.72, 123.7, 123.72, 123.9, 123.97, 123.92, 123.93, 123.26, 123.49, 123.31, 123.13, 123.2, 122.85, 123.34, 123.34, 123.59, 123.57, 123.73, 124.03, 124.17, 124.17, 124.98, 124.97, 124.78, 124.8, 124.88, 125.25, 125.22, 125.3, 125.201, 125.3289, 125.1, 125.345, 125.4, 125.27, 125.66, 125.68, 125.73, 125.946, 126.04, 126.11, 126.2, 126.08, 126.11, 126.02, 126.05, 126.005, 126.41, 126.39, 126.77, 126.78, 126.39, 126.57, 126.55, 126.635, 126.58, 126.55, 126.72, 126.62, 126.7, 126.65, 126.49, 126.49, 125.74, 125.4, 125.39, 125.1, 125.05, 125.08, 124.94, 125.14, 125.29, 125.13, 124.99, 124.78, 124.88, 124.83, 125.42, 125.6991, 125.83, 125.64, 125.9505, 125.8001, 125.83, 125.8077, 125.88, 125.89, 125.86, 126, 126.12, 126.12, 126.1685, 126.08, 126.11, 126.22, 126.03, 126, 126.04, 126, 126.04, 125.78, 125.839, 125.94, 125.5, 125.5, 127.95, 128.19, 128.24, 128.18, 127.955, 127.937, 127.52, 127.56, 127.73, 127.85, 128.04, 127.78, 127.49, 127.495, 127.36, 126.9246,
Re: [R] How to sum and group data by DATE in data frame
Fascinating... dput() has never given me anything that looks like that I would have expected something much more like z <- structure(c(123.53, 123.78, 124.24, 124.2, 124.07, 123.91, 123.44, 123.0616, 123.06, 123.13, 123.745, 123.96, 123.99, 123.99, 124.3, 124.38, 124.67, 125.19, 124.9, 125.27, 125.5, 125.58, 125.91, 125.8, 125.83, 126.215, 126.25, 126.25, 124.901, 124.43, 124.4654, 124.46, 124.68, 124.86, 124.73, 125.22, 125.48, 125.5601, 125.4091, 125.15, 125.43, 125.481, 125.91, 125.29, 124.79, 124.77, 124.7, 124.37, 124.56, 124.86, 125.3, 125.59, 125.95, 125.73, 126.27, 126.26, 127.33, 126.37, 126.46, 126, 126.06, 126.2662, 126.23, 126.4499, 127.12, 127.48, 127.49, 127.69, 127.88, 127.88, 124.51, 124.42, 124.92, 125.18, 125.23, 124.81, 125.07, 124.61, 123.8869, 123.24, 123.3329, 123.6, 123.19, 123.161), index = structure(c(1320258600, 1320260400, 1320262200, 1320264000, 1320265800, 1320267600, 1320269400, 1320271200, 1320273000, 1320274800, 1320276600, 1320278400, 1320280200, 1320282000, 1320345000, 1320346800, 1320348600, 1320350400, 1320352200, 1320354000, 1320355800, 1320357600, 1320359400, 1320361200, 1320363000, 1320364800, 1320366600, 1320368400, 1320431400, 1320433200, 1320435000, 1320436800, 1320438600, 1320440400, 1320442200, 1320444000, 1320445800, 1320447600, 1320449400, 1320451200, 1320453000, 1320454800, 1320697800, 1320699600, 1320701400, 1320703200, 1320705000, 1320706800, 1320708600, 1320710400, 1320712200, 1320714000, 1320715800, 1320717600, 1320719400, 1320721200, 1320784200, 1320786000, 1320787800, 1320789600, 1320791400, 1320793200, 1320795000, 1320796800, 1320798600, 1320800400, 1320802200, 1320804000, 1320805800, 1320807600, 1320870600, 1320872400, 1320874200, 1320876000, 1320877800, 1320879600, 1320881400, 1320883200, 1320885000, 1320886800, 1320888600, 1320890400, 1320892200, 1320894000), class = c("POSIXct", "POSIXt"), tzone = ""), class = "zoo") which is about 100x more convenient With that, aggregate(z, as.Date(time(z)), sum) and aggregate(z, format(time(z), "%m %d"), sum) give different results (at least in my time zone) so try the latter (it seems to be what you were probably looking for) If that doesn't nail it down, I'll need you to answer the questions I asked in my previous email. Best, Michael On Wed, May 16, 2012 at 6:14 AM, Cren wrote: > > Michael Weylandt wrote >> >> Can you provide a reproducible example? >> > Of course, Michael. > > Consider the following time series: > > > 11/2/2011 14:30 123.53 > 11/2/2011 15:00 123.78 > 11/2/2011 15:30 124.24 > 11/2/2011 16:00 124.2 > 11/2/2011 16:30 124.07 > 11/2/2011 17:00 123.91 > 11/2/2011 17:30 123.44 > 11/2/2011 18:00 123.0616 > 11/2/2011 18:30 123.06 > 11/2/2011 19:00 123.13 > 11/2/2011 19:30 123.745 > 11/2/2011 20:00 123.96 > 11/2/2011 20:30 123.99 > 11/2/2011 21:00 123.99 > 11/3/2011 14:30 124.3 > 11/3/2011 15:00 124.38 > 11/3/2011 15:30 124.67 > 11/3/2011 16:00 125.19 > 11/3/2011 16:30 124.9 > 11/3/2011 17:00 125.27 > 11/3/2011 17:30 125.5 > 11/3/2011 18:00 125.58 > 11/3/2011 18:30 125.91 > 11/3/2011 19:00 125.8 > 11/3/2011 19:30 125.83 > 11/3/2011 20:00 126.215 > 11/3/2011 20:30 126.25 > 11/3/2011 21:00 126.25 > 11/4/2011 14:30 124.901 > 11/4/2011 15:00 124.43 > 11/4/2011 15:30 124.4654 > 11/4/2011 16:00 124.46 > 11/4/2011 16:30 124.68 > 11/4/2011 17:00 124.86 > 11/4/2011 17:30 124.73 > 11/4/2011 18:00 125.22 > 11/4/2011 18:30 125.48 > 11/4/2011 19:00 125.5601 > 11/4/2011 19:30 125.4091 > 11/4/2011 20:00 125.15 > 11/4/2011 20:30 125.43 > 11/4/2011 21:00 125.481 > 11/7/2011 15:30 125.91 > 11/7/2011 16:00 125.29 > 11/7/2011 16:30 124.79 > 11/7/2011 17:00 124.77 > 11/7/2011 17:30 124.7 > 11/7/2011 18:00 124.37 > 11/7/2011 18:30 124.56 > 11/7/2011 19:00 124.86 > 11/7/2011 19:30 125.3 > 11/7/2011 20:00 125.59 > 11/7/2011 20:30 125.95 > 11/7/2011 21:00 125.73 > 11/7/2011 21:30 126.27 > 11/7/2011 22:00 126.26 > 11/8/2011 15:30 127.33 > 11/8/2011 16:00 126.37 > 11/8/2011 16:30 126.46 > 11/8/2011 17:00 126 > 11/8/2011 17:30 126.06 > 11/8/2011 18:00 126.2662 > 11/8/2011 18:30 126.23 > 11/8/2011 19:00 126.4499 > 11/8/2011 19:30 127.12 > 11/8/2011 20:00 127.48 > 11/8/2011 20:30 127.49 > 11/8/2011 21:00 127.69 > 11/8/2011 21:30 127.88 > 11/8/2011 22:00 127.88 > 11/9/2011 15:30 124.51 > 11/9/2011 16:00 124.42 > 11/9/2011 16:30 124.92 > 11/9/2011 17:00 125.18 > 11/9/2011 17:30 125.23 > 11/9/2011 18:00 124.81 > 11/9/2011 18:30 125.07 > 11/9/2011 19:00 124.61 > 11/9/2011 19:30 123.8869 > 11/9/2011 20:00 123.24 > 11/9/2011 20:30 123.3329 > 11/9/2011 21:00 123.6 > 11/9/2011 21:30 123.19 > 11/9/2011 22:00 123.161 > > The rownames are datas plus hour, the data column is the time series' value. > > -- > View this message in context: > http://r.789695.n4.nabble.com/How-to-sum-and-group-data-by-DATE-in-data-frame-tp903708p4630228.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-he
Re: [R] How to sum and group data by DATE in data frame
Michael Weylandt wrote > > Can you provide a reproducible example? > Of course, Michael. Consider the following time series: 11/2/2011 14:30 123.53 11/2/2011 15:00 123.78 11/2/2011 15:30 124.24 11/2/2011 16:00 124.2 11/2/2011 16:30 124.07 11/2/2011 17:00 123.91 11/2/2011 17:30 123.44 11/2/2011 18:00 123.0616 11/2/2011 18:30 123.06 11/2/2011 19:00 123.13 11/2/2011 19:30 123.745 11/2/2011 20:00 123.96 11/2/2011 20:30 123.99 11/2/2011 21:00 123.99 11/3/2011 14:30 124.3 11/3/2011 15:00 124.38 11/3/2011 15:30 124.67 11/3/2011 16:00 125.19 11/3/2011 16:30 124.9 11/3/2011 17:00 125.27 11/3/2011 17:30 125.5 11/3/2011 18:00 125.58 11/3/2011 18:30 125.91 11/3/2011 19:00 125.8 11/3/2011 19:30 125.83 11/3/2011 20:00 126.215 11/3/2011 20:30 126.25 11/3/2011 21:00 126.25 11/4/2011 14:30 124.901 11/4/2011 15:00 124.43 11/4/2011 15:30 124.4654 11/4/2011 16:00 124.46 11/4/2011 16:30 124.68 11/4/2011 17:00 124.86 11/4/2011 17:30 124.73 11/4/2011 18:00 125.22 11/4/2011 18:30 125.48 11/4/2011 19:00 125.5601 11/4/2011 19:30 125.4091 11/4/2011 20:00 125.15 11/4/2011 20:30 125.43 11/4/2011 21:00 125.481 11/7/2011 15:30 125.91 11/7/2011 16:00 125.29 11/7/2011 16:30 124.79 11/7/2011 17:00 124.77 11/7/2011 17:30 124.7 11/7/2011 18:00 124.37 11/7/2011 18:30 124.56 11/7/2011 19:00 124.86 11/7/2011 19:30 125.3 11/7/2011 20:00 125.59 11/7/2011 20:30 125.95 11/7/2011 21:00 125.73 11/7/2011 21:30 126.27 11/7/2011 22:00 126.26 11/8/2011 15:30 127.33 11/8/2011 16:00 126.37 11/8/2011 16:30 126.46 11/8/2011 17:00 126 11/8/2011 17:30 126.06 11/8/2011 18:00 126.2662 11/8/2011 18:30 126.23 11/8/2011 19:00 126.4499 11/8/2011 19:30 127.12 11/8/2011 20:00 127.48 11/8/2011 20:30 127.49 11/8/2011 21:00 127.69 11/8/2011 21:30 127.88 11/8/2011 22:00 127.88 11/9/2011 15:30 124.51 11/9/2011 16:00 124.42 11/9/2011 16:30 124.92 11/9/2011 17:00 125.18 11/9/2011 17:30 125.23 11/9/2011 18:00 124.81 11/9/2011 18:30 125.07 11/9/2011 19:00 124.61 11/9/2011 19:30 123.8869 11/9/2011 20:00 123.24 11/9/2011 20:30 123.3329 11/9/2011 21:00 123.6 11/9/2011 21:30 123.19 11/9/2011 22:00 123.161 The rownames are datas plus hour, the data column is the time series' value. -- View this message in context: http://r.789695.n4.nabble.com/How-to-sum-and-group-data-by-DATE-in-data-frame-tp903708p4630228.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to sum and group data by DATE in data frame
Can you provide a reproducible example? use dput() to send your data in a reproducible and user-friendly format. (It might not look user friendly, but trust me, it is) If x is really long, perhaps just dput(head(x, 25)) will suffice. Just a guess as to your problem: I'm not familiar with the timeSequence function -- what package is it from? My guess is that the days it's producing are not properly aligned with the days of your time series object. Can you try it with something closer to my example? Best, Michael On Tue, May 15, 2012 at 12:42 PM, Cren wrote: > Thank you for your help, Michael. > > I used *aggregate(x, by = timeSequence(by = "day"), FUN = sum)* but the > results is very different from *sum(x[1:13])*, where 13 is the number of > daily observations I've sampled. > > Michael Weylandt wrote >> >> How are you using aggregate()? It seems to sum for me... >> >> z <- zoo(1:50, seq.POSIXt(from = Sys.time(), by = "30 min", length.out = >> 50)) >> >> aggregate(z, as.Date(time(z)), sum) >> >> Best, >> Michael >> >> On Tue, May 15, 2012 at 11:52 AM, Crenwrote: >>> Hello, >>> >>> I have a time series with intraday datas, sampled every 30'; I would need >>> to >>> aggregate them in this way: summing up all datas within a day. >>> >>> I tried to use *aggregate(...)* function to get my goal, but it >>> aggregates >>> in wrong way (I did not understand how so far); what I need is like >>> *sum(...)* function applied to datas within the same day. >>> >>> Thank you :) >>> >>> -- >>> View this message in context: >>> http://r.789695.n4.nabble.com/How-to-sum-and-group-data-by-DATE-in-data-frame-tp903708p4630119.html >>> Sent from the R help mailing list archive at Nabble.com. >>> >>> __ >>> R-help@ mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >> >> __ >> R-help@ mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > > -- > View this message in context: > http://r.789695.n4.nabble.com/How-to-sum-and-group-data-by-DATE-in-data-frame-tp903708p4630139.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to sum and group data by DATE in data frame
Thank you for your help, Michael. I used *aggregate(x, by = timeSequence(by = "day"), FUN = sum)* but the results is very different from *sum(x[1:13])*, where 13 is the number of daily observations I've sampled. Michael Weylandt wrote > > How are you using aggregate()? It seems to sum for me... > > z <- zoo(1:50, seq.POSIXt(from = Sys.time(), by = "30 min", length.out = > 50)) > > aggregate(z, as.Date(time(z)), sum) > > Best, > Michael > > On Tue, May 15, 2012 at 11:52 AM, Crenwrote: >> Hello, >> >> I have a time series with intraday datas, sampled every 30'; I would need >> to >> aggregate them in this way: summing up all datas within a day. >> >> I tried to use *aggregate(...)* function to get my goal, but it >> aggregates >> in wrong way (I did not understand how so far); what I need is like >> *sum(...)* function applied to datas within the same day. >> >> Thank you :) >> >> -- >> View this message in context: >> http://r.789695.n4.nabble.com/How-to-sum-and-group-data-by-DATE-in-data-frame-tp903708p4630119.html >> Sent from the R help mailing list archive at Nabble.com. >> >> __ >> R-help@ mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > __ > R-help@ mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- View this message in context: http://r.789695.n4.nabble.com/How-to-sum-and-group-data-by-DATE-in-data-frame-tp903708p4630139.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to sum and group data by DATE in data frame
How are you using aggregate()? It seems to sum for me... z <- zoo(1:50, seq.POSIXt(from = Sys.time(), by = "30 min", length.out = 50)) aggregate(z, as.Date(time(z)), sum) Best, Michael On Tue, May 15, 2012 at 11:52 AM, Cren wrote: > Hello, > > I have a time series with intraday datas, sampled every 30'; I would need to > aggregate them in this way: summing up all datas within a day. > > I tried to use *aggregate(...)* function to get my goal, but it aggregates > in wrong way (I did not understand how so far); what I need is like > *sum(...)* function applied to datas within the same day. > > Thank you :) > > -- > View this message in context: > http://r.789695.n4.nabble.com/How-to-sum-and-group-data-by-DATE-in-data-frame-tp903708p4630119.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to sum and group data by DATE in data frame
Hello, I have a time series with intraday datas, sampled every 30'; I would need to aggregate them in this way: summing up all datas within a day. I tried to use *aggregate(...)* function to get my goal, but it aggregates in wrong way (I did not understand how so far); what I need is like *sum(...)* function applied to datas within the same day. Thank you :) -- View this message in context: http://r.789695.n4.nabble.com/How-to-sum-and-group-data-by-DATE-in-data-frame-tp903708p4630119.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to sum and group data by DATE in data frame
Try this: rowsum(Income, format(Date, '%m-%Y')) or tapply(Income, format(Date, '%m-%Y'), sum) On Wed, Sep 9, 2009 at 7:51 AM, clair.crossup...@googlemail.com < clair.crossup...@googlemail.com> wrote: > Dear all, > > Lets say I have a data frame as follows: > > > > Date <- as.Date(c('2006-08-23', '2006-08-30', '2006-09-06', '2006-09-13', > '2006-09-20')) > > Income <- c(73.79, 72.46, 76.32, 72.43, 72.62) > > data.frame(Date, Income) >Date Income > 1 2006-08-23 73.79 > 2 2006-08-30 72.46 > 3 2006-09-06 76.32 > 4 2006-09-13 72.43 > 5 2006-09-20 72.62 > > > > is there a way to group the data by month (summing the values in each > month), i.e. > > Date Income > 2006-08 146.25 > 2006-09 221.37 > > Thanks in advance, > C.C. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to sum and group data by DATE in data frame
Hi Clair, try to use this code aggregate(toto$Income,by=list((substr(toto$Date,1,7))),sum) Regards mohamed clair.crossup...@googlemail.com a écrit : Dear all, Lets say I have a data frame as follows: Date <- as.Date(c('2006-08-23', '2006-08-30', '2006-09-06', '2006-09-13', '2006-09-20')) Income <- c(73.79, 72.46, 76.32, 72.43, 72.62) data.frame(Date, Income) Date Income 1 2006-08-23 73.79 2 2006-08-30 72.46 3 2006-09-06 76.32 4 2006-09-13 72.43 5 2006-09-20 72.62 is there a way to group the data by month (summing the values in each month), i.e. Date Income 2006-08 146.25 2006-09 221.37 Thanks in advance, C.C. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Mohamed Lajnef INSERM Unité 955. 40 rue de Mesly. 94000 Créteil. Courriel : mohamed.laj...@inserm.fr tel. : 01 49 81 31 31 (poste 18470) Sec : 01 49 81 32 90 fax : 01 49 81 30 99 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to sum and group data by DATE in data frame
Dear all, Lets say I have a data frame as follows: > Date <- as.Date(c('2006-08-23', '2006-08-30', '2006-09-06', '2006-09-13', > '2006-09-20')) > Income <- c(73.79, 72.46, 76.32, 72.43, 72.62) > data.frame(Date, Income) Date Income 1 2006-08-23 73.79 2 2006-08-30 72.46 3 2006-09-06 76.32 4 2006-09-13 72.43 5 2006-09-20 72.62 > is there a way to group the data by month (summing the values in each month), i.e. Date Income 2006-08 146.25 2006-09 221.37 Thanks in advance, C.C. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.