Skip to content
Prev 259401 / 398502 Next

Averaging uneven measurements by time with uneven numbers of measurements

An easy way to average every 30 minutes is using the aggregate function. First I use the mod (%%) to get times as the next earliest possible 30 min time increment. 

bTime<-  as.POSIXct(c( "2011-04-28 09:02:00 CDT","2011-04-28 09:02:00 CDT","2011-04-28 09:12:00 CDT","2011-04-28 09:14:00 CDT","2011-04-28 09:15:00 CDT","2011-04-28 09:23:00 CDT","2011-04-28 09:45:00 CDT","2011-04-28 09:53:00 CDT","2011-04-28 10:03:00 CDT"))
numTime<-as.numeric(bTime)-21600
Weight<-runif(9)*100
minT<-30
excess<-numTime %% (minT*60)
nTime<-numTime-excess
nDateT<-as.POSIXct(nTime, origin="1970-01-01",tz= "US/Central")
meanw <- aggregate(Weight, list(nDateT), mean, na.rm = TRUE)



-----Original Message-----
From: clint at ecy.wa.gov [mailto:clint at ecy.wa.gov] 
Sent: Thursday, May 05, 2011 03:58 PM
To: Thompson, Adele - Adele_Thompson at cargill.com
Cc: r-help at r-project.org
Subject: Re: [R] Averaging uneven measurements by time with uneven numbers of measurements

In your first request for help you said, "How do I average the 
measurements by every 30 minutes?"  With 25000 readings over three 
days, it looks as if you are getting readings just about every 
second.

Okay, why don't you use the first reading as your initial weight, 
w0.  Then subtract each succeeding reading from that to obtain the 
amount of feed dispensed.  Now plot that value every 30 minutes.

If you are interested in the variation of feed dispensed over a 
half hour interval, that can be easily obtained by accummulating 
those half-hour readings.