2017-06-22 114 views
0

期望按组汇总半年期间的数据(平均值)。R按组分组的6个月期间的汇总数据

下面是数据的快照:

Date Score Group Score2 
01/01/2015 15 A 11 
02/01/2015 34 A 33 
03/01/2015 16 A 1 
04/01/2015 29 A 36 
05/01/2015 4 A 28 
06/01/2015 10 B 33 
07/01/2015 21 B 19 
08/01/2015 6 B 47 
09/01/2015 40 B 15 
10/01/2015 34 B 13 
11/01/2015 16 B 7 
12/01/2015 8 B 4 

dfd$mon<-as.yearmon(dfd$Date)然后

r<-as.data.frame(dfd %>% 
    mutate(month = format(Date, "%m"), year = format(Date, "%Y")) %>% 
    group_by(Group,mon) %>% 
    summarise(total = mean(Score), total1 = mean(Score2))) 

每月汇总,而是你怎么会为每6个月做到这一点,通过集团进行分组?

我觉得我在这里过分简单的问题!

回答

0

添加另一发生变异在当前操作后:

mutate(yearhalf = as.integer(6/7)+1) %>% 

输出为1的第6个月和2月7〜12。然后你当然必须适应以下功能为新的名称,但这应该是诀窍。

+0

对不起一月,我只得到1所有行? –

+0

您能否按照[此处](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)中所述提供示例数据的代码,然后我将提供完整代码为您的解决方案。在飞行中首先做到了这一点...... – Jan