简体   繁体   English

R - 寻找多个最大值

[英]R - Finding muliple max values

I have the following sample data set我有以下示例数据集

 Time <- c(1,2,3,4,5,6,7,8,9,10,11,12)
 Value <- c(0,1,2,3,2,1,2,3,2,1,2,3)

 Data <- data.frame(Time, Value)

I would like to automatically find each maximum for the Value column and create a new data frame with only the Value and associated Time.我想自动找到 Value 列的每个最大值,并创建一个仅包含 Value 和相关时间的新数据框。 In this example, maximum values occur every fourth time interval.在此示例中,最大值每四个时间间隔出现一次。 I would like to group the data into bins and find the associated max value.我想将数据分组到箱中并找到相关的最大值。

I kept my example simple for illustrative purposes, however, keep in mind:出于说明目的,我将示例保持简单,但是请记住:

  1. Each max value in my data set will be different我的数据集中的每个最大值都会不同
  2. Each max value is not guaranteed to occur at equal intervals but rather, I can guarantee that each max value will occur within a range (ie a bin) of time values.不保证每个最大值以相等的时间间隔出现,而是我可以保证每个最大值将出现在时间值的范围(即 bin)内。

Thank you for any help with this process!感谢您在此过程中提供的任何帮助!

You could find the local maxima by finding the points where the diff of the sign of the diff of the Value column is negative.您可以通过查找Value列的diff signdiff为负的点来找到局部最大值。

Data[which(diff(sign(diff(Data$Value))) < 0) + 1,]
#>   Time Value
#> 4    4     3
#> 8    8     3

We can see that this works in a more general case too:我们可以看到这也适用于更一般的情况:

 Time <- seq(0, 10, 0.1)
 Value <- sin(Time)
 Data <- data.frame(Time, Value)
 
 plot(Data$Time, Data$Value)
 
 Data2 <- Data[which(diff(sign(diff(Data$Value))) < 0) + 1,]
 
 abline(v = Data2$Time, col = 'red')

在此处输入图像描述


Edit编辑

Following more info from the OP, it seems we are looking for the maxima within a 120-second window.根据 OP 提供的更多信息,我们似乎正在寻找 120 秒窗口内的最大值。 This being the case, we can get the solution more easily like this:在这种情况下,我们可以像这样更容易地得到解决方案:

library(dplyr)
 
bin_size <- 4 # Used for example only, will be 120 in real use case

Data %>% 
  mutate(Bin = floor((Time - 1) / bin_size)) %>%
  group_by(Bin) %>%
  filter(Value == max(Value))
#> # A tibble: 3 x 3
#> # Groups:   Bin [3]
#>    Time Value   Bin
#>   <dbl> <dbl> <dbl>
#> 1     4     3     0
#> 2     8     3     1
#> 3    12     3     2

Obviously in the real data, change bin_size to 120.显然在真实数据中,将bin_size改为 120。

Maybe this one?也许这个?

library(dplyr)

Data %>% 
  slice_max(Value)

  Time Value
1    4     3
2    8     3
3   12     3

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

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