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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -