2

我有一个如下所示的数据框(8 列 - myPOSIX 列采用 'ymd h:s' 格式)

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

我的目标是将检测分箱到小时时间箱中。然后,对于每个时间箱,我想计算纬度和经度的加权平均值,使用每个接收器的检测次数作为加权度量(即每个箱中不同接收器名称的频率)。任何见解都将不胜感激 - 我一直在尝试使用 zoo 和 xts 包来计算代码以聚合每小时检测,但没有成功。

4

1 回答 1

1
# 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))
于 2012-02-19T20:05:00.160 回答