I have a table of values corresponding to an annual series of years, and i wanna know the code for R to give me the year if the highest value?
library(dplyr)
df<-tibble(value=c(10,20,30,20,40,60),year=c(2001,2001,2001,2002,2002,2002))
I have a table of values corresponding to an annual series of years, and i wanna know the code for R to give me the year if the highest value?
library(dplyr)
df<-tibble(value=c(10,20,30,20,40,60),year=c(2001,2001,2001,2002,2002,2002))
We may use slice_max
library(dplyr)
df %>%
group_by(year) %>%
slice_max(n = 1, order_by = value) %>%
ungroup
I assume you have something like that
library(dplyr)
df <- tibble(
value = c(10,20,30,20,40,60),
year = c(2001,2001,2001,2002,2002,2002)
)
You could use group_by to group each year summarise and max functions to return the highest value.
df %>%
dplyr::group_by(year) %>%
dplyr:: summarise(max_value = max(value))
)
This will return that
# A tibble: 2 x 2
year max_value
<dbl> <dbl>
1 2001 30
2 2002 60
Next time provide some sample of your data because I do not know if this solves your problem.