如何计算R中某一时间段的最大平均值



我正在R:中处理这些数据

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

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

我想用摄氏度来计算所有欧洲国家每个月的平均温度,并用每年的最高平均值来计算这个月,然后生成一个显示这些最高年温度的方框图,看看有多少异常值。

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

然后我只选择了变量average_temperature_celciusContinentyear

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

Finnaly我按分组以获得更连贯的数据帧

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

提前谢谢!

这能回答您的问题吗?

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

最新更新