Home > Back-end >  Next highest value by time/index
Next highest value by time/index

Time:08-09

Is there a way to obtain the next highest value from the first year (i.e. 1995), ideally with dplyr verbs? For example, the next highest value from 1995 is in 1997, and from 1997 is 1999.

Example dataset:

dat <- tibble::tribble(
  ~ year, ~name, ~value,
  1995, "A", 3000,
  1996, "B", 2500,
  1997, "C", 4010,
  1998, "D", 4000,
  1999, "E", 5000,
  2000, "F", 4500
)

dat

#> # A tibble: 6 × 3
#>    year name  value
#>   <dbl> <chr> <dbl>
#> 1  1995 A      3000
#> 2  1996 B      2500
#> 3  1997 C      4010
#> 4  1998 D      4000
#> 5  1999 E      5000
#> 6  2000 F      4500

I've tried using arrange() and slice_max() functions but it didn't take into account the year/index.

Desired output:

year name value
1995 A 3000
1997 C 4010
1999 E 5000

CodePudding user response:

Probably we can use cummax

> subset(dat, cummax(value) == value)
# A tibble: 3 × 3
   year name  value
  <dbl> <chr> <dbl>
1  1995 A      3000
2  1997 C      4010
3  1999 E      5000

CodePudding user response:

What you are looking for, is called the local maximum/maxima, or local peaks. Using this term, you can find several packages/functions providing a method to find them, like the one below...

library(pracma)
dat[pracma::findpeaks(dat$value, nups = 0)[,2], ]

# A tibble: 3 × 3
   year name  value
   <dbl> <chr> <dbl>
1  1995 A      3000
2  1997 C      4010
3  1999 E      5000
  • Related