0
votes

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
3
You are only getting a single result because you are grouping by year and month, there is only valid group 2017-01.Kevin Arseneau
Are you using the year & month functions from the lubridate package? Please specify as they are not part of the system library.Z.Lin

3 Answers

2
votes

I would be inclined to make a period column

df <- left_join(
  infy_close_subset %>%
    mutate(
      period = format(date, "%Y-%m"),
      yr = year(date),
      mon = month(date)
    ),
  infy_close_subset %>%
    mutate(period = format(date, "%Y-%m")) %>% 
    group_by(period) %>% 
    summarise(mean_close = mean(INFY.NS.Close)
  ),
  by = "period"
) %>%
select(-period)

#          date INFY.NS.Close   yr mon mean_close
# 1  2007-01-02       568.162 2007   1   558.2987
# 2  2007-01-03       577.838 2007   1   558.2987
# 3  2007-01-04       571.325 2007   1   558.2987
# 4  2007-01-05       568.763 2007   1   558.2987
# 5  2007-01-08       551.400 2007   1   558.2987
# 6  2007-01-09       547.525 2007   1   558.2987
# 7  2007-01-10       541.112 2007   1   558.2987
# 8  2007-01-11       545.750 2007   1   558.2987
# 9  2007-01-12       555.850 2007   1   558.2987
# 10 2007-01-15       560.737 2007   1   558.2987
# 11 2007-01-16       555.550 2007   1   558.2987
# 12 2007-01-17       551.362 2007   1   558.2987
# 13 2007-01-18       556.037 2007   1   558.2987
# 14 2007-01-19       550.588 2007   1   558.2987
# 15 2007-01-22       563.500 2007   1   558.2987
# 16 2007-01-23       558.787 2007   1   558.2987
# 17 2007-01-24       558.513 2007   1   558.2987
# 18 2007-01-25       560.250 2007   1   558.2987
# 19 2007-01-29       561.100 2007   1   558.2987
# 20 2007-01-31       561.825 2007   1   558.2987
# 21 2007-02-01       567.237 2007   2   567.2967
# 22 2007-02-02       566.388 2007   2   567.2967
# 23 2007-02-05       567.325 2007   2   567.2967
# 24 2007-02-06       568.237 2007   2   567.2967
2
votes

A solution utilizing data.table:

library(data.table)
setDT(infy_close_subset)
infy_close_subset[, mean_close := mean(INFY.NS.Close), by = format(date, "%Y-%m")]
1
votes

If you add the yr and mon columns to your original dataframe:

infy_close_subset = infy_close_subset %>% 
    mutate(yr = year(date), mon = month(date))

then you could merge your two resulting tables by yr and mon:

answer = merge(infy_close_subset, Infy_monthlyAvg, by = c("yr", "mon")

I'm assuming you want monthly means. If you wanted overall mean, then the answer becomes simply:

answer = infy_close_subset %>% 
    mutate(mean_close = mean(infy_close_subset$INFY.NS.Close))

without the intermediate steps of grouping, summarising and merging.