My code below currently reads in a dataset, which I am feeding into the function. It calculates the sum of the values in that column into this.sum
. Now I would like to extend the capabilities of this code.
My dataset has lots of 0's, followed by short stretches of values, or "events". I would like to find the number of these "events", based on how many times my data goes from 0 to a value. Also, I would like events which have fewer than five zeroes separating them, to be in the same event.
So, with my dataset below, I would like it to say that "2 events have occurred."
I want to still keep 'this.sum', but would like to now have this.events
stored.
I am used to scripting languages like Python and Matlab, and am new to Javascript, so any help is appreciated!
processData: function(data) {
// convert our data from the file into an array
var lines = data.replace(/\n+$/, "").split("\n");
// we have array of strings but need an array of Numbers this should do the trick getting rid of any text and 0 values (source: http://stackoverflow.com/a/26641971)
lines = lines.map(Number).filter(Boolean);
// now we find the sum of all of the values in our array. (source: http://stackoverflow.com/a/16751601)
this.sum = lines.reduce((a, b) => a + b, 0);
this.sum = this.sum.toFixed(2);
},
Data:
0
0
0
0
0
0
0
0
0
.256
.369
.598
.145
.695
.984
.259
.368
.352
0
0
0
0
0
0
0
0
0
0
0
0
.256
.369
.598
.145
.695
.984
.259
.368
.352
0
0
0
0
.256
.369
.598
.145
.695
.984
.259
.368
.352
EDIT:
The following gave me an incorrect value for ev
, regardless of the changes I made to count
(changed from 5, to 10, to 15) (my dataset contains different numbers than the dataset given above.) But as I changed the value to 500, it dropped, so I went higher to 12,000 and it gave me the value for this.events
that I was expecting.
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 < data.length; i++) { // 'count' will be the counter of consecutive zeros
if(lines[i] != 0) { // if we encounter a non-zero line
if(count > 5) 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;
// we have array of strings but need an array of Numbers this should do the trick getting rid of any text and 0 values (source: http://stackoverflow.com/a/26641971)
lines = lines.map(Number).filter(Boolean);
// now we find the sum of all of the values in our array. (source: http://stackoverflow.com/a/16751601)
this.sum = lines.reduce((a, b) => a + b, 0);
this.sum = this.sum.toFixed(2);
},