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 |