I have a data frame with blocks of values with 0 and 1 and NAs, for example:
mydata <- data.frame(a = c(0,0,0,1,1,1,0,0,0,1,1,NA,NA,NA), b = c(0,0,1,1,1,1,0,0,1,1,0,NA,NA,NA))
what I want is to obtain, for each variable, the index of start and end of each block 1, this will the desired result:
mydata <- data.frame(a = c(4,6,10,11), b = c(3,6,9,10))
How can I code it?
You may try
apply(mydata, 2, function(x){
  y <- rle(x == 1)
  z <- c(cumsum(y$lengths)[which(y$values)], cumsum(y$lengths)[which(y$values) - 1] + 1)
  return(sort(z))
})
      a  b
[1,]  4  3
[2,]  6  6
[3,] 10  9
[4,] 11 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With