我有一个随时间推移的二进制信号突发数据表。这些信号突发的长度是随机的。所以我需要找到从 0 到 1(起点)变化的行索引,以及从 1 到 0(终点)变化的行索引。这样最终我可以找到每个信号突发的开始和结束时间。
我该怎么做?
假设您的数据如下所示:
R> x
V1 V2 V3 V4 V5
[1,] 0 0 0 0 1
[2,] 0 1 0 0 1
[3,] 1 1 0 0 1
[4,] 1 1 0 0 1
[5,] 1 1 1 0 1
[6,] 1 1 1 0 1
[7,] 1 1 1 0 0
[8,] 1 1 1 0 0
[9,] 1 1 1 0 0
[10,] 1 1 1 1 0
[11,] 1 0 1 1 0
[12,] 1 0 1 1 0
[13,] 1 0 1 1 0
[14,] 1 0 1 1 0
[15,] 1 0 1 1 0
[16,] 1 0 1 1 0
[17,] 1 0 1 1 0
[18,] 0 0 1 1 0
[19,] 0 0 1 0 0
[20,] 0 0 1 0 0
我会做的:
apply(x, 2,
function (k) {
w <- which(k == 1, arr.ind=TRUE)
c(head(w, 1), tail(w, 1))
})
V1 V2 V3 V4 V5
[1,] 3 2 5 10 1
[2,] 17 10 20 18 6
您可以使用diff
或rle
如评论中所述。但是您应该提供:
信号示例
set.seed(1)
rr <- rbinom(30,1,0.5)
你试过什么?使用diff
例如我做以下
ind <- c(0,diff(rr))
预期的输出?
start <- min(which(ind==1)) ## change from 0 to 1 (the start point)
end <- max(which(ind==-1)) ## change of 1 to 0 (the end point)