[英]R programming - Split up a group of time series indexed by ID with irregular observation periods into regular monthly observations
[英]R aggregate data in 6-monthly periods by group
希望按組匯總半年期間的數據(平均值)。
這是數據的快照:
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個月進行一次匯總?
我覺得我在這里簡化了一個簡單的問題!
在當前變量之后添加另一個變量:
mutate(yearhalf = as.integer(6/7)+1) %>%
前6個月的輸出為1,第7到12個月的輸出為2。那么您當然必須為新名稱改編以下功能,但這應該可以解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.