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.
I need to format the data so that it counts the number of consecutive 0s and 1s, like this:
0 - 5
1 - 4
0 - 10
1 - 3
0 - 1
1 - 2
0 - 4
I'm new to R and am not sure how to do this, but here's how I would do it in JavaScript. I would love some guidance on how to replicate this result in R. Thank you in advance for your help!
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)