R help计算几年中几个月的平均值

时间:2018-12-18 22:45:47

标签: r mean

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

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

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

1 个答案:

答案 0 :(得分:3)

这是您要找的吗?

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. 
相关问题