大佬教程收集整理的这篇文章主要介绍了如何对术语进行分组?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将月份组合在一起并绘制它。不知道如何处理它,任何帮助都会很棒。
数据:
@H_978_7@month<-c("January","February","march","April","May","June","July","August","September","October","November","December") year<-c(2020,2021) Values<-c(29,43,75,37,89,88,45,64,24,15,26,35,78,93,86,90)
所以我想将 1 月到 4 月、5 月至 8 月、9 月至 12 月分组,然后绘制这些月份的平均值。我不知道该怎么做。
如果您的数据名为 df
,您可以尝试:
library(dplyr)
library(ggplot2)
df %>%
group_by(Month = case_when(Month %in% month.name[1:4] ~ 'Jan - Apr',Month %in% month.name[5:8] ~ 'May - Aug',TRUE ~ 'Sep - Dec')) %>%
summarise(Values = mean(Values,na.rm = TRUE)) %>%
ggplot() + aes(Month,Values) + geom_col()
注意 @H_978_7@month.name 是一个内置的 R 常数
@H_978_7@month.name #[1] "January" "February" "March" "April" "May" "June" "July" #[8] "August" "September" "October" "November" "December",
使用 cut
library(dplyr)
library(ggplot2)
df %>%
group_by(Month = cut(match(Month,month.Name),breaks = c(-Inf,4,8,Inf),labels = c('Jan - Apr','May - Aug','Sep - Dec'))) %>%
summarise(Values = mean(Values,na.rm = TRUE)) %>%
ggplot(aes(Month,Values)) +
geom_col()
以上是大佬教程为你收集整理的如何对术语进行分组?全部内容,希望文章能够帮你解决如何对术语进行分组?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。