Reputation: 863
How would I find the averages of active observations for each day in six month period. The data looks like this:
output start end
12000 04/01/2012 04/01/2012
2175 11/25/2011 01/22/2012
1800 12/06/2011 01/23/2012
1795 03/23/2012 03/23/2012
1550 04/03/2012 04/20/2012
3400 10/13/2011 01/24/2012
1850 02/27/2012 03/08/2012
2500 11/08/2011 01/24/2012
5350 02/02/2012 04/01/2012
2550 02/09/2012 04/09/2012
8000 11/01/2011 01/22/2012
2725 12/02/2011 01/22/2012
6249 01/12/2012 01/24/2012
1875 01/22/2012 02/28/2012
2550 01/18/2012 01/24/2012
2650 01/28/2012 01/29/2012
2100 11/25/2011 02/21/2012
6900 01/17/2012 02/17/2012
1779 04/21/2012 04/21/2012
4900 11/09/2011 01/22/2012
3250 12/09/2011 01/24/2012
2995 03/14/2012 04/18/2012
So for each day within the six months, I want to find the the average of all active observations on that day. For example, the second observation above would be included in the with all other active observations to find the averages on the days between 11/25/2011 and 01/22/2012.
any insights on how to pull it off
Upvotes: 0
Views: 65
Reputation: 48211
start <- as.Date(data$start, "%m/%d/%Y")
end <- as.Date(data$end, "%m/%d/%Y")
period <- as.Date(min(start):max(end), origin = "1970-01-01")
averages <- sapply(period, function(x) mean(data[x >= start & x <= end, 'output']))
data.frame(period, averages)
Upvotes: 1