R help可以计算几年中几个月的平均值 [英] R help calculate a mean for several months over several years

查看:102
本文介绍了R help可以计算几年中几个月的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助找到1950年至2018年的黄金平均月度,这就是我的数据

I need help finding the monthly average of gold from 1950 to 2018 this is what my data looks like

str(gold)
'data.frame':   827 obs. of  2 variables:
 $ Date : Factor w/ 827 levels "1950-01","1950-02",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Price: num  34.7 34.7 34.7 34.7 34.7 ...

> head(gold)
Date Price
1 1950-01 34.73
2 1950-02 34.73
3 1950-03 34.73
4 1950-04 34.73
5 1950-05 34.73
6 1950-06 34.73

> tail(gold)
Date   Price
822 2018-06 1282.13
823 2018-07 1238.06
824 2018-08 1201.86
825 2018-09 1199.20
826 2018-10 1214.73
827 2018-11 1221.28

我找到了每年每个月的平均值,但是现在我不确定在这段时间内如何取每个月的平均值.

I found the mean of each month for each year but now I am not sure how take the mean for each month over that span of time.

推荐答案

这是您要找的吗?

df %>% 
    separate(Date, c("Year","Month")) %>% 
    group_by(Month) %>% 
    summarise(Mean = mean(Price))

   Month   Mean
   <chr>  <dbl>
 1 01      34.7
 2 02      34.7
 3 03      34.7
 4 04      34.7
 5 05      34.7
 6 06     658. 
 7 07    1238. 
 8 08    1202. 
 9 09    1199. 
10 10    1215. 
11 11    1221. 

这篇关于R help可以计算几年中几个月的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆