Home > Software design >  R - Finding muliple max values
R - Finding muliple max values

Time:05-28

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. 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 (i.e. a bin) of time values.

Thank you for any help with this process!

CodePudding user response:

Maybe this one?

library(dplyr)

Data %>% 
  slice_max(Value)

  Time Value
1    4     3
2    8     3
3   12     3

CodePudding user response:

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.

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')

enter image description here


Edit

Following more info from the OP, it seems we are looking for the maxima within a 120-second window. 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.

  • Related