在R中的向量元素中查找极值

时间:2013-10-02 14:47:21

标签: r

我有这样的矢量:

x<-c(-0.193,-0.126,-0.275,-0.375,-0.307,-0.347,-0.159,-0.268,-0.013,0.070,0.346,
0.376,0.471,0.512,0.291,0.554,0.185,0.209,0.057,0.058,-0.157,-0.291,-0.509,
-0.534,-0.239,-0.389,0.060,0.250,0.279,0.116,0.052,0.201,0.407,0.360,0.065,
-0.167,-0.572,-0.984,-1.044,-1.039,-0.831,-0.584,-0.425,-0.362,-0.154,0.207,
0.550,0.677,0.687,0.856,0.683,0.375,0.298,0.581,0.546,0.098,-0.081)

我想找到每次最低数字的位置&gt; = 5个连续值<-0.5。在示例中,值为 -1.044

我如何找到这个?

我所做的是:

xx<-ifelse(x>.5,1,NA)
xx

aa<-rle(xx)
zz <- rep(FALSE, length(xx))
zz[sequence(aa$lengths) == 1] <- aa$lengths >= 5 & aa$values == 1
zz

但是我只是找到了第一个值的位置而不是极端值。

任何帮助?

2 个答案:

答案 0 :(得分:2)

感谢发布您尝试过的内容。

我只使用xx的逻辑比较:

xx <- x < -0.5

然后你的逻辑成为:

aa <- rle(xx)
zz <- aa$lengths >= 5 & aa$values

从那里,确定zz的哪些值为真,并使用cumsum来获取x的指示(这是过于简单的,因为只有一次实例但你得到了图片) :

first <- which(zz)
idxs <- cumsum(aa$lengths[1:first])
min(x[idxs[first-1]:idxs[first]])

在您有多个匹配项的情况下,first将是一个长度为&gt;的向量1.在这种情况下,创建一个函数,你可以apply将它转换为向量:

myfun <- function(y) {
    idxs <- c(0, cumsum(aa$lengths[1:y]))
    min(x[idxs[y]:idxs[y+1]])
}

set.seed(20)
x <- rnorm(100)
xx <- x < -0.5
aa <- rle(xx)
zz <- aa$lengths >= 3 & aa$values
first <- which(zz)

sapply(first, myfun)

答案 1 :(得分:0)

具有apply函数的函数:

find.val <- function(x,threshold,n,all=T){
  tmp <- rle(x < threshold)
  cs <- cumsum(tmp$lengths)
  dfcs <- data.frame(indices=c(0,cs[-length(cs)])+1,l=cs)
  pos <- (apply(dfcs,1,function(y) which.min(x[y[1]:y[2]])+y[1]-1))[tmp$values==1 & tmp$lengths >= n]
  if(all==T) return(pos)
  pos[which.min(x[pos])]

}

如果设置all = T,则获得所有匹配,否则仅获得最低匹配的位置。 例如:

find.val(x,-0.5,5,all=T)