向量的最长有序部分的第一个索引 [英] First index of longest ordered portion of a vector
问题描述
我希望提取矢量的最长有序部分。例如,使用此向量:
I am looking to extract the longest ordered portion of a vector. So for example with this vector:
x <- c(1,2,1,0.5,1,4,2,1:10)
x
[1] 1.0 2.0 1.0 0.5 1.0 4.0 2.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
我会应用一些函数,返回以下内容:
I'd apply some function, get the following returned:
x_ord <- some_func(x)
x_ord
[1] 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
我一直在尝试利用 is.unsorted()
来确定向量不再排序的位置。这是我的混乱尝试和迄今为止我所拥有的:
I've been trying to leverage is.unsorted()
to determine at what point the vector is no longer sorted. Here is my messy attempt and what I have so far:
for(i in 1:length(x)){
if( is.unsorted(x[i:length(x)])==TRUE ){
cat(i,"\n")}
else{x_ord=print(x[i])}
}
然而,这显然不是正确的 x_ord
正在生成 10
。我也希望这更加通用,并且在有序序列之后覆盖非增加数字以及类似这样的向量:
However, this clearly isn't right as x_ord
is producing a 10
. I am also hoping to make this more general and cover non increasing numbers after the ordered sequence as well with a vector something like this:
x2 <- c(1,2,1,0.5,1,4,2,1:10,2,3)
现在虽然我仍然坚持确定所提到的第一个向量中的增加序列。
Right now though I am stuck on identifying the increasing sequence in the first vector mentioned.
任何想法?
推荐答案
这似乎有效:
s = 1L + c(0L, which( x[-1L] < x[-length(x)] ), length(x))
w = which.max(diff(s))
x[s[w]:(s[w+1]-1L)]
# 1 2 3 4 5 6 7 8 9 10
s
是运行开始的地方,再加上 length(x) +1
,为方便起见:
s
are where the runs start, plus length(x)+1
, for convenience:
- 第一次运行从1开始
- 后续运行开始时有一个下降
- 我们在
length(x)+1
上进行操作,下一次运行将开始如果向量继续
- the first run starts at 1
- subsequent runs starts where there is a drop
- we tack on
length(x)+1
, where the next run would start if the vector continued
diff(s)
是运行的长度
和 which.max
获得第一个最大化,以打破关系。
diff(s)
are the lengths of the runs
and which.max
takes the first maximizer, to break ties.
s [w]
是选定运行的开始; s [w + 1L]
是下次运行的开始;所以要获得属于所选运行的数字: s [w] :( s [w + 1] -1L)
。
s[w]
is the start of the chosen run; s[w+1L]
is the start of the next run; so to get the numbers belonging to the chosen run: s[w]:(s[w+1]-1L)
.
或者,拆分然后选择所需的子向量:
Alternately, split and then select the desired subvector:
sp = split(x, cumsum(x < c(-Inf, x[-length(x)])))
sp[[which.max(lengths(sp))]]
# 1 2 3 4 5 6 7 8 9 10
这篇关于向量的最长有序部分的第一个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!