如何计算特定时间段内R的最大平均值?

时间:2020-10-08 09:06:04

标签: r

我正在R中处理此数据:

https://www.dropbox.com/s/dl/chmzqmus6bfoaim/climate_clean.csv

我想知道如何计算欧洲每年的最大平均每月温度。在这里,我们必须考虑变量average_temperature_celcius的含义,即月平均温度和观测值"Europe",它是变量"Continent"的一部分。

我想在所有欧洲国家/地区中以每月的平均温度为单位,以每年的最高平均值作为月份,并制作一个箱形图,显示这些年度最高温度,以查看有多少异常值。

首先,我对数据帧climate进行了子集处理,以仅保留变量Continent == "Europe"

然后我只选择了变量average_temperature_celciusContinentyear

Europe = climate[climate$continent == "Europe", ]
Europebis = select(Europe, year,average_temperature_celsius, month)

最后我分组依据以具有更一致的数据帧

Europebis2 = group_by(Europebis, year, month)
EUROPE = summarise(Europebis2, meany = mean(average_temperature_celsius)

提前谢谢!

1 个答案:

答案 0 :(得分:1)

这能回答您的问题吗?

library(dplyr)
climate %>%
    filter(continent == "Europe") %>% # keep only european countries data
    group_by(year, month) %>% # next, take the average temperature in celsius for each month over all countries
    summarise(across(c(average_temperature_celsius), mean)) %>%
    group_by(year) %>% # next take the month with the maximum average for each year
    summarise(across(c(average_temperature_celsius), max)) %>%
    ggplot(aes(y = average_temperature_celsius)) + 
    geom_boxplot()

要编辑您的箱线图,您可以查看http://www.sthda.com/english/wiki/ggplot2-box-plot-quick-start-guide-r-software-and-data-visualization