我有大文件,我需要计算不同记录的时差。为了说明,MWE提供了
数据数据框df:
st time from to type size flg fid src dst no ID
+ 0.163944 2 1 a 40 ------- 1 2.4 5.4 0 10
+ 0.215400 2 1 a 40 ------- 1 2.4 5.4 1 28
+ 0.239528 2 1 t 40 ------- 1 2.4 5.4 0 37
+ 0.287784 2 1 t 1040 ------- 1 2.4 5.4 1 62
+ 0.287784 2 1 t 1040 ------- 1 2.4 5.4 2 63
.......... . . ... .. ....... . . .. . ..
# here should be some more lines with different value such as
- 0.487784 3 0 t 1040 ------- 4 2.8 7.4 2 23
# the above line will be filtered out by the conditions-just ignore it
.......... . . ... .. ....... . . .. . ..
r 0.188072 0 5 a 40 ------- 1 2.4 5.4 0 10
r 0.239528 0 5 a 40 ------- 1 2.4 5.4 1 28
r 0.263656 0 5 t 40 ------- 1 2.4 5.4 0 37
r 0.317128 0 5 t 1040 ------- 1 2.4 5.4 1 62
r 0.318792 0 5 t 1040 ------- 1 2.4 5.4 2 63
条件1:对于每条记录以“+”开头,“ID”将是唯一的。 “src”,“dst”和“from”被添加到条件中。根据该信息,“时间”字段将被记录为数组中的开始(即数组[ID] =时间)。
条件2:对于每条记录以“r”开头,将检查“ID”。根据此信息,所需的时间差将为:当前“时间” - 数组[ID]。
我创建了R代码并且它有效。但是,我使用的是固定的src和dst值。 src的格式:x.y,其中x总是= 2,y正在变化(即y = 0,1,2,3,4,.......)。另外,dst:z.f,其中z和f正在变化(即可能是4.3,5.2,6.100 ....)
R代码:
src<-"2.4" # this value should be automated like 2.y. Any suggestions !!!
dst<-"5.4" # this value should be automated like z.f
ReqTime<-0
timeHolder<-c()
#start
start<-df[df[, "st"] == "+" &
df[, "from"] == 2 &
# the src and dst should be automated
df[, "src"] == src &
df[, "dst"] == dst,]
timeHolder[start$ID]<-start$time
#end
end<-df[df[, "st"] == "r" &
df[, "from"] == 0 &
df[, "src"] == src &
df[, "dst"] == dst,]
if(!is.null(timeHolder[end$ID])){
ReqTime<- end$time- timeHolder[end$pktID]
}
cat("Time from ",src,"--",dst,": ",ReqTime,"\n")
}
预期产出:
Time from 2.4 -- 5.4 : 0.024128 0.024128 0.024128 0.029344 0.031008
或非常感谢,如果我能得到如下输出:
Time from 2.4 -- 5.4 : mean( 0.024128 0.024128 0.024128 0.029344 0.031008) which is =0.0265472
答案 0 :(得分:1)
如果我理解了您想要的内容,您可以aggregate
您的数据:
#your data plus some extra
DF <- read.table(text = 'st time from to type size flg fid src dst no ID
+ 0.163944 2 1 a 40 ------- 1 2.4 5.4 0 10
+ 0.215400 2 1 a 40 ------- 1 2.4 5.4 1 28
+ 0.239528 2 1 t 40 ------- 1 2.4 5.4 0 37
+ 0.287784 2 1 t 1040 ------- 1 2.4 5.4 1 62
+ 0.287784 2 1 t 1040 ------- 1 2.4 5.4 2 63
+ 0.297784 2 1 t 1040 ------- 1 2.5 5.7 2 65
+ 0.307984 2 1 t 1040 ------- 1 2.5 5.7 2 67
+ 0.325784 2 1 t 1040 ------- 1 2.5 5.7 2 68
#.......... . . ... .. ....... . . .. . ..
# here should be some more lines with different value such as
#- 0.487784 3 0 t 1040 ------- 4 2.8 7.4 2 23
# the above line will be filtered out by the conditions-just ignore it
#.......... . . ... .. ....... . . .. . ..
r 0.188072 0 5 a 40 ------- 1 2.4 5.4 0 10
r 0.239528 0 5 a 40 ------- 1 2.4 5.4 1 28
r 0.263656 0 5 t 40 ------- 1 2.4 5.4 0 37
r 0.317128 0 5 t 1040 ------- 1 2.4 5.4 1 62
r 0.318792 0 5 t 1040 ------- 1 2.4 5.4 2 63
r 0.328792 0 5 t 1040 ------- 1 2.5 5.7 2 65
r 0.338792 0 5 t 1040 ------- 1 2.5 5.7 2 67
r 0.348792 0 5 t 1040 ------- 1 2.5 5.7 2 68',
header = T, stringsAsFactors = F)
aggregate(DF$time, list(src = DF$src, dst = DF$dst, ID = DF$ID), diff)
# src dst ID x
#1 2.4 5.4 10 0.024128
#2 2.4 5.4 28 0.024128
#3 2.4 5.4 37 0.024128
#4 2.4 5.4 62 0.029344
#5 2.4 5.4 63 0.031008
#6 2.5 5.7 65 0.031008
#7 2.5 5.7 67 0.030808
#8 2.5 5.7 68 0.023008
此外,通过命名aggregate
的返回aggDF
,您可以拨打第二个aggregate
来清楚地显示结果:
aggDF <- aggregate(DF$time, list(src = DF$src, dst = DF$dst, ID = DF$ID), diff)
aggregate(aggDF$x, list(src = aggDF$src, dst = aggDF$dst), list)
# src dst x
#1 2.4 5.4 0.024128, 0.024128, 0.024128, 0.029344, 0.031008
#2 2.5 5.7 0.031008, 0.030808, 0.023008