i'm trying to get a specific count from a previously created result set. I need a count of rows between rows that contain NA's. An aggregation of values of these rows are not of interest, only the count.
Below a quite simplified example which hopefully better explains what i'm talking about. At left hand the actual data and at right hand the desired result.
+------+-------+---+------+--------+ | TIME | Value | - | TIME | Result | +------+-------+---+------+--------+ | 10 | NA | - | 20 | 2 | | 20 | 0 | - | 60 | 3 | | 30 | 1 | - | | | | 40 | NA | - | | | | 50 | NA | - | | | | 60 | 30 | - | | | | 70 | 68 | - | | | | 80 | 0 | - | | | | 90 | NA | - | | | +------+-------+---+------+--------+
Any comments are welcome. In case additional input is needed please leave a message.
In order to make my answer complete here a modified version:
d <- data.frame( TIME = seq(10, 90, by = 10), Value = c(NA, 0, 1, NA, NA, 30, 68, 0, NA))
aux <- rle(as.numeric((!is.na(d[,2]))))
cbind(TIME = d[cumsum(aux$lengths)[which(aux$values == 1)] - aux$lengths[aux$values == 1] +1, 1],
Result = rle(is.na(d$Value))$lengths[!rle(is.na(d$Value))$values])
TIME Result
[1,] 2 20
[2,] 3 60
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