Subset 将不规则数据分为最近的时间间隔/带缓冲区的子集

Subset 将不规则数据分为最近的时间间隔/带缓冲区的子集,subset,Subset,我有在不同取样制度下收集的数据,从每三秒到每小时 我希望以最接近10分钟的固定间隔(也可以是其他时间间隔,但我假设一旦我掌握了正确的代码,就可以很容易地更改间隔)获取此数据的子样本 我在这里看到过类似的帖子(例如:),但没有答案可以考虑缓冲区或“最接近”的想法 问题是: 数据没有定期抽样。所以我不能要求R每隔十分钟给我一次所有数据,但我需要尽可能靠近每十分钟的“” 我还想缓冲这段时间,这样间隔就不会少于9分钟 当前我的日期时间列被读取为“因子”: 类别(示例$日期\时间) [1] “因素”

我有在不同取样制度下收集的数据,从每三秒到每小时

我希望以最接近10分钟的固定间隔(也可以是其他时间间隔,但我假设一旦我掌握了正确的代码,就可以很容易地更改间隔)获取此数据的子样本

我在这里看到过类似的帖子(例如:),但没有答案可以考虑缓冲区或“最接近”的想法

问题是:

  • 数据没有定期抽样。所以我不能要求R每隔十分钟给我一次所有数据,但我需要尽可能靠近每十分钟的“
  • 我还想缓冲这段时间,这样间隔就不会少于9分钟
  • 当前我的日期时间列被读取为“因子”: 类别(示例$日期\时间) [1] “因素”

样本数据:

设备\u无日期\u时间纬度经度温度 1 23/04/2012 15:57:22 -33.2415715 19.4810864 27.7 1 23/04/2012 15:58:08 -33.2415396 19.4810666 27.7 1 23/04/2012 15:58:58 -33.2415963 19.48109 27.2 1 23/04/2012 15:59:46 -33.2415137 19.4810624 27.2 1 23/04/2012 16:00:33 -33.2415019 19.4810885 27 1 23/04/2012 16:01:21 -33.241561 19.4810867 26.3 1 23/04/2012 16:02:10 -33.2415579 19.4810926 26.4 1 23/04/2012 16:02:57 -33.2414687 19.4810465 25.6 1 23/04/2012 16:03:45 -33.2415096 19.4810736 24.6 1 23/04/2012 16:05:20 -33.2415707 19.4810614 24.8 1 23/04/2012 16:06:22 -33.2415188 19.4810708 24.6 1 23/04/2012 16:07:12 -33.2415754 19.4810538 24.6 1 23/04/2012 16:08:00 -33.2415054 19.4810874 24.2 1 23/04/2012 16:08:48 -33.2414794 19.4810908 24.3 1 23/04/2012 16:09:36 -33.2415538 19.4810802 24 1 23/04/2012 16:10:25 -33.2413946 19.4811353 23.4 1 23/04/2012 16:11:14 -33.2414529 19.4811084 23.7 1 23/04/2012 16:12:01 -33.2413949 19.4810978 23.5 1 24/04/2012 05:26:39 -33.2415605 19.4810589 23.1 1 24/04/2012 05:56:51 -33.2414826 19.4811049 23 1 24/04/2012 06:01:22 -33.2415975 19.4810535 23 1 24/04/2012 06:02:09 -33.2384224 19.482825 21.7 1 24/04/2012 06:02:58 -33.2380158 19.4833283 20.6 1 24/04/2012 06:03:45 -33.241538 19.4810951 19.9 1 24/04/2012 06:04:34 -33.2416337 19.4810291 19.3 1 24/04/2012 06:05:22 -33.2410841 19.4819002 19.9 1 24/04/2012 06:06:11 -33.2401989 19.4817907 19.9 1 24/04/2012 06:06:57 -33.241593 19.4810426 19.4 1 24/04/2012 06:07:46 -33.241575 19.4810011 18.6 1 24/04/2012 06:08:34 -33.2415497 19.4810493 18.8 1 24/04/2012 06:09:22 -33.2415104 19.4810216 18.4 1 24/04/2012 06:10:11 -33.2416627 19.4810065 18 1 24/04/2012 06:10:59 -33.2414893 19.4811548 18.2 1 24/04/2012 06:11:44 -33.2420604 19.4810295 18.7 1 24/04/2012 06:12:33 -33.2408584 19.4803685 35.5 1 24/04/2012 06:13:20 -33.2407331 19.4805824 38.3 1 24/04/2012 06:25:58 -33.2411718 19.4810405 39.9 1 24/04/2012 06:26:49 -33.2415396 19.4810794 41.4 1 24/04/2012 06:27:56 -33.2415588 19.481089 40 1 24/04/2012 06:28:54 -33.2415257 19.4810381 41 1 24/04/2012 06:29:42 -33.239857 19.4807259 37.6 2012年4月24日06:30:29-33.2409401 19.480927 36.4

样本结果:

设备\u无日期\u时间纬度经度温度 1 23/04/2012 15:57:22 -33.2415715 19.4810864 27.7 1 23/04/2012 16:07:12 -33.2415754 19.4810538 24.6 1 24/04/2012 05:26:39 -33.2415605 19.4810589 23.1 1 24/04/2012 05:56:51 -33.2414826 19.4811049 23 1 24/04/2012 06:06:57 -33.241593 19.4810426 19.4 2012年4月24日06:25:58-33.2411718 19.4810405 39.9`

很抱歉,我的屏幕截图无法发布,并且此数据未显示为表…

#创建一些数据
# Create some data

# start at Jan 1, 2014
start=as.POSIXct("1-Jan-2014 00:00",format="%d-%b-%Y %H:%M")

# end at Jan 31, 2014
end=as.POSIXct("31-Jan-2014 23:00",format="%d-%b-%Y %H:%M")

# sequence the time from start to end and create a continuous time 
# set with observations every 5 minutes with a sd of 1 minute
time=seq.POSIXt(start,end,by=rnorm(1,1*60*5,60))

# create a loop

# you want the buffer for time > 9 minutes
time.diff=9
new.time=time[1]
# seq 10 minute intervals (this is the data we are going to try and match)
time.10min=seq.POSIXt(start,end,by=1*60*10)

# skip the first observation
for(i in 2:length(time.10min)){
  # Look for where the minimum difference occurs in your original dataset
  obs=time[which(min(abs(time.10min[i]-time))==abs(time.10min[i]-time))]

  # see if it meets you buffer requirements. If it doesn't meet your buffer requirements
  # it will take the next observation in your dataset
  if(obs-new.time[i-1]<time.diff){
    obs=time[which(min(abs(time.10min[i]-time))==abs(time.10min[i]-time))+1]
    new.time[length(new.time)+1]=obs
  } else {
    new.time[length(new.time)+1]=obs
  }
}

head(new.time)


# Good luck and hopefully this helps.
#2014年1月1日开始 start=as.POSIXct(“2014年1月1日00:00”,格式=“%d-%b-%Y%H:%M”) #截止2014年1月31日 end=as.POSIXct(“2014年1月31日23:00”,格式=“%d-%b-%Y%H:%M”) #从开始到结束对时间进行排序,并创建一个连续的时间 #每5分钟进行一次观察,sd为1分钟 时间=顺序位置(开始、结束、结束=形式(1,1*60*5,60)) #创建一个循环 #您希望缓冲时间>9分钟 时间差=9 新建。时间=时间[1] #seq 10分钟间隔(这是我们将尝试匹配的数据) 10分钟时间=顺序位置(开始、结束、结束时间=1*60*10) #跳过第一个观察 适用于(i/2:长度(时间10分钟)){ #查找原始数据集中出现最小差异的位置 obs=时间[其中(分钟(abs(time.10min[i]-时间))==abs(time.10min[i]-时间))] #看看它是否满足您的缓冲区要求。如果它不满足您的缓冲区要求 #它将在您的数据集中进行下一次观察 if(obs新时间[i-1]