系列跨越固定水平的所有区间的界限

时间:2015-08-07 20:46:13

标签: r

我有很多两列矩阵,比如说这个:

X<-structure(c(-3.09, -3.028, -2.965, -2.903, -2.841, -2.778, -2.716, 
-2.653, -2.591, -2.528, -2.466, -2.404, -2.341, -2.279, -2.216, 
-2.154, -2.091, -2.029, -1.967, -1.904, -1.842, -1.779, -1.717, 
-1.654, -1.592, -1.53, -1.467, -1.405, -1.342, -1.28, -1.217, 
-1.155, -1.093, -1.03, -0.968, -0.905, -0.843, -0.78, -0.718, 
-0.656, -0.593, -0.531, -0.468, -0.406, -0.343, -0.281, -0.219, 
-0.156, -0.094, -0.031, 0.031, 0.094, 0.156, 0.219, 0.281, 0.343, 
0.406, 0.468, 0.531, 0.593, 0.656, 0.718, 0.78, 0.843, 0.905, 
0.968, 1.03, 1.093, 1.155, 1.217, 1.28, 1.342, 1.405, 1.467, 
1.53, 1.592, 1.654, 1.717, 1.779, 1.842, 1.904, 1.967, 2.029, 
2.091, 2.154, 2.216, 2.279, 2.341, 2.404, 2.466, 2.528, 2.591, 
2.653, 2.716, 2.778, 2.841, 2.903, 2.965, 3.028, 3.09, 10.188, 
10.195, 10.202, 10.209, 10.216, 10.224, 10.233, 10.241, 10.25, 
10.26, 10.27, 10.281, 10.293, 10.305, 10.318, 10.332, 10.346, 
10.362, 10.378, 10.396, 10.415, 10.435, 10.456, 10.479, 10.504, 
10.53, 10.558, 10.589, 10.622, 10.658, 10.696, 10.738, 10.784, 
10.833, 10.887, 10.946, 11.011, 11.082, 11.161, 0.165, 0.095, 
0.086, 0.091, 0.348, 12.949, 12.793, 12.673, 12.587, 12.531, 
12.503, 12.503, 12.531, 12.587, 12.673, 12.793, 12.949, 0.348, 
0.091, 0.086, 0.095, 0.165, 11.161, 11.082, 11.011, 10.946, 10.887, 
10.833, 10.784, 10.738, 10.696, 10.658, 10.622, 10.589, 10.558, 
10.53, 10.504, 10.479, 10.456, 10.435, 10.415, 10.396, 10.378, 
10.362, 10.346, 10.332, 10.318, 10.305, 10.293, 10.281, 10.27, 
10.26, 10.25, 10.241, 10.233, 10.224, 10.216, 10.209, 10.202, 
10.195, 10.188), .Dim = c(100L, 2L), .Dimnames = list(NULL, c("x0", 
"x2")))

很多时候,第二列(x2)的条目将越过1然后再回到上面。

我正在尝试构建一个矩阵 每次x2与1相交时将是一行,并且对于每一行,具有第一个值x0(第一列)的列为x2 <1,第二列具有第一个值x0,其中x2> 1。例如,此表的第一行应具有值:

 -0.656,-0.343

第二行:

 0.406,0.718

,对于这个X,不应该有第三行。

1 个答案:

答案 0 :(得分:1)

叉口

d<-diff(X[,2]>1)

过境指数

ind<-cbind(which(d==-1)+1,which(d==1)+1)

交叉口的值

matrix(X[ind,1],nrow(ind),ncol(ind))
       [,1]   [,2]
[1,] -0.656 -0.343
[2,]  0.406  0.718