2017-02-12 78 views
0

我目前正在統計我數據集中的events的數量(只是我正在讀取的.csv中的一列數據),以及計數值從0變爲1的次數。如果該值從0變爲1,則將其歸類爲事件。我還說如果兩個事件之間的零數小於60,那麼我將它們歸入同一事件。計算一定長度的一列數據中的事件數

現在,我試圖修改我的代碼來只計算持續時間超過20行的事件。我試圖把它寫入我的條件,如if(lines[i] != 0 && i>20),但我沒有得到ev的正確值。

processData: function(data) { 
     // convert our data from the file into an array 
     var lines = data.replace(/\n+$/, "").split("\n"); 
     var ev = 0; // the event counter (initialized to 0) 
     for(var i = 0, count = 0; i < lines.length; i++) { // 'count' will be the counter of consecutive zeros 
     if(lines[i] != 0) { // if we encounter a non-zero line 
      if(count > 60) ev++; // if the count of the previous zeros is greater than 5, then increment ev (>5 mean that 5 zeros or less will be ignored) 

       count = 1; // reset the count to 1 instead of 0 because the next while loop will skip one zero 
      while(++i < lines.length && lines[i] != 0) // skip the non-zero values (unfortunetly this will skip the first (next) zero as well that's why we reset count to 1 to include this skipped 0) 
      ; 
     } 
     else // if not (if this is a zero), then increment the count of consecutive zeros 
     count++; 
     } 
     this.events = ev; 
+0

你想要得到的是對他們有至少20項事件的計數? –

+0

@ibrahimmahrir對,對。因此,如果該事件包含少於20個條目,那麼我不想將其視爲事件。 – Gary

+0

上次我沒有問你的一件事:可以將事件的值設爲0嗎?如果小於60,這是否意味着這些0不是分隔符,而是事件的延續? –

回答

1
processData: function(data) { 
     var lines = data.replace(/\n+$/, "").split("\n"); 
     var ev = 0; 
     for(var i = 0, count = 0; i < lines.length; i++) { 
      if(lines[i] != 0) { 
       var evcount = 1; // initialize evcount to use it to count non-zero values (the entries in an event) (1 so we won't skip the first) 
       while(++i < lines.length && lines[i] != 0) 
        evcount++; // increment every time a non-zero value is found 

       if(count > 60 && evcount > 20) ev++; // if there is more than 60 0s and there is more than 20 entries in the event then increment the event counter 

       count = 1; // initialize count 
      } 
      else // if not (if this is a zero), then increment the count of consecutive zeros 
       count++; 
     } 
     this.events = ev;