计算一定长度的数据列中的事件数

时间:2017-02-12 04:10:35

标签: javascript

我目前正在计算数据集中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;

1 个答案:

答案 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;