简体   繁体   中英

Finding first max, min and second max along the time series data

I am new to timeseries data analysis using R. I would like to find the two maximum and one minimum values along the time series data. The lines look like waves as follow:在此处输入图片说明

I would like to find the first max on the line just before the depression (let's say, around 764 on x-axis), the second max on the line after the depression, and minimum value in between these two maximum values. Here is the sample dataset.

X-axis  10:00-10:30 10:30-11:00 11:00-11:30 11:30-12:00 12:00-12:30 12:30-13:00
700.31  0.04    0.05    0.04    0.04    0.05    0.04
700.73  0.04    0.05    0.04    0.04    0.05    0.04
701.14  0.04    0.05    0.04    0.05    0.05    0.04
701.55  0.04    0.05    0.04    0.05    0.05    0.04
701.96  0.04    0.06    0.04    0.05    0.06    0.04
702.37  0.04    0.06    0.04    0.05    0.06    0.04
702.78  0.04    0.06    0.04    0.05    0.06    0.04
703.20  0.04    0.06    0.04    0.05    0.06    0.04
703.61  0.04    0.06    0.04    0.05    0.06    0.05
704.02  0.04    0.06    0.04    0.05    0.06    0.05
704.43  0.05    0.06    0.04    0.05    0.06    0.05
704.84  0.05    0.07    0.05    0.06    0.06    0.05
705.25  0.05    0.07    0.05    0.06    0.07    0.05
705.66  0.05    0.07    0.05    0.06    0.07    0.05
706.07  0.05    0.07    0.05    0.06    0.07    0.05
706.49  0.05    0.07    0.05    0.06    0.07    0.05
706.90  0.05    0.07    0.05    0.06    0.07    0.06
707.31  0.05    0.08    0.05    0.06    0.07    0.06
707.72  0.06    0.08    0.05    0.06    0.08    0.06
708.13  0.06    0.08    0.05    0.07    0.08    0.06
708.54  0.06    0.08    0.06    0.07    0.08    0.06
708.95  0.06    0.08    0.06    0.07    0.08    0.06
709.36  0.06    0.08    0.06    0.07    0.08    0.06
709.77  0.06    0.08    0.06    0.07    0.08    0.06
710.18  0.06    0.09    0.06    0.07    0.08    0.06
710.60  0.06    0.09    0.06    0.07    0.09    0.07
711.01  0.06    0.09    0.06    0.08    0.09    0.07
711.42  0.07    0.09    0.06    0.08    0.09    0.07
711.83  0.07    0.09    0.06    0.08    0.09    0.07
712.24  0.07    0.09    0.07    0.08    0.09    0.07
712.65  0.07    0.09    0.07    0.08    0.09    0.07
713.06  0.07    0.10    0.07    0.08    0.10    0.07
713.47  0.07    0.10    0.07    0.08    0.10    0.07
713.88  0.07    0.10    0.07    0.08    0.10    0.07
714.29  0.07    0.10    0.07    0.09    0.10    0.08
714.70  0.07    0.10    0.07    0.09    0.10    0.08
715.11  0.07    0.11    0.07    0.09    0.10    0.08
715.53  0.08    0.11    0.07    0.09    0.10    0.08
715.94  0.08    0.11    0.08    0.09    0.11    0.08
716.35  0.08    0.11    0.08    0.09    0.11    0.08
716.76  0.08    0.11    0.08    0.09    0.11    0.08
717.17  0.08    0.11    0.08    0.09    0.11    0.08
717.58  0.08    0.11    0.08    0.10    0.11    0.08
717.99  0.08    0.12    0.08    0.10    0.11    0.08
718.40  0.08    0.12    0.08    0.10    0.11    0.08
718.81  0.08    0.12    0.08    0.10    0.12    0.09
719.22  0.08    0.12    0.08    0.10    0.12    0.09
719.63  0.09    0.12    0.08    0.10    0.12    0.09
720.04  0.09    0.12    0.09    0.10    0.12    0.09
720.45  0.09    0.12    0.09    0.10    0.12    0.09
720.86  0.09    0.13    0.09    0.11    0.12    0.09
721.27  0.09    0.13    0.09    0.11    0.13    0.09
721.68  0.09    0.13    0.09    0.11    0.13    0.09
722.09  0.09    0.13    0.09    0.11    0.13    0.09
722.50  0.09    0.13    0.09    0.11    0.13    0.10
722.91  0.09    0.14    0.09    0.11    0.13    0.10
723.32  0.09    0.14    0.09    0.11    0.13    0.10
723.73  0.10    0.14    0.09    0.12    0.13    0.10
724.14  0.10    0.14    0.10    0.12    0.14    0.10
724.55  0.10    0.14    0.10    0.12    0.14    0.10
724.96  0.10    0.14    0.10    0.12    0.14    0.10
725.37  0.10    0.14    0.10    0.12    0.14    0.10
725.78  0.10    0.15    0.10    0.12    0.14    0.11
726.19  0.10    0.15    0.10    0.12    0.15    0.11
726.60  0.10    0.15    0.10    0.13    0.15    0.11
727.01  0.11    0.15    0.10    0.13    0.15    0.11
727.42  0.11    0.15    0.11    0.13    0.15    0.11
727.83  0.11    0.15    0.11    0.13    0.15    0.11
728.24  0.11    0.15    0.11    0.13    0.15    0.11
728.65  0.11    0.16    0.11    0.13    0.15    0.12
729.06  0.11    0.16    0.11    0.13    0.15    0.12
729.47  0.11    0.16    0.11    0.13    0.16    0.12
729.88  0.11    0.16    0.11    0.13    0.16    0.12
730.29  0.11    0.16    0.11    0.14    0.16    0.12
730.70  0.11    0.16    0.11    0.14    0.16    0.12
731.11  0.12    0.16    0.11    0.14    0.16    0.12
731.52  0.12    0.17    0.12    0.14    0.16    0.12
731.93  0.12    0.17    0.12    0.14    0.16    0.13
732.34  0.12    0.17    0.12    0.14    0.17    0.13
732.75  0.12    0.17    0.12    0.14    0.17    0.13
733.16  0.12    0.17    0.12    0.14    0.17    0.13
733.57  0.12    0.17    0.12    0.14    0.17    0.13
733.98  0.12    0.17    0.12    0.15    0.17    0.13
734.39  0.12    0.18    0.12    0.15    0.17    0.13
734.80  0.12    0.18    0.12    0.15    0.17    0.14
735.20  0.12    0.18    0.12    0.15    0.17    0.14
735.61  0.13    0.18    0.12    0.15    0.17    0.14
736.02  0.13    0.18    0.12    0.15    0.18    0.14
736.43  0.13    0.18    0.13    0.15    0.18    0.14
736.84  0.13    0.18    0.13    0.15    0.18    0.14
737.25  0.13    0.18    0.13    0.15    0.18    0.14
737.66  0.13    0.18    0.13    0.15    0.18    0.14
738.07  0.13    0.18    0.13    0.15    0.18    0.14
738.48  0.13    0.18    0.13    0.16    0.18    0.14
738.89  0.13    0.19    0.13    0.16    0.18    0.14
739.30  0.13    0.19    0.13    0.16    0.18    0.14
739.71  0.13    0.19    0.13    0.16    0.18    0.14
740.11  0.13    0.19    0.13    0.16    0.18    0.14
740.52  0.13    0.19    0.13    0.16    0.18    0.14
740.93  0.13    0.19    0.13    0.16    0.19    0.14
741.34  0.13    0.19    0.13    0.16    0.19    0.14
741.75  0.13    0.19    0.13    0.16    0.19    0.15
742.16  0.14    0.19    0.13    0.16    0.19    0.15
742.57  0.14    0.19    0.13    0.16    0.19    0.15
742.98  0.14    0.19    0.14    0.16    0.19    0.15
743.39  0.14    0.19    0.14    0.16    0.19    0.15
743.79  0.14    0.19    0.14    0.16    0.19    0.15
744.20  0.14    0.19    0.14    0.16    0.19    0.15
744.61  0.14    0.20    0.14    0.16    0.19    0.15
745.02  0.14    0.20    0.14    0.16    0.19    0.15
745.43  0.14    0.20    0.14    0.16    0.19    0.15
745.84  0.14    0.20    0.14    0.17    0.19    0.15
746.25  0.14    0.20    0.14    0.17    0.19    0.15
746.66  0.14    0.20    0.14    0.17    0.19    0.15
747.06  0.14    0.20    0.14    0.17    0.19    0.15
747.47  0.14    0.20    0.14    0.17    0.19    0.15
747.88  0.14    0.20    0.14    0.17    0.19    0.15
748.29  0.14    0.20    0.14    0.17    0.20    0.15
748.70  0.14    0.20    0.14    0.17    0.20    0.15
749.11  0.14    0.20    0.14    0.17    0.20    0.15
749.52  0.14    0.20    0.14    0.17    0.20    0.15
749.92  0.14    0.20    0.14    0.17    0.20    0.15
750.33  0.14    0.20    0.14    0.17    0.20    0.16
750.74  0.14    0.20    0.14    0.17    0.20    0.16
751.15  0.14    0.20    0.14    0.17    0.20    0.16
751.56  0.14    0.20    0.14    0.17    0.20    0.16
751.96  0.14    0.20    0.14    0.17    0.20    0.16
752.37  0.14    0.20    0.14    0.17    0.20    0.16
752.78  0.14    0.20    0.14    0.17    0.20    0.15
753.19  0.14    0.20    0.14    0.17    0.20    0.15
753.60  0.14    0.20    0.14    0.17    0.20    0.15
754.01  0.14    0.21    0.14    0.17    0.20    0.16
754.41  0.14    0.21    0.14    0.17    0.20    0.16
754.82  0.14    0.21    0.14    0.17    0.20    0.16
755.23  0.14    0.21    0.14    0.17    0.20    0.16
755.64  0.15    0.21    0.14    0.17    0.20    0.16
756.05  0.15    0.21    0.14    0.17    0.20    0.16
756.45  0.15    0.21    0.14    0.17    0.20    0.16
756.86  0.15    0.21    0.14    0.17    0.20    0.16
757.27  0.15    0.21    0.15    0.17    0.20    0.16
757.68  0.15    0.21    0.14    0.17    0.20    0.16
758.09  0.15    0.21    0.14    0.17    0.20    0.16
758.49  0.15    0.21    0.14    0.17    0.20    0.16
758.90  0.15    0.21    0.14    0.17    0.20    0.16
759.31  0.15    0.21    0.15    0.17    0.20    0.16
759.72  0.14    0.21    0.15    0.17    0.20    0.16
760.12  0.14    0.21    0.14    0.17    0.20    0.16
760.53  0.14    0.20    0.14    0.17    0.20    0.15
760.94  0.14    0.20    0.14    0.17    0.20    0.16
761.35  0.14    0.20    0.14    0.17    0.20    0.16
761.75  0.14    0.19    0.14    0.17    0.20    0.16
762.16  0.14    0.19    0.14    0.17    0.20    0.16
762.57  0.14    0.19    0.14    0.17    0.20    0.16
762.98  0.14    0.19    0.14    0.17    0.20    0.16
763.38  0.14    0.19    0.14    0.17    0.20    0.16
763.79  0.14    0.19    0.14    0.17    0.20    0.15
764.20  0.14    0.20    0.14    0.17    0.20    0.15
764.61  0.14    0.20    0.14    0.17    0.20    0.15
765.01  0.14    0.20    0.14    0.17    0.20    0.16
765.42  0.14    0.20    0.14    0.17    0.20    0.16
765.83  0.14    0.20    0.14    0.17    0.20    0.15
766.24  0.14    0.20    0.14    0.17    0.20    0.15
766.64  0.14    0.21    0.15    0.18    0.21    0.16
767.05  0.15    0.21    0.15    0.18    0.21    0.16
767.46  0.15    0.21    0.15    0.18    0.21    0.16
767.86  0.15    0.21    0.15    0.18    0.21    0.16
768.27  0.15    0.21    0.15    0.18    0.21    0.16
768.68  0.15    0.21    0.15    0.18    0.21    0.16
769.09  0.15    0.21    0.15    0.18    0.21    0.16
769.49  0.15    0.21    0.15    0.18    0.21    0.16
769.90  0.15    0.21    0.15    0.18    0.21    0.16
770.31  0.15    0.21    0.15    0.18    0.21    0.16
770.71  0.15    0.21    0.15    0.18    0.21    0.17
771.12  0.15    0.21    0.15    0.18    0.21    0.17
771.53  0.15    0.21    0.15    0.18    0.21    0.16
771.93  0.15    0.21    0.15    0.18    0.21    0.16
772.34  0.15    0.21    0.15    0.18    0.21    0.16
772.75  0.15    0.21    0.15    0.18    0.21    0.16
773.15  0.15    0.21    0.15    0.18    0.21    0.16
773.56  0.15    0.21    0.15    0.18    0.21    0.16
773.97  0.15    0.21    0.15    0.18    0.21    0.16
774.37  0.15    0.21    0.15    0.18    0.21    0.16
774.78  0.15    0.21    0.15    0.18    0.21    0.16
775.19  0.15    0.21    0.15    0.18    0.21    0.16
775.59  0.15    0.21    0.15    0.18    0.21    0.16
776.00  0.15    0.21    0.15    0.18    0.21    0.16
776.41  0.15    0.21    0.15    0.18    0.21    0.16
776.81  0.15    0.21    0.15    0.18    0.21    0.16
777.22  0.15    0.22    0.15    0.18    0.21    0.16
777.63  0.15    0.22    0.15    0.18    0.21    0.16
778.03  0.15    0.22    0.15    0.18    0.21    0.16
778.44  0.15    0.22    0.15    0.18    0.21    0.16
778.85  0.15    0.21    0.15    0.18    0.21    0.16
779.25  0.15    0.21    0.15    0.18    0.21    0.16
779.66  0.15    0.21    0.15    0.18    0.21    0.16
780.06  0.15    0.21    0.15    0.18    0.21    0.16
780.47  0.15    0.21    0.15    0.18    0.21    0.16
780.88  0.15    0.21    0.15    0.18    0.21    0.16
781.28  0.15    0.21    0.15    0.18    0.21    0.16
781.69  0.15    0.21    0.15    0.18    0.21    0.16
782.09  0.15    0.22    0.15    0.18    0.21    0.16
782.50  0.15    0.22    0.15    0.18    0.21    0.16
782.91  0.15    0.22    0.15    0.18    0.21    0.16
783.31  0.15    0.21    0.15    0.18    0.21    0.16
783.72  0.15    0.22    0.15    0.18    0.21    0.16
784.12  0.15    0.21    0.15    0.18    0.21    0.16
784.53  0.15    0.22    0.15    0.18    0.21    0.16
784.94  0.15    0.22    0.15    0.18    0.21    0.16
785.34  0.15    0.22    0.15    0.18    0.21    0.16
785.75  0.15    0.22    0.15    0.18    0.21    0.16
786.15  0.15    0.22    0.15    0.18    0.21    0.17
786.56  0.15    0.22    0.15    0.18    0.21    0.17
786.97  0.15    0.22    0.15    0.18    0.21    0.16
787.37  0.15    0.22    0.15    0.18    0.21    0.16
787.78  0.15    0.22    0.15    0.18    0.21    0.17
788.18  0.15    0.22    0.15    0.18    0.21    0.17
788.59  0.15    0.22    0.15    0.18    0.21    0.17
788.99  0.15    0.22    0.15    0.18    0.21    0.17
789.40  0.15    0.22    0.15    0.18    0.21    0.17
789.80  0.15    0.22    0.15    0.18    0.21    0.17
790.21  0.15    0.22    0.15    0.18    0.21    0.17
790.62  0.15    0.22    0.15    0.18    0.21    0.16
791.02  0.15    0.22    0.15    0.18    0.21    0.17
791.43  0.15    0.22    0.15    0.18    0.21    0.17
791.83  0.15    0.22    0.15    0.18    0.21    0.17
792.24  0.15    0.22    0.15    0.18    0.21    0.17
792.64  0.15    0.22    0.15    0.18    0.21    0.17
793.05  0.15    0.22    0.15    0.18    0.21    0.17
793.45  0.15    0.22    0.15    0.18    0.21    0.17
793.86  0.15    0.22    0.15    0.18    0.21    0.17
794.26  0.15    0.22    0.15    0.18    0.21    0.17
794.67  0.15    0.22    0.15    0.18    0.21    0.17
795.07  0.15    0.22    0.15    0.18    0.21    0.17
795.48  0.15    0.22    0.15    0.18    0.21    0.17
795.88  0.15    0.22    0.15    0.18    0.21    0.16
796.29  0.15    0.22    0.15    0.18    0.21    0.16
796.69  0.15    0.22    0.15    0.18    0.21    0.17
797.10  0.15    0.22    0.15    0.18    0.21    0.17
797.50  0.15    0.22    0.15    0.18    0.21    0.17
797.91  0.15    0.22    0.15    0.18    0.21    0.17
798.31  0.15    0.22    0.15    0.18    0.21    0.17
798.72  0.15    0.22    0.15    0.18    0.21    0.17
799.12  0.15    0.22    0.15    0.18    0.21    0.17
799.53  0.15    0.22    0.15    0.18    0.21    0.16
799.93  0.15    0.22    0.15    0.18    0.21    0.17

I'm not sure this function works for your data, and also, I think the data you provide has not enough time points, so I made dummy data to this example.

Please let me know if I misunderstand your purpose.

By using findPeaks and findValleys from library(quantmod) ,

dummy <- data.frame(
  x = seq(from = -pi/4, to = 9/4*pi, length.out = 1000)
) %>% as_tibble %>%
  mutate(y = cos(x))
plot(dummy$x, dummy$y, type="l")
points(dummy$x[findPeaks(dummy$y)], dummy$y[findPeaks(dummy$y)], col = "red", cex = 2, pch = 19)
points(dummy$x[findValleys(dummy$y)], dummy$y[findValleys(dummy$y)], col = "blue", cex = 2, pch = 19)

在此处输入图片说明

I think this graph is similar with your situation. Red dot will first and second max and blue dot will minimum. In your case, you might need to apply these function to some restricted intervals. Take a look at findPeaks function documentation for information about functions.

findPeaks(dummy$y)
[1] 102 901
findValleys(dummy$y)
[1] 501 502

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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