I have a data frame with two variables: dates and value and and I want to select the date in which there was the maximum value. The max () function gives me the maximum value of the value variable but I don't know if there is any way to get the date that this value occurred, does anyone have any ideas?
dates
value
max ()
Thanks for reading me
dat <- data.frame(dates = c("2020-05-23","2020-05-24","2020-05-25"), value = c(42,137,137)) dat[which(dat$value == max(dat$value)),][1] #> dates #> 2 2020-05-24 #> 3 2020-05-25
library dplyr has a convenience function for selecting the max (also has one for min) using technocrat's example dat data.frame :
dat %>% slice_max(value)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.