观察在特定时间范围内是否唯一

时间:2013-01-10 17:55:55

标签: r datetime if-statement

我正在尝试清理R

中的数据集(下面的子示例)

解释通过if函数解释的最好方法:

如果天线= 1,是否在此时间戳之前的5分钟内有另一个观察?如果是这样给予真/假

但是我不确定如何解决这个问题

               Date.Time Aerial
794  2012-10-01 08:18:00      1
795  2012-10-01 08:34:00      1
796  2012-10-01 08:39:00      1
797  2012-10-01 08:42:00      1
798  2012-10-01 08:48:00      1
799  2012-10-01 08:54:00      1
800  2012-10-01 08:58:00      1
801  2012-10-01 09:04:00      1
802  2012-10-01 09:05:00      1
803  2012-10-01 09:11:00      1
1576 2012-10-01 09:17:00      2
1577 2012-10-01 09:18:00      2
804  2012-10-01 09:19:00      1
805  2012-10-01 09:20:00      1
1580 2012-10-01 09:21:00      2
1581 2012-10-01 09:23:00      2
806  2012-10-01 09:25:00      1
807  2012-10-01 09:32:00      1
808  2012-10-01 09:37:00      1
809  2012-10-01 09:43:00      1

e.g。在09:19天线= 1,在此之前的5分钟内有一个观察在09:18和09:17所以我想在09:19取消观察。这是大数据集,因此可能会发生多次

道歉,如果这不是正确的方式,要求相对较新的R。

我的想法:

使用if.else语句,但是我无法获得使用它的日期时间。

没有代码,因为在上面询问上面之前已经试图进行此操作

3 个答案:

答案 0 :(得分:0)

diff会为您提供特定数据列的“运行差异”。如果您在diff上运行Date.Time(或as.POSIXct(Date.Time),如果它尚未采用该格式),它会告诉您每个连续间隔之间的差异。那么看看

的结果
diff(DataFrame$Date.Time) #or
diff(as.POSIXct(DataFrame$Date.Time)) #if the first one doesn't work

如果你可以使用一个软件包并且离开base函数{@ 1}},就像@AriBFriedman所提到的那样,可以让rollapply更进一步,并应用基于函数的函数关于滚动值(它是diff包的一部分)。

答案 1 :(得分:0)

 with( dfrm, Aerial == 1 & c(diff(Date.Time),0) > 5 )
 [1]  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE
[12] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE

这实际上只是处理差异的一个方向,无论如何你是否想要包括两端的项目是一个悬而未决的问题。如果您想在两个方向上执行此操作,则可能会正确设置处理&的其他逻辑rev( diff (rev (Date.Time) ) )子句。我承认我想知道diff.POSIXt正在重新开始几分钟。帮助页面没有帮助,测试显示它只需几分钟。

获得向后差异的另一种方法可能是测试diff-vector以另一种方式移动:

with( dfrm,  c( FALSE, abs(diff(Date.Time)) > 5 ) )

答案 2 :(得分:0)

d<-read.table(text='Date.Time Aerial
794  "2012-10-01 08:18:00"      1
795  "2012-10-01 08:34:00"      1
796  "2012-10-01 08:39:00"      1
797  "2012-10-01 08:42:00"      1
798  "2012-10-01 08:48:00"      1
799  "2012-10-01 08:54:00"      1
800  "2012-10-01 08:58:00"      1
801  "2012-10-01 09:04:00"      1
802  "2012-10-01 09:05:00"      1
803  "2012-10-01 09:11:00"      1
1576 "2012-10-01 09:17:00"      2
1577 "2012-10-01 09:18:00"      2
804  "2012-10-01 09:19:00"      1
805  "2012-10-01 09:20:00"      1
1580 "2012-10-01 09:21:00"      2
1581 "2012-10-01 09:23:00"      2
806  "2012-10-01 09:25:00"      1
807  "2012-10-01 09:32:00"      1
808  "2012-10-01 09:37:00"      1
809  "2012-10-01 09:43:00"      1', header=TRUE, stringsAsFactors=FALSE, row.names=1)

# convert Date.Time to POSIXct
d<-within(d, Date.Time<-as.POSIXct(Date.Time))


# define row aggregator 
f <- function(accumulation, next.row, min.mins=5) {
    last.dtime <- tail(accumulation,1)$Date.Time
    next.dtime <- next.row$Date.Time
    # don't add next.row if Aerial is 1 and time between last record is less than min.mins
    if (next.row$Aerial == 1 & (as.numeric(next.dtime - last.dtime, units='mins') < min.mins))
        accumulation
    else
        rbind(accumulation, next.row)
}

# aggregate rows
Reduce(f, split(d[order(d$Date.Time), ], sequence(nrow(d))))

#                Date.Time Aerial
# 794  2012-10-01 08:18:00      1
# 795  2012-10-01 08:34:00      1
# 796  2012-10-01 08:39:00      1
# 798  2012-10-01 08:48:00      1
# 799  2012-10-01 08:54:00      1
# 801  2012-10-01 09:04:00      1
# 803  2012-10-01 09:11:00      1
# 1576 2012-10-01 09:17:00      2
# 1581 2012-10-01 09:23:00      2
# 807  2012-10-01 09:32:00      1
# 808  2012-10-01 09:37:00      1
# 809  2012-10-01 09:43:00      1
相关问题