0

我有大文件,我需要计算不同记录的时间差。为了说明,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 的格式: xy ,其中 x 始终 =2 并且 y 正在变化(即 y=0,1,2,3,4,.......)。此外, dst: zf ,其中 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
4

1 回答 1

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

此外,通过命名aggregatereturn 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
于 2013-10-27T09:01:59.503 回答