获取熔融数据框中具有最大值的变量的名称

Getting the name of the variable with maximum value in a molten data frame

示例数据:

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

我想得到一个新的数据框,其中包含最高温度和最高温度的月份,所以,这个:

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

只有我有 400 年的数据,每年有 1100 个月,所以运行得相当快很重要。

我已经融化了原始数据框并按年份对数据进行了分组:

melted <- melt(df, id.vars = "year")
new_frame <- melted %>%
  group_by(year) %>%
  summarize(max_temp = max(value))

但是我还没有弄清楚如何获得月份。在 R 习语中有没有一种有效的方法来做到这一点?

library(tidyverse)

df_new <- df %>% gather(month, temp, January:February) %>%
      group_by(year) %>% filter(temp == max(temp))