如何返回数值向量的最新最小值的向量?

时间:2019-03-18 03:16:40

标签: r

我有以下向量,它包含以秒为单位的时间:

[uwsgi]
socket=127.0.0.1:8000
chdir=home/sscc/sscc2019
wsgi-file=sscc2019/wsgi.py
processes=4
threads=2
master=True
pidfile=uwsgi.pid
daemonize=uswgi.log
module=sscc2019.wsgi:application
vacuum=true
virtualenv=/home/ssccenv

我正在寻找返回另一个向量,该向量将是该元素的“最佳最小值”。因此结果将是:

times <- c(93.813, 89.662, 89.246, 88.641, 88.759, 89.034, 88.783, 88.651, 88.664, 89.028, 88.784, 88.633, 88.616, 88.300, 88.217, 87.954, 88.377, 88.203,
87.836, 87.832, 87.815, 88.514, 88.637, 88.496, 87.338, 87.450, 87.638, 87.511, 87.547, 87.240, 87.235, 87.266, 87.362, 87.457, 87.376, 87.465,
87.108, 87.358, 87.229, 87.273, 87.043, 86.739, 87.397, 87.540, 87.435, 87.448, 87.098, 87.184, 87.071, 87.238, 86.890, 87.898, 86.983, 86.540,
86.830, 86.610, 85.580, 86.627)

,依此类推,直到出现更短的时间。我一直在玩c(93.813, 89.662, 89.246, 88.641, 88.641, 88.641, 88.641, ...) ,就像这样:

lag

但这没用。

2 个答案:

答案 0 :(得分:3)

您是否在cummin之后(即vector的累积最小值)?

cummin(times)
# [1] 93.813 89.662 89.246 88.641 88.641 88.641 88.641 88.641 88.641 88.641
#[11] 88.641 88.633 88.616 88.300 88.217 87.954 87.954 87.954 87.836 87.832
#[21] 87.815 87.815 87.815 87.815 87.338 87.338 87.338 87.338 87.338 87.240
#[31] 87.235 87.235 87.235 87.235 87.235 87.235 87.108 87.108 87.108 87.108
#[41] 87.043 86.739 86.739 86.739 86.739 86.739 86.739 86.739 86.739 86.739
#[51] 86.739 86.739 86.739 86.540 86.540 86.540 85.580 85.580

答案 1 :(得分:0)

也许有人可以提出一个更优雅的解决方案,但是可行的解决方案如下:

current = NULL
min.vec = NULL

for(i in 1:length(times)){
  current = c(current, times[i])
  min.vec = c(min.vec, min(current))
}

> min.vec
 [1] 93.813 89.662 89.246 88.641 88.641 88.641 88.641 88.641 88.641
[10] 88.641 88.641 88.633 88.616 88.300 88.217 87.954 87.954 87.954
[19] 87.836 87.832 87.815 87.815 87.815 87.815 87.338 87.338 87.338
[28] 87.338 87.338 87.240 87.235 87.235 87.235 87.235 87.235 87.235
[37] 87.108 87.108 87.108 87.108 87.043 86.739 86.739 86.739 86.739
[46] 86.739 86.739 86.739 86.739 86.739 86.739 86.739 86.739 86.540
[55] 86.540 86.540 85.580 85.580