Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect index based on a condition

Tags:

arrays

sorting

r

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?

like image 827
Nicolas Cuenca Zaldivar Avatar asked Oct 28 '25 09:10

Nicolas Cuenca Zaldivar


1 Answers

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
like image 69
Park Avatar answered Oct 30 '25 23:10

Park



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!