简体   繁体   English

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

[英]Finding extreme values in vector elements in R

I have a vector like this: 我有一个像这样的向量:

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)

I would like to find the position of the lowest number each time >=5 consecutive values are <-0.5. 我想每次> = 5连续值<-0.5时,找到最低数字的位置。 In the example that is the value -1.044 . 在示例中,该值为-1.044

How do I find this? 我怎么找到这个?

What I have done is this: 我所做的是这样的:

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

But then I just find the position of the first value and not the extreme. 但是,我只是找到第一个值的位置而不是极端值。

Any help? 有什么帮助吗?

Thanks for posting what you've tried. 感谢您发布您尝试过的内容。

I'd just use a logical comparison for xx : 我只是对xx使用逻辑比较:

xx <- x < -0.5

Then your rle logic becomes: 然后,您的角色逻辑变为:

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

From there, identify which values of zz are true and use cumsum to get the indicies of x (this is oversimplified since there is only once instance but you get the picture): 从那里,确定zz哪个值是真实的,并使用cumsum来获得x的指数(这被简化了,因为只有一次实例但您得到了图片):

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

In the instance where you have multiple matches, first will be a vector with length > 1. In that case, make a function and you can apply it to your vector: 在你有多个匹配的情况下, first将与长度> 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)

A function with the apply function inside: 内部具有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])]

} }

if you set all=T you get all matches otherwise only the position of the lowest match. 如果将all设置为T,则将获得所有匹配项,否则仅获得最低匹配项的位置。 Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM