Aakriti Aakriti - 1 month ago 22
R Question

For loops in R for data compilation

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)




Answer

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))