I have the following ten year daily data :
library(lubridate)
library(dplyr)
head(infy_close_subset,24)
date INFY.NS.Close
1 2007-01-02 568.162
2 2007-01-03 577.838
3 2007-01-04 571.325
4 2007-01-05 568.763
5 2007-01-08 551.400
6 2007-01-09 547.525
7 2007-01-10 541.112
8 2007-01-11 545.750
9 2007-01-12 555.850
10 2007-01-15 560.737
11 2007-01-16 555.550
12 2007-01-17 551.362
13 2007-01-18 556.037
14 2007-01-19 550.588
15 2007-01-22 563.500
16 2007-01-23 558.787
17 2007-01-24 558.513
18 2007-01-25 560.250
19 2007-01-29 561.100
20 2007-01-31 561.825
21 2007-02-01 567.237
22 2007-02-02 566.388
23 2007-02-05 567.325
24 2007-02-06 568.237
I am trying to create a new column of average by year and month as below:
Infy_monthlyAvg <- infy_close_subset %>%
group_by(yr = year(date), mon = month(date)) %>%
summarize(mean_close = mean(INFY.NS.Close))
What I get is just a singe mean value as below:
head(Infy_monthlyAvg)
mean_close
1 731.6223
I am looking to add a column mean_close
appended to infy_close_subset
dataframe...
date INFY.NS.Close yr mon mean_close
<date> <dbl> <dbl> <dbl>
1 2007-01-02 568.162 2007 1 731.6223
2 2007-01-03 577.838 2007 1 731.6223
3 2007-01-04 571.325 2007 1 731.6223
4 2007-01-05 568.763 2007 1 731.6223
5 2007-01-08 551.400 2007 1 731.6223
6 2007-01-09 547.525 2007 1 731.6223
.................
999 2017-09-08 988.400 2007 9 921.3333
1000 2017-09-09 977.525 2007 9 921.3333
2017-01
. – Kevin Arseneauyear
&month
functions from the lubridate package? Please specify as they are not part of the system library. – Z.Lin