Example data:
year <- c(1990, 1991)
January <- c(1, 1)
February <- c(0, 3)
df <- data.frame(year, January, February)
year January February
1 1990 1 0
2 1991 1 3
I want to get a new data frame with the maximum temperature and the month of the maximum temperature, so, this:
max_temp <- c(1,3)
month <- c("January", "February")
new_df <- data.frame(year, month, max_temp)
year month max_temp
1 1990 January 1
2 1991 February 3
Only I have data for 400 years and each year has 1100 months, so it's important that this runs reasonably quickly.
I've melted the original data frame and grouped the data by year:
melted <- melt(df, id.vars = "year")
new_frame <- melted %>%
group_by(year) %>%
summarize(max_temp = max(value))
But I haven't figured out how to get the month. Is there an efficient way to do this in the R idiom?