r - Using dplyr group_by summarise how to keep a variable occuring at the maximum of another variable? -
for example, using airquality data, want calculate maximum temperature each month. keep day on maximum temperature occurred.
library(dplyr) # maximum temperature per month airqualitymax <- airquality %>% group_by(month) %>% summarise(maxtemp = max(temp)) # day of month on max occured airquality %>% left_join(airqualitymax, = "month") %>% filter(temp == maxtemp)
now appears day not unique, suppose unique, there way select day on maximum occurs in summarise() directly?
we can use slice
keep row have maximum 'temp' each 'month'
airquality %>% group_by(month) %>% slice(which.max(temp))
a faster option arrange
'temp' in descending (or ascending) , first observation (or last slice(n())
)
airquality %>% group_by(month) %>% arrange(desc(temp)) %>% slice(1l)
Comments
Post a Comment