I have a data frame that has a column populated with 0s and 1s. Here is an example of what that data looks like:
0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 0 0 0 ... etc.
0 - 5
1 - 4
0 - 10
1 - 3
0 - 1
1 - 2
0 - 4
var data = [0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0];
var result = [];
for (var i = 0; i < data.length; i++) {
var item = {};
item[data[i]] = 1;
if (!result.length) {
result.push(item);
} else {
var lastItem = result[result.length - 1];
if (lastItem[data[i]]) {
lastItem[data[i]] += 1;
} else {
result.push(item);
}
}
}
console.log(result)
There is a function for that. rle
calculates the run-length of each element in the vector. There are two parts to the output, the lengths of the runs, and the values themselves:
rle(x)
#Run Length Encoding
# lengths: int [1:7] 5 4 10 3 1 2 4
# values : int [1:7] 0 1 0 1 0 1 0
To create a matrix, we can use:
with(rle(x), cbind(values, lengths))