我想识别并标记我的GPS项圈使用日期时间列切换修复率时间表。
我的项圈在5分钟的时间表上交替持续2天,然后每小时交替持续6天,持续几个月然后关闭。理想情况下,我只会使用原始时间表,但是项圈并不总是立即响应时间表的变化。
在过去,我计算了一个时差列,使用datetime [{datetime2-datetime1)* 24 * 60]的数字版本隔离5分钟的连续位置,但这会导致失去"错过修复&#34 ;数据。现在我想要包含这些数据,将所有数据标记为5分钟数据,以及在每小时数据时将标记标记。
示例(包括我需要的列):
> dput(example)
structure(list(OBJECTID = c(1L, 2L, 3L, 4L, 5L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L,
43L, 44L, 45L, 46L), DT_LMT = structure(c(1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 11L, 12L, 13L, 23L, 24L, 25L), .Label = c("28/05/2013 1:00",
"28/05/2013 1:05", "28/05/2013 1:10", "28/05/2013 1:15", "28/05/2013 1:20",
"28/05/2013 1:35", "28/05/2013 1:40", "28/05/2013 1:45", "28/05/2013 1:50",
"28/05/2013 1:55", "28/05/2013 10:00", "28/05/2013 11:00", "28/05/2013 12:00",
"28/05/2013 2:00", "28/05/2013 2:05", "28/05/2013 2:10", "28/05/2013 3:50",
"28/05/2013 3:55", "28/05/2013 4:00", "28/05/2013 5:00", "28/05/2013 6:00",
"28/05/2013 7:00", "29/05/2013 12:00", "30/05/2013 12:00", "31/05/2013 12:00"
), class = "factor"), DT_LMT.1 = c(41422.0416666667, 41422.0451388889,
41422.0486111111, 41422.0520833333, 41422.0555555556, 41422.0659722222,
41422.0694444444, 41422.0729166667, 41422.0763888889, 41422.0798611111,
41422.0833333333, 41422.0868055556, 41422.0902777778, 41422.1597222222,
41422.1631944444, 41422.1666666667, 41422.2083333333, 41422.25,
41422.2916666667, 41422.4166666667, 41422.4583333333, 41422.5,
41423.5, 41424.5, 41425.5), TIMEDIFF = c(5L, 5L, 5L, 5L, 15L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 100L, 5L, 5L, 60L, 60L, 60L, 180L,
60L, 60L, 1440L, 1440L, 1440L, NA), FIVE = c(1L, 1L, 1L, 1L,
0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, NA), HOUR = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L,
NA), NEED = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 1L), .Label = c("",
"FIVE", "HOUR", "OFF"), class = "factor")), .Names = c("OBJECTID",
"DT_LMT", "DT_LMT.1", "TIMEDIFF", "FIVE", "HOUR", "NEED"), class = "data.frame", row.names = c(NA,
-25L))
在一些帮助下,这是我到目前为止所做的,除非它最终被绊倒:
data<-data[-25,]
c=1
DTD<-1
for (i in 1:24){
if(data$TIMEDIFF[i]<=25){
data[i,9]<-5}
else{
j=1
DTD<-1
while (j < 100){
if (data$DT_LMT.1[i+j]<=data$DT_LMT.1[5]+4){
if (data$TIMEDIFF[i+j]<=25){
DTD<-0
j=101
print (j)}
else{j=j+1}
}
else{j=101}
}
if (DTD==0){data[i,9]<-5}
else{data[i,9]<-0}
}
}
data
答案 0 :(得分:0)
对于想要做这类事情的人来说,这似乎有用:
min.days<-3
summerexample<-summerexample[,-60]
summerexample[,60]<-5
invalid.time.diffs<-which(summerexample$TIMEDIFF>25)#the indices of potential scheduale changes
i<-1
while (i<(length(invalid.time.diffs))){
#check for a single min.days gap
if ((summerexample$DT_LMT[invalid.time.diffs[i]]-summerexample$DT_LMT[invalid.time.diffs[i]-1])>3){
summerexample[invalid.time.diffs[i],60]<-0
i<-i+1
}
else{
#j is the number of consecutive invalid time differences
j<-1
while (((i+j)<length(invalid.time.diffs))&
((invalid.time.diffs[i+j]-invalid.time.diffs[i])==j)){
j<-j+1
}
#if the last consecutive invalid time difference is more than min.days after the first one
if ((summerexample$DT_LMT[invalid.time.diffs[i+(j-1)]]-summerexample$DT_LMT[invalid.time.diffs[i]])>3){
summerexample[invalid.time.diffs[i:(i+(j-1))],60]<-0
i<-i+j
}
else {
i<-i+1
}
}
}