Stack Exchange
time binning and calculating weighted means
I have a data frame that looks as follows (8 columns - the myPOSIX column is in 'y-m-d h:s' format)
head(new)
Date.and.Time..UTC.    Receiver    Transmitter Latitude Longitude ndiffs29912  flag             
1    07/10/2010 15:53 VR2W-107619 A69-1303-29912 48.56225 -53.89144          NA FALSE 2010-10-07 
2    07/10/2010 15:56 VR2W-107619 A69-1303-29912 48.56225 -53.89144         180 FALSE 2010-10-07 
3    07/10/2010 16:00 VR2W-107619 A69-1303-29912 48.56225 -53.89144         240 FALSE 2010-10-07 
4    07/10/2010 16:24 VR2W-107619 A69-1303-29912 48.56225 -53.89144        1440 FALSE 2010-10-07 
5    07/10/2010 16:45 VR2W-104556 A69-1303-29912 48.56460 -53.88956        1260 FALSE 2010-10-07 
6    07/10/2010 16:47 VR2W-107619 A69-1303-29912 48.56225 -53.89144         120 FALSE 2010-10-07  
myPOSIX
15:53:00
15:56:00
16:00:00
16:24:00
16:45:00
16:47:00

My goal is to bin the detections into hour time bins. Then, for each time bin, I would like to calculate weighted means for latitude and longitude, using the number of detections per receiver as the weighting measure (ie the frequency of the different receiver names in each bin).
Any insight would be greatly appreciated - I've been trying to work out a code using the zoo and xts package for aggregating hourly detections, but have not succeeded.

This part isn't clear: "using the number of detections per receiver as the weighting measure (ie the frequency of the different receiver names in each bin)". When weighting, you supply one weight per observation. The way it sounds right now is that you really just want means (unweighted) that are calculated by hour, receiver, lat (or long).

# Dummy data
x <- data.frame(
  date=as.POSIXct(1:10000,origin="2005-01-01 12:00:00"),
receiver=rep(letters[1:10],1000),
  Latitude=rep(letters[1:10],1000),
  Longitude=rep(letters[1:10],1000),
  ndiffs29912=rnorm(10000)+10)

# Break dates into hours using cut append to data frame
x$hour <- cut(x$date, breaks="hour")

library(plyr) 
# By hour and latitude, get weighted
# mean of ndiffs based on number of receivers
ddply(x, .(hour, latitude), function(x) data.frame(
weighted.mean(x$ndiffs29912, length(x$receiver), na.rm=T))

# By hour and longitude, get weighted
# mean of ndiffs based on number of receivers
ddply(x, .(hour, longitude), function(x) data.frame(
weighted.mean(x$ndiffs29912, length(x$receiver), na.rm=T))


Thanks for the quick reponse. I am trying to understand your approach, but I have a question. For Ri receivers, does the length(x$receiver) argument weight the mean using the sum of the number of detections at the i-th receiver?

ddply breaks your data into sets by hour AND longitude (or latitidue). It's essentially programatic shorthand for subset(x, hour == A & longitude == B) where A and B are all possible combinations of hour and long/lat. If you need a weighted mean by reciever as well than the code would change to ddply(x, .(hour, receiver, longitude) ...)

Thank you for taking the time to help me Brandon, I really appreciate it. Ok, that makes sense. But what I am really trying to do is calculate a weigthed mean of the latitude positions for each hour subset, using the number of detections per receiver as the weight. If I modify your code as shown below, I get an error message reading that x and w are of different lengths. My receiver column and latitude column are the same length though, so I do not understand. Do I have to add in a function that counts the frequency of the different receiver types in each hour timebin for the weights?

ddply(t.29912, .(hourbins,Receiver), function(t.29912) data.frame(
weighted.mean(t.29912$Latitude, length(t.29912$Receiver))))

I am trying to follow this formula to calculate the weighted means: for i receivers over delta-t of one hour, sum ((# of detections at the i-th receiver)*Latitude of the i-th receiver)/sum(#of detectons at the i-th receiver)

ddply(t.29912, .(hourbins,Receiver), function(x) data.frame(weighted.mean(t.29912$Latitude,length(t.29912$Receiver))))

Thanks Brandon, I ended up using table() to calculate the frequency of detections per receiver for each bin, and then used ddply to calculate the weighted mean using those frequencies. ddply sure is a helpful tool!
