我有一个TRUE和FALSE的逻辑向量。现在我想找出10个最长序列的TRUE或FALSE和(最重要的)它们的位置。与
set.seed(123)
data <- sample(seq(from = 1, to = 100, by = 1), size = 3000, replace = TRUE)
reference=3
logical.vec <- data > reference
logical.vec.rle <- rle(logical.vec)
MaxiSequence <- max(logical.vec.rle$length[logical.vec.rle$values=="TRUE"])
它会返回类似MaxiSequence = 55L的内容。我的问题是那个
答案 0 :(得分:1)
使用data.table
及其rleid
- 功能,您可以执行以下操作:
DT <- data.table(logical.vec, runid = rleid(logical.vec))
DT_stat <- DT[,.(length = .N, position = .I[1], type=logical.vec[1]), by = runid]
# order by length
setorder(DT_stat, -length)
# Get top 3 (or n) by type
DT_stat[, .SD[1:3], type]
所以这导致:
type runid length position
1: TRUE 7 125 144
2: TRUE 103 121 1623
3: TRUE 115 96 1847
4: FALSE 34 2 717
5: FALSE 80 2 1351
6: FALSE 88 2 1491