1

大きなデータ (.tr ファイル) があります。ファイルを読み、データ フレーム (df) の列の名前を変更しました。私はなんとか既存の記録をすべて調べ、特定の条件を確認しました。ファイル全体に存在する一意の値 (src.port 列から) の数を計算する必要がありますか? 次の MWE は私の質問を説明します。

# The df looks like:
     st time      from to protocol size flags    flowID src.port dst.port  seq   pktID
      + 0.100000    1   2      tcp   40 -------      1      5.0       2.1     0     0
      - 0.100000    5   0      ack   40 -------      1      5.1       2.3     0     0
      r 0.102032    1   2      tcp   40 -------      1      5.20      2.5     0     0
      r 0.102032    1   2      tcp   40 -------      1      5.11      2.6     0     0
      r 0.102032    1   2      tcp   40 -------      1      3.0       2.0     0     0
      + 0.121247   11   0      ack   40 -------      1      11.1      2.10    0     1
      r 0.132032    1   2      tcp   40 -------      1      3.0       2.0     0     0
      r 0.142065    1   2      tcp   40 -------      1      3.0       4.0     0     0

 # I have tried the following:
   unique<-0
  for (i in 1:nrow(df)){
    # feel free to suggest different way from the below line. 
    # I think using the name of column would be better 
    if(df[i,1]=="r" && df[i,3]== 1 && df[i,4]== 2 && df[i,5]== "tcp" ){
     # now this condition is my question
     # check if df[i,9] is new not presented before...Note 5.0 is different from 5.1 
     # check if df[i,10] is 2 and ignore any value after the dot (i.e 2.x ..X means any value)
     # so the condition would be:
      if ( df[i,9] is new not repeated && df[i,10] is 2.x)
          unique<-unique+1
     }

   } 

サンプル データから予想される出力:is unique=3

4

1 に答える 1

0

関連するデータを単純にサブセット化して使用できますunique。ここでは、すべての条件を連鎖させ、「scr.port」列だけを抽出uniqueして結果に使用しました。

unique(mydf[mydf[, "st"] == "r" & 
              mydf[, "from"] == 1 & 
              mydf[, "protocol"] == "tcp" & 
              grepl("^2.*", mydf[, "dst.port"]), 
            "src.port"])
# [1] 5.20 5.11 3.00

それをラップして、length探しているカウントを取得します。

または、データのサブセットを作成し、行数を数えます。

out <- mydf[mydf[, "st"] == "r" & 
              mydf[, "from"] == 1 & 
              mydf[, "protocol"] == "tcp" & 
              grepl("^2.*", mydf[, "dst.port"]), ]
nrow(out[!duplicated(out$src.port), ])
# [1] 3
于 2013-10-26T03:02:32.837 に答える